Python: Finalize abandoned functional workflow streams without ContextVar leaks - #7798
Open
Shivani . (Shivani767) wants to merge 2 commits into
Open
Conversation
…tVar leaks Stop holding the run span and event-origin token across yields so breaking out of a streaming functional workflow no longer fails during GC.
Shivani . (Shivani767)
deployed
to
github-app-auth
August 20, 2026 18:10 — with
GitHub Actions
Active
Shivani . (Shivani767)
deployed
to
github-app-auth
August 20, 2026 18:10 — with
GitHub Actions
Active
Shivani . (Shivani767)
deployed
to
github-app-auth
August 20, 2026 18:10 — with
GitHub Actions
Active
Shivani . (Shivani767)
deployed
to
github-app-auth
August 20, 2026 18:16 — with
GitHub Actions
Active
Shivani . (Shivani767)
deployed
to
github-app-auth
August 20, 2026 18:17 — with
GitHub Actions
Active
Contributor
There was a problem hiding this comment.
Pull request overview
Prevents ContextVar leaks when functional workflow streams are abandoned.
Changes:
- Uses unattached workflow spans with scoped activation.
- Builds framework events before yielding.
- Adds abandonment and span behavior tests.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
_functional.py |
Revises functional workflow tracing and event emission. |
_events.py |
Adds safe framework-event construction. |
observability.py |
Adds unattached workflow span creation. |
test_functional_workflow.py |
Adds stream-abandonment regression tests. |
test_observability.py |
Tests unattached span behavior. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Reset the run guard in the generator finally so a follow-up run after GC is allowed, and assert no OpenTelemetry detach errors plus nested-span parenting under workflow.run.
Shivani . (Shivani767)
deployed
to
github-app-auth
August 20, 2026 18:32 — with
GitHub Actions
Active
5 tasks
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.
Motivation & Context
Abandoning a functional-workflow
ResponseStreammid-iteration (async for event in stream: break) is a normal HITL pattern, but the run generator held both the OpenTelemetry run span and_framework_event_origin()open acrossyield. Garbage-collecting the generator later injectsGeneratorExitfrom a differentContext, soContextVar.resetraises (Failed to detach context,async generator ignored GeneratorExit).Description & Review Guide
_run_corenow starts the run span unattached (start_workflow_span) and activates it only around_execute. Framework lifecycle events are built inside_framework_event()so the origin token is reset before each yield._framework_event_origin()also suppresses cross-contextresetfailures as a last line of defense.originremainsFRAMEWORK. Nested processing spans still parent under the run span during_execute. GraphWorkflow._run_coreis unchanged (same class of OTel attach-across-yield; can be a follow-up)._activate_spanaround_executeis enough for nested-span parenting without reintroducing the leak.Related Issue
Fixes #7787
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.