ref(anthropic): Factor out streamed result handling #5563
2 issues
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
Span never closed if synchronous streaming iterator is interrupted - `sentry_sdk/integrations/anthropic.py:358-369`
The _wrap_synchronous_message_iterator function lacks try/finally handling around the iteration loop. If an exception is raised during iteration, or if the consumer breaks out of the loop early (e.g., for event in stream: break), or if the generator is explicitly closed, the code after the for loop (lines 371-389) will never execute. This leaves the span unclosed, causing resource leaks and incomplete traces. Other integrations like google_genai and openai_agents use try/finally patterns to ensure span cleanup.
Also found at:
sentry_sdk/integrations/anthropic.py:405-416
4 skills analyzed
| Skill | Findings | Duration | Cost |
|---|---|---|---|
| code-review | 1 | 2m 57s | $0.71 |
| find-bugs | 1 | 4m 3s | $1.04 |
| skill-scanner | 0 | 59.3s | $0.23 |
| security-review | 0 | 43.2s | $0.26 |
Duration: 8m 42s · Tokens: 1.5M in / 21.4k out · Cost: $2.25 (+extraction: $0.00, +merge: $0.00, +dedup: $0.01)