Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2944,6 +2944,7 @@ async def _generate_updates() -> AsyncIterable[AgentResponseUpdate]:
return ResponseStream(
_generate_updates(),
finalizer=AgentResponse.from_updates,
cleanup_hooks=[workflow_stream.close],

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.

If close() races an active anext(), the outer generator's aclose() raises, but ResponseStream.close() still runs this hook; the inner close then releases _is_running even though the original workflow remains active. A subsequent run is accepted concurrently, violating the single-run invariant and risking duplicate side effects or checkpoint state corruption. Please coordinate close with in-flight pulls, or otherwise retain the guard until the inner workflow has actually terminated, and cover this race with a regression test.

)

def _request_info_to_approval_request(self, event: WorkflowEvent[Any]) -> Any:
Expand Down
65 changes: 65 additions & 0 deletions python/packages/core/tests/workflow/test_functional_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2561,6 +2561,71 @@ async def wf(x: int) -> str:
response = await stream.get_final_response()
assert len(response.messages) >= 1

@pytest.mark.parametrize("consume", ["none", "first", "all"])
async def test_as_agent_stream_close_releases_workflow(self, consume: str):
@built_workflow
async def wf(x: int) -> str:
return f"result: {x}"

agent = wf.as_agent()
stream = agent.run(1, stream=True)
if consume == "first":
assert (await anext(stream)).text == "result: 1"
elif consume == "all":
assert (await stream.get_final_response()).text == "result: 1"

await stream.close()
assert (await agent.run(2)).text == "result: 2"

# Closing an old stream again must not release a newer run's guard.
next_stream = agent.run(3, stream=True)
try:
await stream.close()
with pytest.raises(RuntimeError, match="already running"):
await agent.run(4)
assert (await next_stream.get_final_response()).text == "result: 3"
finally:
await next_stream.close()

@pytest.mark.parametrize("cancel", [False, True])
async def test_as_agent_stream_failure_releases_workflow(self, cancel: bool):
started = asyncio.Event()
stopped = asyncio.Event()

@built_workflow
async def wf(x: int) -> str:
if x == 1:
try:
started.set()
if cancel:
await asyncio.Event().wait()
raise ValueError("workflow failure")
finally:
stopped.set()
return f"result: {x}"

agent = wf.as_agent()
stream = agent.run(1, stream=True)
pull = asyncio.create_task(anext(stream))
try:
await asyncio.wait_for(started.wait(), timeout=5)
if cancel:
pull.cancel()
with pytest.raises(asyncio.CancelledError):
await pull
else:
with pytest.raises(ValueError, match="workflow failure"):
await pull
assert stopped.is_set()
await stream.close()
assert (await agent.run(2)).text == "result: 2"
finally:
if not pull.done():
pull.cancel()
with suppress(asyncio.CancelledError):
await pull
await stream.close()

async def test_as_agent_has_id_and_description(self):
@built_workflow(description="A test workflow")
async def wf(x: int) -> int:
Expand Down
Loading