Hash and verify passwords off the event loop (B-55)
Argon2 is deliberately expensive — tens of milliseconds of CPU per call. Called inline from the async handlers for register, login, change-password and the admin reset, that cost froze the entire process for its duration: every other request, plus all six background tasks (scheduler, confirmation poller, RBF bumper, listener, both reconcilers). A burst of unauthenticated login attempts was therefore not just slow logins, it delayed draws and confirmations. hash_password_async/verify_password_async wrap the existing pair in run_in_threadpool, and every async caller now uses them. The synchronous functions stay: they're what the wrappers call, and what tests and scripts (no running loop) use directly. The regression test runs a heartbeat task alongside the hashing and counts how often the loop got to run it — 1 tick with the old inline call, many with the threadpooled one. Also drops the running "already fixed and removed" list from BUGS.md: the file tracks open findings, and `git log --all --grep 'B-nn'` is the record of how a closed one was closed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import asyncio
|
||||
|
||||
from app.auth import security
|
||||
|
||||
|
||||
@@ -42,3 +44,59 @@ def test_verify_password_still_rejects_a_wrong_password():
|
||||
stored = hash_password("correct-horse-battery")
|
||||
assert verify_password("correct-horse-battery", stored) is True
|
||||
assert verify_password("wrong", stored) is False
|
||||
|
||||
|
||||
# --- B-55: Argon2 must not run on the event loop --------------------------------
|
||||
|
||||
|
||||
async def _count_loop_ticks_during(coro) -> tuple[object, int]:
|
||||
"""Runs `coro` while a heartbeat task tries to run as often as the event loop
|
||||
lets it. A blocking call starves the heartbeat completely; a threadpooled one
|
||||
leaves the loop free the whole time."""
|
||||
ticks = 0
|
||||
|
||||
async def heartbeat() -> None:
|
||||
nonlocal ticks
|
||||
while True:
|
||||
ticks += 1
|
||||
await asyncio.sleep(0)
|
||||
|
||||
task = asyncio.create_task(heartbeat())
|
||||
await asyncio.sleep(0) # let the heartbeat reach its loop before timing starts
|
||||
try:
|
||||
result = await coro
|
||||
finally:
|
||||
task.cancel()
|
||||
return result, ticks
|
||||
|
||||
|
||||
async def test_hash_password_async_keeps_the_event_loop_free():
|
||||
"""Argon2 costs tens of milliseconds of CPU by design. Run inline from an async
|
||||
handler it froze the whole process for that long — every other request plus all
|
||||
six background tasks (scheduler, confirmation poller, RBF bumper, listener, both
|
||||
reconcilers) — which made a burst of unauthenticated login attempts a cheap way
|
||||
to delay draws and confirmations."""
|
||||
hashed, ticks = await _count_loop_ticks_during(security.hash_password_async("s3cret-passphrase"))
|
||||
|
||||
assert security.verify_password("s3cret-passphrase", hashed)
|
||||
assert ticks > 1 # the loop kept running while the hashing happened
|
||||
|
||||
|
||||
async def test_verify_password_async_keeps_the_event_loop_free():
|
||||
stored = security.hash_password("correct-horse-battery")
|
||||
|
||||
ok, ticks = await _count_loop_ticks_during(
|
||||
security.verify_password_async("correct-horse-battery", stored)
|
||||
)
|
||||
|
||||
assert ok is True
|
||||
assert ticks > 1
|
||||
|
||||
|
||||
async def test_verify_password_async_rejects_a_wrong_password():
|
||||
"""Same answers as the synchronous function it wraps — including the B-13
|
||||
unparseable-hash case, which must read as "wrong password", not as an error."""
|
||||
stored = security.hash_password("correct-horse-battery")
|
||||
|
||||
assert await security.verify_password_async("wrong", stored) is False
|
||||
assert await security.verify_password_async("whatever", "not-an-argon2-hash") is False
|
||||
|
||||
Reference in New Issue
Block a user