Skip to content

gRPC aio ServerInterceptor missing isolation_scope per request #5894

@robinvd

Description

@robinvd

Description

The async gRPC server interceptor (sentry_sdk/integrations/grpc/aio/server.py) does not create an isolation_scope() per request, unlike its sync counterpart and every other request-handling integration.

This causes scope state (attachments, tags, breadcrumbs, extras) to leak between requests.

Sync vs Async

The sync interceptor correctly isolates each request:

with sentry_sdk.isolation_scope():

with sentry_sdk.isolation_scope():
    ...
    with sentry_sdk.start_transaction(transaction=transaction):
        return handler.unary_unary(request, context)

The async interceptor skips isolation:

with sentry_sdk.start_transaction(transaction=transaction):

# no isolation_scope()
with sentry_sdk.start_transaction(transaction=transaction):
    return await handler.unary_unary(request, context)

Other async integrations all use isolation_scope():

Impact

We add various debug attachments to the sentry scope. And on sync grpc this works, but on async grpc every request gets all the attachments from the previous requests to the same pod. Causing more storage usage and weird debug viewing.

Expected fix

Wrap the handler in sentry_sdk.isolation_scope(), matching the sync implementation:

async def wrapped(request, context):
    name = self._find_method_name(context)
    if not name:
        return await handler(request, context)

    with sentry_sdk.isolation_scope():
        transaction = sentry_sdk.continue_trace(...)
        with sentry_sdk.start_transaction(transaction=transaction):
            ...

i can make a 1 line PR if you agree :)

Metadata

Metadata

Assignees

No one assigned
    No fields configured for issues without a type.

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions