Skip to content

Commit 84775a0

Browse files
Add None check for grpc.aio interceptor (#3109)
--------- Co-authored-by: Neel Shah <[email protected]>
1 parent b496a71 commit 84775a0

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

sentry_sdk/integrations/grpc/aio/server.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
if TYPE_CHECKING:
99
from collections.abc import Awaitable, Callable
10-
from typing import Any
10+
from typing import Any, Optional
1111

1212

1313
try:
@@ -26,9 +26,11 @@ def __init__(self, find_name=None):
2626
super().__init__()
2727

2828
async def intercept_service(self, continuation, handler_call_details):
29-
# type: (ServerInterceptor, Callable[[HandlerCallDetails], Awaitable[RpcMethodHandler]], HandlerCallDetails) -> Awaitable[RpcMethodHandler]
29+
# type: (ServerInterceptor, Callable[[HandlerCallDetails], Awaitable[RpcMethodHandler]], HandlerCallDetails) -> Optional[Awaitable[RpcMethodHandler]]
3030
self._handler_call_details = handler_call_details
3131
handler = await continuation(handler_call_details)
32+
if handler is None:
33+
return None
3234

3335
if not handler.request_streaming and not handler.response_streaming:
3436
handler_factory = grpc.unary_unary_rpc_method_handler

tests/integrations/grpc/test_grpc_aio.py

+23
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ def event_loop(request):
2929
loop.close()
3030

3131

32+
@pytest.mark.asyncio
33+
async def test_noop_for_unimplemented_method(sentry_init, capture_events, event_loop):
34+
sentry_init(traces_sample_rate=1.0, integrations=[GRPCIntegration()])
35+
server = grpc.aio.server()
36+
server.add_insecure_port("[::]:{}".format(AIO_PORT))
37+
38+
await event_loop.create_task(server.start())
39+
40+
events = capture_events()
41+
try:
42+
async with grpc.aio.insecure_channel(
43+
"localhost:{}".format(AIO_PORT)
44+
) as channel:
45+
stub = gRPCTestServiceStub(channel)
46+
with pytest.raises(grpc.RpcError) as exc:
47+
await stub.TestServe(gRPCTestMessage(text="test"))
48+
assert exc.value.details() == "Method not found!"
49+
finally:
50+
await server.stop(None)
51+
52+
assert not events
53+
54+
3255
@pytest_asyncio.fixture(scope="function")
3356
async def grpc_server(sentry_init, event_loop):
3457
sentry_init(traces_sample_rate=1.0, integrations=[GRPCIntegration()])

0 commit comments

Comments
 (0)