Skip to content

fix(event_handler): run consecutive sync middlewares on one thread in the async chain - #8907

Merged
leandrodamascena merged 1 commit into
developfrom
fix/8866-async-middleware-deadlock
Nov 25, 2026
Merged

leandrodamascena merged 1 commit into
developfrom
fix/8866-async-middleware-deadlock

Conversation

@leandrodamascena

Copy link
Copy Markdown
Contributor

Issue number:

closes #8866

Summary

Changes

HttpResolverLocal with enable_validation=True stopped answering for good once enough requests arrived at the same time. @dacevedo12 had the diagnosis right in the issue: every sync middleware runs in asyncio.to_thread, and its next() 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. AsyncMiddlewareFrame now decides once, at construction, whether its middleware is async, and the sync next() only goes through run_coroutine_threadsafe when 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

  • Reproduced the hang with the script from the issue under uvicorn on 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 request 200.
  • Three new tests through the public ASGI interface, with a two-thread default executor so the starvation is deterministic: validation on with six concurrent requests, two chained user sync middlewares with six concurrent requests, and twelve distinct concurrent requests that must each get their own value and context back. All three time out without the fix and pass with it. The two that need validation live under _pydantic/, the chained-middleware one under required_dependencies/.
  • tests/functional/event_handler and tests/unit/event_handler: 1290 passed. nox test_with_only_required_packages and test_with_pydantic_required_package green. 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.

@leandrodamascena
leandrodamascena requested a review from a team as a code owner November 25, 2026 11:02
@leandrodamascena leandrodamascena added this to the 3.42.0 milestone Nov 25, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 25, 2026
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.15%. Comparing base (0f41207) to head (125cf4f).
⚠️ Report is 1 commits behind head on develop.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena
leandrodamascena merged commit a1f97bd into develop Nov 25, 2026
18 checks passed
@leandrodamascena
leandrodamascena deleted the fix/8866-async-middleware-deadlock branch November 25, 2026 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event_handlers size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: HttpResolverLocal with enable_validation hangs under concurrent ASGI requests

1 participant