Skip to content

Commit c356be8

Browse files
committed
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
1 parent cca3d6f commit c356be8

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

sentry_sdk/integrations/strawberry.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,14 @@ def on_validate(self) -> "Generator[None, None, None]":
258258
origin=StrawberryIntegration.origin,
259259
)
260260

261-
yield
262-
263-
if isinstance(validation_span, StreamedSpan):
264-
validation_span.end()
265-
else:
266-
validation_span.finish()
261+
# If an exception is raised during validation, we still need to close the span
262+
try:
263+
yield
264+
finally:
265+
if isinstance(validation_span, StreamedSpan):
266+
validation_span.end()
267+
else:
268+
validation_span.finish()
267269

268270
def on_parse(self) -> "Generator[None, None, None]":
269271
client = sentry_sdk.get_client()
@@ -284,12 +286,14 @@ def on_parse(self) -> "Generator[None, None, None]":
284286
origin=StrawberryIntegration.origin,
285287
)
286288

287-
yield
288-
289-
if isinstance(parsing_span, StreamedSpan):
290-
parsing_span.end()
291-
else:
292-
parsing_span.finish()
289+
# If an exception is raised during parsing, we still need to close the span
290+
try:
291+
yield
292+
finally:
293+
if isinstance(parsing_span, StreamedSpan):
294+
parsing_span.end()
295+
else:
296+
parsing_span.finish()
293297

294298
def should_skip_tracing(
295299
self,

0 commit comments

Comments
 (0)