Repository navigation
Python: preserve workflow run guard during concurrent stream close - #10261
袁永旗(Yuan Yongqi) (Yongthyuan) wants to merge 1 commit into
Conversation
|
/review |
There was a problem hiding this comment.
MAF Automated Review — Iteration 1
Result: Findings reported
Scope: full PR (1 commit(s)): d53a2ce15bf0
Model: gpt-5.6-sol
Overview
The PR moves run-guard ownership into a per-run async generator and adds idempotent release plus direct and agent-adapter race coverage. Those guards prevent a failed concurrent close or stale stream from unlocking a newer run. One lifecycle path remains: after a raced close, normal finalization can return a result without resuming or closing the guarded generator, leaving that workflow instance locked until another explicit close.
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 medium) across 1 file. Details are attached to the affected lines below.
Affected areas: python/packages/core/agent_framework/_workflows/_functional.py
|
|
||
| async def cleanup() -> None: | ||
| # An active pull rejects aclose(); retain its guard until it settles. | ||
| await guarded_stream.aclose() |
There was a problem hiding this comment.
After this close races an active pull, ResponseStream.close() marks the stream consumed and consumes this one-shot cleanup hook. Once the pull yields, get_final_response() then finalizes the partial updates without draining or closing guarded_stream, so it returns successfully while the workflow remains locked and every later run() is rejected; this occurs through both direct and as_agent() streams. Please keep failed close recoverable through normal finalization so get_final_response() also releases the guard.
Motivation & Context
Calling
close()while a functional workflow stream has an active pull raisesRuntimeError: aclose(): asynchronous generator is already running. However, the cleanup hook still releases the workflow's run guard. If the caller catches the close error, anotherrun()is accepted while the original workflow is still executing.This affects both direct workflow streams and the
as_agent()adapter. It is separate from the early-close cleanup fixed in #10164: that issue prevented a subsequent run, while this race permits overlapping runs. The existing cancellation tests cancel and await the pull before closing, so they do not cover this ordering.Description & Review Guide
ResponseStreamcleanup-on-error behavior is preserved.Related Issue
Fixes #10262.
Follow-up to #10164. Addresses the concurrent-close finding on #10220.
The original early-close fix was merged in #10204, and #10220 was closed as superseded. This PR starts from current main and contains the additional concurrency fix and its regression tests. No competing open PR for this race was found before submission.
AI Assistance
AI assistance details: reproduction, implementation, regression tests, local validation, and this description.
Contribution Checklist
All 10 new cases fail against the unpatched implementation and pass with the fix. Independent public-API probes reproduce overlapping runs before the fix and retain a maximum of one active execution afterward. The original #10164 minimal reproduction still returns
result: 2.Pinned Ruff lint/format, strict source Pyright, and test Pyright, mypy, Pyrefly, ty, and zuban checks pass for the changed files. Existing experimental-feature warnings and three Pyrefly warnings in unchanged tests remain. External provider integration suites were not run.