⚡ Bolt: Offload bcrypt to threadpool to prevent event loop blocking - #93
⚡ Bolt: Offload bcrypt to threadpool to prevent event loop blocking#93benpiper wants to merge 1 commit into
Conversation
- Re-defined `verify_password` and `get_password_hash` as async in `auth.py`. - Offloaded internal `bcrypt` calls to `starlette.concurrency.run_in_threadpool`. - Updated endpoints in `main.py` (`login`, `register`, `setup_initial_admin`) to await these functions. - Prevents CPU-bound bcrypt hashing from blocking the asyncio event loop and causing severe latency spikes for concurrent API requests. Co-authored-by: benpiper <4343814+benpiper@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What
Refactored
get_password_hashandverify_passwordinbackend/auth.pyto beasyncfunctions that offload the slow, CPU-boundbcrypthashing algorithms to a background threadpool usingstarlette.concurrency.run_in_threadpool. The/api/auth/register,/api/auth/login, and/api/auth/setupendpoints inbackend/main.pywere updated toawaitthese new async functions.🎯 Why
Password hashing algorithms like
bcryptare intentionally designed to be slow and CPU-intensive to thwart brute-force attacks (typically taking 100ms - 500ms). When called synchronously inside anasync defFastAPI endpoint, these operations completely block the underlying Pythonasyncioevent loop. This means that while one user is logging in, no other concurrent requests (like serving video segments, polling jobs, etc.) can be processed by the server, causing severe and cascading latency spikes across the entire application.📊 Impact
🔬 Measurement
To verify the improvement:
cd backend && uv run uvicorn main:app --port 8000.wrkorlocust) or run concurrentcurlrequests hitting a lightweight, non-blocking endpoint (e.g.,/api/auth/statusor/api/features)./api/auth/loginwith valid or invalid credentials.loginrequest's bcrypt hash to complete before returning.loginrequest is actively computing the bcrypt hash in the background threadpool.PR created automatically by Jules for task 6929092177359738763 started by @benpiper