From 2020cb676c4687b0a88b160a128e0a8a73716e18 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 2 Sep 2025 13:04:36 +0200 Subject: [PATCH] fix(openai): Avoid double exit causing an unraisable exception --- sentry_sdk/integrations/openai.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sentry_sdk/integrations/openai.py b/sentry_sdk/integrations/openai.py index 187f795807..6ea545322c 100644 --- a/sentry_sdk/integrations/openai.py +++ b/sentry_sdk/integrations/openai.py @@ -78,12 +78,12 @@ def count_tokens(self, s): return 0 -def _capture_exception(exc): - # type: (Any) -> None +def _capture_exception(exc, manual_span_cleanup=True): + # type: (Any, bool) -> None # Close an eventually open span # We need to do this by hand because we are not using the start_span context manager current_span = sentry_sdk.get_current_span() - if current_span is not None: + if manual_span_cleanup and current_span is not None: current_span.__exit__(None, None, None) event, hint = event_from_exception( @@ -516,7 +516,7 @@ def _execute_sync(f, *args, **kwargs): try: result = f(*args, **kwargs) except Exception as e: - _capture_exception(e) + _capture_exception(e, manual_span_cleanup=False) raise e from None return gen.send(result) @@ -550,7 +550,7 @@ async def _execute_async(f, *args, **kwargs): try: result = await f(*args, **kwargs) except Exception as e: - _capture_exception(e) + _capture_exception(e, manual_span_cleanup=False) raise e from None return gen.send(result)