Skip to content

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

Open
wuodar wants to merge 1 commit into
aws-powertools:developfrom
wuodar:fix/local-http-request-isolation
Open

fix(event_handler): isolate local ASGI request state#8452
wuodar wants to merge 1 commit into
aws-powertools:developfrom
wuodar:fix/local-http-request-isolation

Conversation

@wuodar

@wuodar wuodar commented Sep 12, 2026

Copy link
Copy Markdown

Issue number: relates to #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
@sonarqubecloud

Copy link
Copy Markdown

@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
@powertools-for-aws-oss-automation

Copy link
Copy Markdown

Not all issues are linked correctly.

Please link each issue to the PR either manually or using a closing keyword in the format fixes #<issue-number> format.

If mentioning more than one issue, separate them with commas: i.e. fixes #<issue-number-1>, closes #<issue-number-2>.

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.

1 participant