Skip to content

util-genai: don't activate invocation when finalizing abandoned streams - #1116

Open
lmolkova wants to merge 2 commits into
open-telemetry:mainfrom
lmolkova:util-stream-del-no-activation
Open

lmolkova wants to merge 2 commits into
open-telemetry:mainfrom
lmolkova:util-stream-del-no-activation

Conversation

@lmolkova

@lmolkova lmolkova commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Abandoned stream wrappers no longer make the invocation current when __del__ finalizes them. The default _execution_context() moves from the shared base to SyncStreamWrapper and AsyncStreamWrapper, which own _self_invocation, so the getattr goes away.

The scope in __del__ came from #817, when fail() ran the completion hook in whatever context was current. Now the invocation attaches its own context around the hook, and metrics, the exception event and span end all pass that context explicitly. Attaching context from a GC finalizer adds nothing.

Finalization already records with the invocation's own context, so
__del__ no longer attaches it. The default _execution_context moves to
the concrete wrappers, which own _self_invocation, dropping the getattr.
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-11-21 13:04 UTC

Investigate required status check failures.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

@lmolkova lmolkova added the Skip Changelog PR does not require a changelog entry label Nov 20, 2026
@lmolkova
lmolkova marked this pull request as ready for review November 20, 2026 23:50
@lmolkova
lmolkova requested a review from a team as a code owner November 20, 2026 23:50
Copilot AI balanced review requested due to automatic review settings November 20, 2026 23:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The abandonment tests cannot detect invocation-span activation during finalization and omit assertions for ERROR status and wrapper-specific error.type, leaving these behaviors insufficiently tested.

2 open issues
What changed in this PR

This PR updates shared GenAI stream wrappers so garbage-collection finalization no longer activates abandoned invocations, relying instead on invocation-owned telemetry context.

Changes:

  • Removes activation from generic and tool stream destructors.
  • Moves default execution-context handling to the sync and async wrappers through a shared helper.
  • Updates abandonment tests to check finalization and context restoration.
File Description
util/​opentelemetry-util-genai/​tests/​test_stream.py Revises sync and async abandonment assertions.
util/​opentelemetry-util-genai/​src/​opentelemetry/​util/​genai/​stream.py Removes destructor activation and relocates execution-context handling.

Review effort: Balanced

…and records error

Assisted-by: Claude Opus 4.6

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the changes to stream finalization and execution-scope handling.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@AgentGymLeader AgentGymLeader left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lmolkova I abandoned instrumented OpenAI streams after one chunk and ran gc.collect() under a caller span. I covered sync and async chat create(stream=True) and sync responses.create(stream=True).

At 6ae5660 the span ended with ERROR status and error.type _OTHER as on main (43337da). The duration exemplar and both log records (details and exception) still point at it.

The only difference I saw is in _on_stream_error, which now sees the caller's span.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Skip Changelog PR does not require a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants