Skip to content

fix(llm): avoid double execution in schedule_stream_flow#363

Merged
imbajin merged 2 commits into
apache:mainfrom
Muawiya-contact:fix/schedule-stream-flow-double-run
Jun 21, 2026
Merged

fix(llm): avoid double execution in schedule_stream_flow#363
imbajin merged 2 commits into
apache:mainfrom
Muawiya-contact:fix/schedule-stream-flow-double-run

Conversation

@Muawiya-contact

Copy link
Copy Markdown
Contributor

Summary

Fixes a control-flow fall-through in Scheduler.schedule_stream_flow that caused
the flow to be built, run, and streamed twice when no reusable pipeline
exists (manager.fetch() returns None).

Closes #360.

Root cause

The if pipeline is None: branch builds a fresh pipeline, runs it, streams the
result via post_deal_stream, and caches it with manager.add(pipeline) — but
it was missing a return at the end. Execution therefore fell straight through
into the reuse try block below, where the just-built (now non-None) pipeline
was prepared and run a second time.

Impact: duplicate pipeline execution, extra LLM calls, and doubled streaming
output on the first call for a given flow.

The synchronous counterpart schedule_flow does not have this bug because its
if pipeline is None: branch ends with an explicit return res.

Fix

Add a bare return after manager.add(pipeline) in the if pipeline is None:
branch, so the build/run/stream path terminates instead of falling through.

A bare return is used (not return res) because schedule_stream_flow is an
async generator, where return <value> is not allowed.

Tests

Adds hugegraph-llm/src/tests/flows/test_scheduler.py (Layer A / unit marker,
no Docker, network, or real LLM):

  • test_stream_flow_runs_once_when_no_reusable_pipeline — forces
    manager.fetch() to return None and asserts the flow is built, initialized,
    run, and streamed exactly once, the stream output is not duplicated,
    manager.add is called once, and the reuse/release path (prepare /
    release) is never entered. This test fails on the previous code and passes
    with the fix.
  • test_stream_flow_rejects_unknown_flow_name — covers the ValueError guard
    for unsupported flow names.

The Scheduler is instantiated via __new__ with a single mocked pool entry to
avoid eagerly constructing every real flow/manager in __init__.

How to verify locally

uv sync --extra llm --extra dev
uv run pytest hugegraph-llm/src/tests/flows/test_scheduler.py -v
uv run ruff format --check .
uv run ruff check .

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working labels Jun 20, 2026
@github-actions github-actions Bot added the llm label Jun 20, 2026
@Muawiya-contact

Copy link
Copy Markdown
Contributor Author

@imbajin Could you please review that one?

@imbajin imbajin 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.

Blocking: no. Summary: No obvious issues found in the current head. Evidence: GitHub checks passed; git diff --check refs/remotes/apache/main...HEAD; uv run --extra llm --extra dev pytest hugegraph-llm/src/tests/flows/test_scheduler.py -q; ruff format/check on touched files.

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 21, 2026
@imbajin imbajin changed the title fix(scheduler): avoid double execution in schedule_stream_flow (#360) fix(llm): avoid double execution in schedule_stream_flow Jun 21, 2026
@imbajin imbajin merged commit 3ca5701 into apache:main Jun 21, 2026
18 checks passed
@Muawiya-contact Muawiya-contact deleted the fix/schedule-stream-flow-double-run branch June 21, 2026 13:56
haohao0103 pushed a commit to haohao0103/incubator-hugegraph-ai that referenced this pull request Jun 30, 2026
## Summary

Fixes a control-flow fall-through in `Scheduler.schedule_stream_flow`
that caused
the flow to be built, run, and streamed **twice** when no reusable
pipeline
exists (`manager.fetch()` returns `None`).

Closes apache#360
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer llm size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] schedule_stream_flow runs the flow twice when pipeline is None

2 participants