ref(anthropic): Factor out streamed result handling #5563
1 issue
code-review: Found 1 issue (1 medium)
Medium
Span never closes if streaming iteration fails or is abandoned early - `sentry_sdk/integrations/anthropic.py:358-369`
The _wrap_synchronous_message_iterator generator function lacks a try...finally block around the iteration loop. If an exception occurs during iteration, or if the consumer breaks out of the loop early, _set_output_data(..., finish_span=True) will never be called and the span will never be closed via __exit__(). This results in unclosed spans and potential resource leaks. Other integrations like langchain.py use try...finally to ensure proper span cleanup.
Also found at:
sentry_sdk/integrations/anthropic.py:405-416
Duration: 2m 57s · Tokens: 486.2k in / 8.3k out · Cost: $0.71 (+extraction: $0.00, +merge: $0.00)
Annotations
Check warning on line 369 in sentry_sdk/integrations/anthropic.py
sentry-warden / warden: code-review
Span never closes if streaming iteration fails or is abandoned early
The `_wrap_synchronous_message_iterator` generator function lacks a `try...finally` block around the iteration loop. If an exception occurs during iteration, or if the consumer breaks out of the loop early, `_set_output_data(..., finish_span=True)` will never be called and the span will never be closed via `__exit__()`. This results in unclosed spans and potential resource leaks. Other integrations like langchain.py use `try...finally` to ensure proper span cleanup.
Check warning on line 416 in sentry_sdk/integrations/anthropic.py
sentry-warden / warden: code-review
[BY7-HCY] Span never closes if streaming iteration fails or is abandoned early (additional location)
The `_wrap_synchronous_message_iterator` generator function lacks a `try...finally` block around the iteration loop. If an exception occurs during iteration, or if the consumer breaks out of the loop early, `_set_output_data(..., finish_span=True)` will never be called and the span will never be closed via `__exit__()`. This results in unclosed spans and potential resource leaks. Other integrations like langchain.py use `try...finally` to ensure proper span cleanup.