Skip to content

fix(event_handler): clear AppSyncEventsResolver context on exception - #8959

Open
mayankcodes-dev wants to merge 1 commit into
aws-powertools:developfrom
mayankcodes-dev:fix/appsync-events-clear-context
Open

mayankcodes-dev wants to merge 1 commit into
aws-powertools:developfrom
mayankcodes-dev:fix/appsync-events-clear-context

Conversation

@mayankcodes-dev

Copy link
Copy Markdown

Issue number: closes #8958

Summary

Changes

Wrap _setup_context() and event resolution inside try ... finally: self.clear_context() in AppSyncEventsResolver.resolve() (aws_lambda_powertools/event_handler/events_appsync/appsync_events.py), matching ApiGatewayResolver.resolve(), AppSyncResolver.resolve(), and BedrockAgentFunctionResolver.resolve() (#8874).

Added three functional tests in tests/functional/event_handler/required_dependencies/appsync/test_appsync_events_unauthorized.py:

  • test_publish_clears_context_when_unauthorized_raised
  • test_subscribe_clears_context_when_unauthorized_raised
  • test_resolve_clears_context_when_event_setup_raises

User experience

  • Before: When AppSyncEventsResolver.resolve() raised UnauthorizedException (or failed during event setup), self.clear_context() was skipped, leaking app.context entries into subsequent warm Lambda invocations.
  • After: self.clear_context() always executes in the finally block so app.context is cleared even when an exception escapes resolve().

Verification

Verified locally against origin/develop (0bbc252, Python 3.14.4) after the final edit:

  • Reproducer TDD check: the 3 new tests failed before the fix (AssertionError: assert {'user_id': '12345'} == {}) and pass with the fix applied.
  • pytest tests/functional/event_handler/required_dependencies/appsync/ tests/unit/event_handler/_required_dependencies/appsync_events/ -v: 104 passed, 1 skipped (1 pre-existing skip in test_appsync_events_resolvers.py:1502).
  • ruff check and ruff format --check on both modified files: all checks passed, 2 files already formatted.
  • mypy on both modified files: Success: no issues found in 2 source files.

Prepared with AI assistance and verified locally by @mayankcodes-dev.


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

@mayankcodes-dev
mayankcodes-dev requested a review from a team as a code owner December 5, 2026 04:58
@mayankcodes-dev
mayankcodes-dev requested review from svozza and a balanced review from Copilot December 5, 2026 04:58
@boring-cyborg

boring-cyborg Bot commented Dec 5, 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/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Dec 5, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@sonarqubecloud

sonarqubecloud Bot commented Dec 5, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event_handlers size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: AppSyncEventsResolver leaks context across invocations when resolving raises an exception

2 participants