Skip to content

Commit fdb7512

Browse files
authored
The wrapped receive() did not return anything. (#1698)
We wrapped the receive() callback of all ASGI middleware to create spans when they where executed. The receive() callback is used to receive message from the server. But we forgot to return the value that the original receive() callback returns. So basically swallowing the return of the server. Refs #1696
1 parent cfab78d commit fdb7512

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

sentry_sdk/integrations/starlette.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ async def _sentry_receive(*args, **kwargs):
106106
description=receive.__qualname__,
107107
) as span:
108108
span.set_tag("starlette.middleware_name", middleware_name)
109-
await receive(*args, **kwargs)
109+
return await receive(*args, **kwargs)
110110

111111
receive_patched = receive.__name__ == "_sentry_receive"
112112
new_receive = _sentry_receive if not receive_patched else receive
@@ -119,15 +119,15 @@ async def _sentry_send(*args, **kwargs):
119119
op=OP.MIDDLEWARE_STARLETTE_SEND, description=send.__qualname__
120120
) as span:
121121
span.set_tag("starlette.middleware_name", middleware_name)
122-
await send(*args, **kwargs)
122+
return await send(*args, **kwargs)
123123

124124
send_patched = send.__name__ == "_sentry_send"
125125
new_send = _sentry_send if not send_patched else send
126126

127-
await old_call(app, scope, new_receive, new_send, **kwargs)
127+
return await old_call(app, scope, new_receive, new_send, **kwargs)
128128

129129
else:
130-
await old_call(app, scope, receive, send, **kwargs)
130+
return await old_call(app, scope, receive, send, **kwargs)
131131

132132
not_yet_patched = old_call.__name__ not in [
133133
"_create_span_call",

tests/integrations/starlette/test_starlette.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,21 @@ async def do_stuff(message):
174174
await self.app(scope, receive, do_stuff)
175175

176176

177+
class SampleReceiveSendMiddleware:
178+
def __init__(self, app):
179+
self.app = app
180+
181+
async def __call__(self, scope, receive, send):
182+
message = await receive()
183+
assert message
184+
assert message["type"] == "http.request"
185+
186+
send_output = await send({"type": "something-unimportant"})
187+
assert send_output is None
188+
189+
await self.app(scope, receive, send)
190+
191+
177192
@pytest.mark.asyncio
178193
async def test_starlettrequestextractor_content_length(sentry_init):
179194
with mock.patch(
@@ -644,6 +659,25 @@ def test_middleware_callback_spans(sentry_init, capture_events):
644659
idx += 1
645660

646661

662+
@pytest.mark.asyncio
663+
async def test_middleware_receive_send(sentry_init, capture_events):
664+
sentry_init(
665+
traces_sample_rate=1.0,
666+
integrations=[StarletteIntegration()],
667+
)
668+
starlette_app = starlette_app_factory(
669+
middleware=[Middleware(SampleReceiveSendMiddleware)]
670+
)
671+
672+
client = TestClient(starlette_app, raise_server_exceptions=False)
673+
try:
674+
# NOTE: the assert statements checking
675+
# for correct behaviour are in `SampleReceiveSendMiddleware`!
676+
client.get("/message", auth=("Gabriela", "hello123"))
677+
except Exception:
678+
pass
679+
680+
647681
def test_last_event_id(sentry_init, capture_events):
648682
sentry_init(
649683
integrations=[StarletteIntegration()],

0 commit comments

Comments
 (0)