util-genai: end streamed invocations abandoned before draining - #492
Draft
sidsri14 wants to merge 2 commits into
Draft
util-genai: end streamed invocations abandoned before draining#492sidsri14 wants to merge 2 commits into
sidsri14 wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_tokenguard 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_invocationconvention; 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.