Repository navigation
fix(event_handler): run consecutive sync middlewares on one thread in the async chain - #8907
Merged
Merged
Conversation
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #8907 +/- ##
========================================
Coverage 97.14% 97.15%
========================================
Files 327 327
Lines 16802 16813 +11
Branches 1538 1539 +1
========================================
+ Hits 16323 16334 +11
Misses 336 336
Partials 143 143 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Issue number:
closes #8866
Summary
Changes
HttpResolverLocalwithenable_validation=Truestopped answering for good once enough requests arrived at the same time. @dacevedo12 had the diagnosis right in the issue: every sync middleware runs inasyncio.to_thread, and itsnext()hops back to the event loop, which then needs a second executor thread to run the next sync middleware. Validation adds two sync middlewares around every route, so with all executor threads blocked inside the first one, the second never starts and every request waits forever. Two user-written sync middlewares in a row trigger the same deadlock, validation or not.The fix keeps consecutive sync middlewares on the thread they are already in.
AsyncMiddlewareFramenow decides once, at construction, whether its middleware is async, and the syncnext()only goes throughrun_coroutine_threadsafewhen the next step is async (an async middleware or the route adapter). A sync next frame is called directly in the same worker thread, so one request holds one executor thread, not one per sync middleware. The async path and the single-middleware sync path are unchanged.User experience
Before: with validation on, a burst of requests under uvicorn hung the server, and it had to be killed. After: the same burst answers 200 for every request, and a single request afterwards works. Nothing changes for Lambda invocations, which use the sync
resolve().Verification
develop(64 concurrent requests: every one timed out, and so did the single request after). With this branch the burst returns{200}and the single request200._pydantic/, the chained-middleware one underrequired_dependencies/.tests/functional/event_handlerandtests/unit/event_handler: 1290 passed. noxtest_with_only_required_packagesandtest_with_pydantic_required_packagegreen. ruff, mypy and xenon clean.Written with an AI assistant; I ran everything above myself.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.