Skip to content

Commit 029c647

Browse files
Nick Gashkovuntitaker
Nick Gashkov
authored andcommitted
Fix 'asyncio.CancelledError' capturing for 'AioHttpIntegration' (getsentry#571)
* Add failing test for 'asyncio.CancelledError' * Fix 'asyncio.CancelledError' capturing * Add 'cancelled' to the span's status * Make 'black' happy
1 parent 5c94499 commit 029c647

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

sentry_sdk/integrations/aiohttp.py

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ async def inner():
8282
except HTTPException as e:
8383
span.set_http_status(e.status_code)
8484
raise
85+
except asyncio.CancelledError:
86+
span.set_status("cancelled")
87+
raise
8588
except Exception:
8689
# This will probably map to a 500 but seems like we
8790
# have no way to tell. Do not set span status.

tests/integrations/aiohttp/test_aiohttp.py

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import asyncio
12
import json
3+
from contextlib import suppress
24

35
from aiohttp import web
6+
from aiohttp.client import ServerDisconnectedError
47

58
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
69

@@ -120,6 +123,28 @@ async def hello(request):
120123
assert not events
121124

122125

126+
async def test_cancelled_error_not_captured(
127+
sentry_init, aiohttp_client, loop, capture_events
128+
):
129+
sentry_init(integrations=[AioHttpIntegration()])
130+
131+
async def hello(request):
132+
raise asyncio.CancelledError()
133+
134+
app = web.Application()
135+
app.router.add_get("/", hello)
136+
137+
events = capture_events()
138+
client = await aiohttp_client(app)
139+
140+
with suppress(ServerDisconnectedError):
141+
# Intended `aiohttp` interaction: server will disconnect if it
142+
# encounters `asyncio.CancelledError`
143+
await client.get("/")
144+
145+
assert not events
146+
147+
123148
async def test_half_initialized(sentry_init, aiohttp_client, loop, capture_events):
124149
sentry_init(integrations=[AioHttpIntegration()])
125150
sentry_init()

0 commit comments

Comments
 (0)