Skip to content

fix(event_handler): isolate local ASGI request state - #8452

Merged
leandrodamascena merged 4 commits into
aws-powertools:developfrom
wuodar:fix/local-http-request-isolation
Sep 14, 2026
Merged

leandrodamascena merged 4 commits into
aws-powertools:developfrom
wuodar:fix/local-http-request-isolation

Conversation

@wuodar

@wuodar wuodar commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Issue number: Fixes #8451

Summary

Concurrent HTTP requests to one HttpResolverLocal ASGI app can consume another request's body or lose routing context when another request completes. With Pydantic validation enabled, six distinct requests in the reproduction all return the last body.

This draft isolates state on the local ASGI path. It does not introduce concurrent Lambda invocations or change APIGatewayHttpResolver.

Changes

  • Give each ASGI request a state object containing its event, Lambda-context stand-in, routing context, and processed middleware frames.
  • Carry that object through a ContextVar, including asyncio.to_thread middleware and callbacks scheduled back onto the event loop.
  • Reset the request binding on completion/cancellation instead of clearing a shared dictionary; preserve the synchronous resolve() path outside ASGI requests.
  • Add functional regressions for concurrent distinct validated bodies, a validation failure overlapping a pending request, and cancellation overlapping another/subsequent request.
  • Widen ASGI scope annotations to the mapping interfaces accepted by HTTP clients.

User experience

A local development server can handle overlapping requests without those requests overwriting the resolver's event/context. Resolution stays concurrent rather than putting a lock around every HTTP request.

Validation

  • All three new regressions fail against unchanged develop and pass with this patch.
  • python -m pytest tests/functional/event_handler -o addopts='' -q: 863 passed, 1 skipped.
  • Ruff check and format check pass for both changed files.
  • mypy aws_lambda_powertools/event_handler/http_resolver.py --follow-imports=silent: passes.
  • Full make pr was attempted but could not start because Poetry is not installed in the contribution environment. The checks above used a separate virtual environment; full repository CI has not been claimed as passing.

Maintainer review / remaining scope

This is a draft pending issue triage and agreement on the state-isolation boundary. The patch covers state accessed through the resolver and resolver-bound dependency providers. Direct Router.current_event / Router.context access on an included upstream Router still uses legacy shared state and needs a design decision before treating this as a complete upstream solution. Existing synchronous resolver and event-handler tests remain green.

Cancellation isolation does not imply that this patch cancels handler tasks already scheduled by the existing synchronous middleware bridge; it ensures their state cannot be cleared by another request.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@boring-cyborg

boring-cyborg Bot commented Sep 12, 2026

Copy link
Copy Markdown

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@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 Sep 12, 2026
@wuodar
wuodar marked this pull request as ready for review September 13, 2026 21:31
@wuodar
wuodar requested a review from a team as a code owner September 13, 2026 21:31
Comment thread aws_lambda_powertools/event_handler/http_resolver.py
Comment thread aws_lambda_powertools/event_handler/http_resolver.py

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced the concurrency issue and agree with the direction. I found two compatibility regressions with included Router instances; details inline.

@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.62%. Comparing base (51c89a3) to head (46dc1ec).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8452      +/-   ##
===========================================
+ Coverage    96.60%   96.62%   +0.01%     
===========================================
  Files          296      296              
  Lines        14820    14883      +63     
  Branches      1257     1263       +6     
===========================================
+ Hits         14317    14380      +63     
  Misses         366      366              
  Partials       137      137              

☔ 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.

@sonarqubecloud

Copy link
Copy Markdown

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @wuodar! Amazing work, approved!

@leandrodamascena
leandrodamascena merged commit e0566b0 into aws-powertools:develop Sep 14, 2026
15 checks passed
@boring-cyborg

boring-cyborg Bot commented Sep 14, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@powertools-for-aws-oss-automation

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@wuodar
wuodar deleted the fix/local-http-request-isolation branch September 15, 2026 14:11
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 shares request state across concurrent ASGI requests

2 participants