dify icon indicating copy to clipboard operation
dify copied to clipboard

Agent transient commands ignore timeout while replaying truncated output

Open Wang-Zephyr opened this issue 2 weeks ago • 2 comments

Self Checks

  • [x] I have read the Contributing Guide and Language Policy.
  • [x] This is a bug report.
  • [x] I searched existing open and closed issues and PRs.
  • [x] I am submitting this report in English.
  • [x] I have filled the required bug-report fields.

Dify version

Agent package 1.20.0, main at 6b8e067e6690860e76a3622de1021fc3202dc54f.

Cloud or Self Hosted

Self Hosted (Source), isolated Agent runtime reproduction on Windows / Python 3.13.18.

Steps to reproduce

execute_complete_with_commands is used by complete Shell calls and Binding file operations. It checks its deadline before wait, but the truncated output branch calls read_output and continues without checking the deadline or bounding that await. A slow or stuck output replay can therefore outlive the command's total timeout.

From dify-agent, save this as repro.py and run uv run python repro.py. The scripted command adapter deliberately stalls read_output; this is a protocol-level reproduction, with no remote provider or credentials required.

import asyncio

from dify_agent.adapters.shell.protocols import ShellCommandResult, ShellCommandStatus
from dify_agent.runtime.command_runner import execute_complete_with_commands


class StalledReplay:
    async def run(self, script, **kwargs):
        return ShellCommandResult("job-1", "running", False, None, "first", 5, True)

    async def read_output(self, job_id, *, offset):
        await asyncio.Event().wait()

    async def interrupt(self, job_id, *, grace_seconds):
        return ShellCommandStatus(job_id, "interrupted", True, 130, 5)

    async def delete(self, job_id, *, force):
        print("deleted", job_id, force)


async def main():
    try:
        result = await asyncio.wait_for(
            execute_complete_with_commands(
                StalledReplay(), "command", cwd=None, env=None,
                timeout=0.01, max_output_bytes=4096, mode="stdio",
            ),
            timeout=1,
        )
    except TimeoutError:
        print("outer guard expired; command helper did not honor timeout")
    else:
        print(result.incomplete_reason, result.output_complete, result.output)


asyncio.run(main())

✔️ Expected Behavior

The helper should stop starting output reads once its deadline is exhausted, bound an in-flight replay by the remaining time, return the captured prefix with output_complete=False and incomplete_reason="timeout", and retain its interrupt/delete cleanup policy.

❌ Actual Behavior

The configured 0.01-second timeout does not finish the helper. Only the outer one-second safety guard cancels it:

deleted job-1 True
outer guard expired; command helper did not honor timeout

With the fix, the same script returns:

deleted job-1 True
timeout False first

The same issue also occurs when run returns a truncated page after consuming the full budget: the helper still starts another output read. Two deterministic clock cases and the stalled-read case fail on the unmodified main source.

Wang-Zephyr avatar Nov 19 '26 15:11 Wang-Zephyr

One cleanup distinction matters here: external cancellation reaches the finally block and force-deletes the job, but bypasses the explicit interrupt() path. Returning an internal incomplete_reason="timeout" instead interrupts a nonterminal job first, then force-deletes it. So the outer asyncio.wait_for in the reproduction is a safety guard, not an equivalent replacement for the helper’s timeout behavior. [1]

The existing local command-runner test covers cancellation during a blocked wait and asserts deletion, but its read_output stub rejects every call. Your stalled-replay case therefore exercises a separate gap in that coverage. A regression test should also distinguish a running job, which needs interruption, from an already-terminal job with unread truncated output, which the current cleanup policy deletes without interrupting. [2] [1]

This is source-level confirmation; I haven’t executed the reproduction.

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about dify

dosubot[bot] avatar Nov 19 '26 15:11 dosubot[bot]

Confirmed, nice repro script.

The issue is that execute_complete_with_commands computes the deadline against wait(), but the truncated replay loop calls adapter.read_output() unbounded without checking the remaining time budget.

Wrapping read_output with the slice of remaining time and handling asyncio.TimeoutError solves it cleanly while keeping the existing cleanup path intact:

remaining = deadline - loop.time()
if remaining <= 0:
    incomplete_reason = "timeout"
    output_complete = False
    break

try:
    chunk = await asyncio.wait_for(
        adapter.read_output(job_id, offset=offset),
        timeout=remaining,
    )
except asyncio.TimeoutError:
    incomplete_reason = "timeout"
    output_complete = False
    break

If you open a PR with this and your repro as a test case, it should be straightforward to merge.

grinem avatar Nov 19 '26 17:11 grinem