Repository navigation
Description
Expected Behaviour
When AppSyncEventsResolver.resolve() raises an exception (such as UnauthorizedException from an @app.on_publish / @app.on_subscribe handler, or an invalid event failure during setup), self.clear_context() should still run in a finally block so app.context (and any shared router.context) does not leak into the next warm Lambda invocation.
This matches ApiGatewayResolver.resolve(), AppSyncResolver.resolve(), and BedrockAgentFunctionResolver.resolve() (#8874).
Current Behaviour
AppSyncEventsResolver.resolve() (aws_lambda_powertools/event_handler/events_appsync/appsync_events.py) calls self.clear_context() only after _publish_events() or _subscribe_events() returns normally:
self._setup_context(event, context)
if self.current_event.info.operation == "PUBLISH":
response = self._publish_events(payload=self.current_event.events)
else:
response = self._subscribe_events()
self.clear_context()
return responseBecause UnauthorizedException is intentionally re-raised in _subscribe_events(), _process_publish_event_sync_resolver(), and _call_publish_event_async_resolver() (and _setup_context can raise on malformed events), self.clear_context() is bypassed whenever an exception escapes. Any keys added via app.append_context(...) before the failure (for example on an earlier event in the same publish batch, inside a subscribe handler, or before resolve) persist into subsequent Lambda invocations.
Code snippet
from aws_lambda_powertools.event_handler import AppSyncEventsResolver
from aws_lambda_powertools.event_handler.events_appsync.exceptions import UnauthorizedException
app = AppSyncEventsResolver()
calls = 0
@app.on_publish(path="/default/*")
def handle_event(payload):
global calls
calls += 1
if calls == 1:
app.append_context(user_id="12345")
return {"processed": True}
if calls == 2:
raise UnauthorizedException
return dict(app.context)
batch_event = {
"identity": None,
"request": {"headers": {}, "domainName": None},
"info": {
"channel": {"path": "/default/test", "segments": ["default", "test"]},
"channelNamespace": {"name": "default"},
"operation": "PUBLISH",
},
"events": [{"id": "1", "payload": {}}, {"id": "2", "payload": {}}],
}
try:
app.resolve(batch_event, object())
except UnauthorizedException:
pass
print("After failed invocation, app.context =", app.context)Possible Solution
Wrap _setup_context and event dispatch in AppSyncEventsResolver.resolve() inside try ... finally: self.clear_context().
Steps to Reproduce
- Run the batch / subscribe failure scenario against
origin/develop(0bbc252) whereapp.append_context(user_id="12345")runs beforeUnauthorizedExceptionis raised. - Catch
UnauthorizedExceptionand inspectapp.context(or invokeapp.resolve(...)a second time). - Observe
app.contextstill contains{'user_id': '12345'}instead of{}.
Did you run the reproduction yourself?
Yes, I ran it and saw the failure
Powertools for AWS Lambda (Python) version
latest
AWS Lambda function runtime
3.14
Packaging format used
PyPi
Debugging logs
AssertionError: assert {'user_id': '12345'} == {}
Prepared with AI assistance and verified locally by @mayankcodes-dev.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTriage
Activity