From c356be841d19039431e3f5a57cbf3259271d25fd Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Thu, 21 May 2026 13:14:46 -0400 Subject: [PATCH] fix(strawberry): Wrap yields in try-except to ensure span cleanup Ensures that validation and parsing spans are properly closed even if an exception is raised during these operations. Moves yield statements into try/finally blocks so span cleanup always happens. Fixes PY-2425 Fixes #6309 --- sentry_sdk/integrations/strawberry.py | 28 +++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index 7fe4609cf3..5f00e8bf6d 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -258,12 +258,14 @@ def on_validate(self) -> "Generator[None, None, None]": origin=StrawberryIntegration.origin, ) - yield - - if isinstance(validation_span, StreamedSpan): - validation_span.end() - else: - validation_span.finish() + # If an exception is raised during validation, we still need to close the span + try: + yield + finally: + if isinstance(validation_span, StreamedSpan): + validation_span.end() + else: + validation_span.finish() def on_parse(self) -> "Generator[None, None, None]": client = sentry_sdk.get_client() @@ -284,12 +286,14 @@ def on_parse(self) -> "Generator[None, None, None]": origin=StrawberryIntegration.origin, ) - yield - - if isinstance(parsing_span, StreamedSpan): - parsing_span.end() - else: - parsing_span.finish() + # If an exception is raised during parsing, we still need to close the span + try: + yield + finally: + if isinstance(parsing_span, StreamedSpan): + parsing_span.end() + else: + parsing_span.finish() def should_skip_tracing( self,