From 84775a028f3bdf5d4ffb549e92bcf6ee852f5aa3 Mon Sep 17 00:00:00 2001 From: Jamie Phan Date: Wed, 29 May 2024 21:53:52 +0800 Subject: [PATCH] Add None check for grpc.aio interceptor (#3109) --------- Co-authored-by: Neel Shah --- sentry_sdk/integrations/grpc/aio/server.py | 6 ++++-- tests/integrations/grpc/test_grpc_aio.py | 23 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/integrations/grpc/aio/server.py b/sentry_sdk/integrations/grpc/aio/server.py index 550f194c62..a3027dbd4f 100644 --- a/sentry_sdk/integrations/grpc/aio/server.py +++ b/sentry_sdk/integrations/grpc/aio/server.py @@ -7,7 +7,7 @@ if TYPE_CHECKING: from collections.abc import Awaitable, Callable - from typing import Any + from typing import Any, Optional try: @@ -26,9 +26,11 @@ def __init__(self, find_name=None): super().__init__() async def intercept_service(self, continuation, handler_call_details): - # type: (ServerInterceptor, Callable[[HandlerCallDetails], Awaitable[RpcMethodHandler]], HandlerCallDetails) -> Awaitable[RpcMethodHandler] + # type: (ServerInterceptor, Callable[[HandlerCallDetails], Awaitable[RpcMethodHandler]], HandlerCallDetails) -> Optional[Awaitable[RpcMethodHandler]] self._handler_call_details = handler_call_details handler = await continuation(handler_call_details) + if handler is None: + return None if not handler.request_streaming and not handler.response_streaming: handler_factory = grpc.unary_unary_rpc_method_handler diff --git a/tests/integrations/grpc/test_grpc_aio.py b/tests/integrations/grpc/test_grpc_aio.py index 0b02a59f71..4faebb6172 100644 --- a/tests/integrations/grpc/test_grpc_aio.py +++ b/tests/integrations/grpc/test_grpc_aio.py @@ -29,6 +29,29 @@ def event_loop(request): loop.close() +@pytest.mark.asyncio +async def test_noop_for_unimplemented_method(sentry_init, capture_events, event_loop): + sentry_init(traces_sample_rate=1.0, integrations=[GRPCIntegration()]) + server = grpc.aio.server() + server.add_insecure_port("[::]:{}".format(AIO_PORT)) + + await event_loop.create_task(server.start()) + + events = capture_events() + try: + async with grpc.aio.insecure_channel( + "localhost:{}".format(AIO_PORT) + ) as channel: + stub = gRPCTestServiceStub(channel) + with pytest.raises(grpc.RpcError) as exc: + await stub.TestServe(gRPCTestMessage(text="test")) + assert exc.value.details() == "Method not found!" + finally: + await server.stop(None) + + assert not events + + @pytest_asyncio.fixture(scope="function") async def grpc_server(sentry_init, event_loop): sentry_init(traces_sample_rate=1.0, integrations=[GRPCIntegration()])