Skip to content

Commit

Permalink
Add None check for grpc.aio interceptor (getsentry#3109)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Neel Shah <[email protected]>
  • Loading branch information
ordinary-jamie and sl0thentr0py authored May 29, 2024
1 parent b496a71 commit 84775a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 4 additions & 2 deletions sentry_sdk/integrations/grpc/aio/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

if TYPE_CHECKING:
from collections.abc import Awaitable, Callable
from typing import Any
from typing import Any, Optional


try:
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/integrations/grpc/test_grpc_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()])
Expand Down

0 comments on commit 84775a0

Please sign in to comment.