From c28c609badd1557f9b58a7bbf475d1f19e7afe5f Mon Sep 17 00:00:00 2001 From: Mohil-Ahuja Date: Tue, 17 Nov 2026 15:54:04 +0530 Subject: [PATCH 1/2] fix(gql): Scope the event processor to the query The event processor was added to the isolation scope on every execute() and never removed, so later unrelated events got the last query's request data, and more than 20 queries made the scope drop all of its processors. Add it to a forked scope instead. Also set contexts.response.type to "response" instead of the response dict itself. Fixes #8262 --- sentry_sdk/integrations/gql.py | 35 ++++++++++++---------- tests/integrations/gql/test_gql.py | 48 ++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 16 deletions(-) diff --git a/sentry_sdk/integrations/gql.py b/sentry_sdk/integrations/gql.py index a05eeaa202..0b5e1e7c2d 100644 --- a/sentry_sdk/integrations/gql.py +++ b/sentry_sdk/integrations/gql.py @@ -144,22 +144,25 @@ def sentry_patched_execute( *args: "Any", **kwargs: "Any", ) -> "Any": - scope = sentry_sdk.get_isolation_scope() - # document is a gql.GraphQLRequest with gql v4.0.0. - scope.add_event_processor(_make_gql_event_processor(self, document)) - - try: + # Add the processor to a scope that lives only as long as this + # query, so that it doesn't apply to unrelated events captured + # later on the same isolation scope. + with sentry_sdk.new_scope() as scope: # document is a gql.GraphQLRequest with gql v4.0.0. - return real_execute(self, document, *args, **kwargs) - except TransportQueryError as e: - event, hint = event_from_exception( - e, - client_options=sentry_sdk.get_client().options, - mechanism={"type": "gql", "handled": False}, - ) + scope.add_event_processor(_make_gql_event_processor(self, document)) + + try: + # document is a gql.GraphQLRequest with gql v4.0.0. + return real_execute(self, document, *args, **kwargs) + except TransportQueryError as e: + event, hint = event_from_exception( + e, + client_options=sentry_sdk.get_client().options, + mechanism={"type": "gql", "handled": False}, + ) - sentry_sdk.capture_event(event, hint) - raise e + sentry_sdk.capture_event(event, hint) + raise e gql.Client.execute = sentry_patched_execute @@ -200,7 +203,7 @@ def processor(event: "Event", hint: "dict[str, Any]") -> "Event": response.update( { "data": {"errors": errors}, - "type": response, + "type": "response", } ) @@ -221,7 +224,7 @@ def processor(event: "Event", hint: "dict[str, Any]") -> "Event": response.update( { "data": {"errors": errors}, - "type": response, + "type": "response", } ) diff --git a/tests/integrations/gql/test_gql.py b/tests/integrations/gql/test_gql.py index 9c0c1c94ed..1dbfe9069d 100644 --- a/tests/integrations/gql/test_gql.py +++ b/tests/integrations/gql/test_gql.py @@ -6,6 +6,7 @@ from gql.transport.exceptions import TransportQueryError from gql.transport.requests import RequestsHTTPTransport +import sentry_sdk from sentry_sdk.integrations.gql import GQLIntegration from sentry_sdk.utils import parse_version @@ -356,3 +357,50 @@ def test_transport_url_is_sanitized( assert event["request"].get("query_string") == expected_query_string assert "password" not in str(event["request"]) assert "secret" not in str(event["request"]) + + +def test_event_processor_does_not_outlive_query(sentry_init, capture_events): + """ + Events captured after a query has finished must not get the query's + request data. + """ + sentry_init(integrations=[GQLIntegration()], send_default_pii=True) + events = capture_events() + + _execute_mock_query({"data": {"example": "This is the example"}}) + sentry_sdk.capture_message("unrelated") + + (event,) = events + assert "request" not in event + assert "response" not in event.get("contexts", {}) + + +def test_queries_do_not_remove_other_event_processors(sentry_init, capture_events): + """ + Running many queries must not fill up the isolation scope's event + processors, which would make the scope drop processors added by others. + """ + sentry_init(integrations=[GQLIntegration()]) + events = capture_events() + + def add_tag(event, hint): + event.setdefault("tags", {})["other_processor"] = "ran" + return event + + sentry_sdk.get_isolation_scope().add_event_processor(add_tag) + + for _ in range(25): + _execute_mock_query({"data": {"example": "This is the example"}}) + + sentry_sdk.capture_message("unrelated") + + (event,) = events + assert event["tags"]["other_processor"] == "ran" + + +def test_response_context_type(sentry_init, capture_events): + sentry_init(integrations=[GQLIntegration()], send_default_pii=True) + + event = _make_erroneous_query(capture_events, _execute_mock_query) + + assert event["contexts"]["response"]["type"] == "response" From 8cbc64d5c9443fce57e14c4bce3a61e9c674d749 Mon Sep 17 00:00:00 2001 From: Mohil-Ahuja Date: Tue, 17 Nov 2026 23:25:30 +0530 Subject: [PATCH 2/2] test(gql): Check the response context type in an existing test Drop the processor-count test, which only covered a side effect of the old behavior, and move the response type assertion into test_real_gql_request_with_error_with_pii. --- tests/integrations/gql/test_gql.py | 32 +----------------------------- 1 file changed, 1 insertion(+), 31 deletions(-) diff --git a/tests/integrations/gql/test_gql.py b/tests/integrations/gql/test_gql.py index 1dbfe9069d..13fc2177df 100644 --- a/tests/integrations/gql/test_gql.py +++ b/tests/integrations/gql/test_gql.py @@ -174,6 +174,7 @@ def test_real_gql_request_with_error_with_pii( assert "data" in event["request"] assert "response" in event["contexts"] + assert event["contexts"]["response"]["type"] == "response" @pytest.mark.parametrize("execute_query", _execute_query_funcs) @@ -373,34 +374,3 @@ def test_event_processor_does_not_outlive_query(sentry_init, capture_events): (event,) = events assert "request" not in event assert "response" not in event.get("contexts", {}) - - -def test_queries_do_not_remove_other_event_processors(sentry_init, capture_events): - """ - Running many queries must not fill up the isolation scope's event - processors, which would make the scope drop processors added by others. - """ - sentry_init(integrations=[GQLIntegration()]) - events = capture_events() - - def add_tag(event, hint): - event.setdefault("tags", {})["other_processor"] = "ran" - return event - - sentry_sdk.get_isolation_scope().add_event_processor(add_tag) - - for _ in range(25): - _execute_mock_query({"data": {"example": "This is the example"}}) - - sentry_sdk.capture_message("unrelated") - - (event,) = events - assert event["tags"]["other_processor"] == "ran" - - -def test_response_context_type(sentry_init, capture_events): - sentry_init(integrations=[GQLIntegration()], send_default_pii=True) - - event = _make_erroneous_query(capture_events, _execute_mock_query) - - assert event["contexts"]["response"]["type"] == "response"