Skip to content

docstrings

13240de
Select commit
Loading
Failed to load commit list.
Merged

fix(anthropic): Patch Stream.close() and MessageStream.close() to finish spans #5674

docstrings
13240de
Select commit
Loading
Failed to load commit list.
@sentry/warden / warden: code-review completed Mar 18, 2026 in 3m 10s

2 issues

code-review: Found 2 issues (2 medium)

Medium

AsyncStream.close() and AsyncMessageStream.close() not patched, spans may not finish - `sentry_sdk/integrations/anthropic.py:108-123`

The PR patches Stream.close() and conditionally MessageStream.close() to finish spans when the connection is closed early, but does not patch the corresponding AsyncStream.close() or AsyncMessageStream.close() methods. If a user calls close() on an async stream before fully consuming the iterator, the span will not be finished properly, leaving orphaned spans. This creates inconsistent behavior between sync and async APIs.

Missing capture_internal_exceptions() around _finish_streaming_span in _wrap_close - `sentry_sdk/integrations/anthropic.py:805-813`

The _wrap_close function calls _finish_streaming_span() without wrapping it in capture_internal_exceptions(), unlike the same call in _wrap_synchronous_message_iterator() (line 455). If an exception occurs during span finishing (e.g., unexpected state, SDK bug), it would prevent the original close() method from being called, potentially leaking HTTP connections and propagating unexpected exceptions to user code.


Duration: 3m 4s · Tokens: 910.2k in / 10.4k out · Cost: $1.67 (+extraction: $0.00, +merge: $0.00, +fix_gate: $0.00)

Annotations

Check warning on line 123 in sentry_sdk/integrations/anthropic.py

See this annotation in the file changed.

@sentry-warden sentry-warden / warden: code-review

AsyncStream.close() and AsyncMessageStream.close() not patched, spans may not finish

The PR patches `Stream.close()` and conditionally `MessageStream.close()` to finish spans when the connection is closed early, but does not patch the corresponding `AsyncStream.close()` or `AsyncMessageStream.close()` methods. If a user calls `close()` on an async stream before fully consuming the iterator, the span will not be finished properly, leaving orphaned spans. This creates inconsistent behavior between sync and async APIs.

Check warning on line 813 in sentry_sdk/integrations/anthropic.py

See this annotation in the file changed.

@sentry-warden sentry-warden / warden: code-review

Missing capture_internal_exceptions() around _finish_streaming_span in _wrap_close

The `_wrap_close` function calls `_finish_streaming_span()` without wrapping it in `capture_internal_exceptions()`, unlike the same call in `_wrap_synchronous_message_iterator()` (line 455). If an exception occurs during span finishing (e.g., unexpected state, SDK bug), it would prevent the original `close()` method from being called, potentially leaking HTTP connections and propagating unexpected exceptions to user code.