feat(cohere): Add span streaming support #6479
2 issues
find-bugs: Found 2 issues (2 low)
Low
StreamedSpan may leak on scope if `_capture_exception` raises before `span.__exit__` in shared internal-exceptions block - `sentry_sdk/integrations/cohere.py:171-174`
In the streaming branch of _wrap_chat.new_chat, when is_span_streaming_enabled=True, a StreamedSpan immediately registers itself as _scope.streamed_span during __init__ (via _start()). On the error path, _capture_exception(e) and span.__exit__(*exc_info) share a single with capture_internal_exceptions(): block. If _capture_exception raises (e.g. failure inside event_from_exception or capture_event), the exception is swallowed by capture_internal_exceptions and span.__exit__ is skipped. The span is never ended and _scope.streamed_span is not restored to its previous value, so any subsequent streamed span started on the same scope is incorrectly parented under the leaked chat span until the scope is torn down. Splitting the two calls into independent with capture_internal_exceptions(): blocks ensures cleanup always runs.
StreamedSpan leaks if streaming generator is never fully consumed
When is_span_streaming_enabled=True and chat_stream is used, _end_span(span) is only reachable at the end of the new_iterator() generator; if the caller breaks out of iteration early, the StreamedSpan is never ended and remains the active span on the scope indefinitely, causing all subsequent child spans in that scope to be incorrectly parented.
⏱ 24m 22s · 3.2M in / 161.4k out · $4.67