Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UnboundLocalError when aiohttp server raises a CancelledError #356

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aws_xray_sdk/ext/aiohttp/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def middleware(request, handler):
# Non 2XX responses are raised as HTTPExceptions
response = exc
six.raise_from(exc, exc)
except Exception as exc:
except BaseException as exc:
# Store exception information including the stacktrace to the segment
response = None
segment.put_http_meta(http.STATUS, 500)
Expand Down
11 changes: 6 additions & 5 deletions tests/ext/aiohttp/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ async def handle_unauthorized(self, request: web.Request) -> web.Response:

async def handle_exception(self, request: web.Request) -> web.Response:
"""
Handle /exception which raises a KeyError
Handle /exception which raises a CancelledError; this is important, as starting from python 3.8 CancelledError
extends BaseException instead of Exception
"""
return {}['key']
raise asyncio.CancelledError()

async def handle_delay(self, request: web.Request) -> web.Response:
"""
Expand Down Expand Up @@ -213,8 +214,8 @@ async def test_exception(test_client, loop, recorder):
"""
client = await test_client(ServerTest.app(loop=loop))

resp = await client.get('/exception')
await resp.text() # Need this to trigger Exception
with pytest.raises(Exception):
await client.get('/exception')

segment = recorder.emitter.pop()
assert not segment.in_progress
Expand All @@ -227,7 +228,7 @@ async def test_exception(test_client, loop, recorder):
assert request['url'] == 'http://127.0.0.1:{port}/exception'.format(port=client.port)
assert request['client_ip'] == '127.0.0.1'
assert response['status'] == 500
assert exception.type == 'KeyError'
assert exception.type == 'CancelledError'


async def test_unhauthorized(test_client, loop, recorder):
Expand Down