Skip to content

util-genai: end streamed invocations abandoned before draining - #492

Draft
sidsri14 wants to merge 2 commits into
open-telemetry:mainfrom
sidsri14:fix/stream-abandoned-invocation-finalize
Draft

util-genai: end streamed invocations abandoned before draining#492
sidsri14 wants to merge 2 commits into
open-telemetry:mainfrom
sidsri14:fix/stream-abandoned-invocation-finalize

Conversation

@sidsri14

@sidsri14 sidsri14 commented Aug 30, 2026

Copy link
Copy Markdown

Fixes #491: a streamed invocation whose wrapper the caller abandoned before draining leaked both the span and the OTel context, so every abandoned stream left a dangling span and a permanently-attached context token.

SyncStreamWrapper now ends the invocation on collection via the GC finalizer, using the same detach/stop path as normal finalization. The _context_token guard makes finalization idempotent, so drained/closed/failed streams are untouched.

Known gaps: sync only (async abandonment deferred until the async lease problem is settled); covers wrappers following the _self_invocation convention; span end time reflects collection, not drain; a cross-thread collection may swallow the context detach. This is a draft so the maintainers can steer the design for the explicitly "needs investigation" path.

A streamed invocation whose wrapper the caller walked away from before
draining stayed open forever: the span was never ended and the OTel
context token never detached, leaking both. Consumers that abandon a
stream after reading a few chunks (e.g. agent tools that take the first
completion and stop) left leaks behind on every call.

SyncStreamWrapper now ends the invocation when the wrapper is collected
without having been drained, closed, or failed. The GC finalizer is the
only signal that the caller left. Detach uses the same detach/stop path
as normal finalization, so a drained, closed, or failed stream is
untouched and nothing is finalized twice. The span end time reflects
collection rather than drain.

Sync only; async abandonment is deferred until the async lease problem
is settled.

Signed-off-by: Siddharth Srivastava <128143077+sidsri14@users.noreply.github.com>
Assisted-by: Claude Opus 5
Signed-off-by: Siddharth Srivastava <128143077+sidsri14@users.noreply.github.com>
Assisted-by: Claude Opus 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

util-genai: streamed invocations leak the span and the OTel context when the caller walks away

1 participant