dify icon indicating copy to clipboard operation
dify copied to clipboard

Dify Agent wait_run polls once more after the timeout deadline

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

Self Checks

  • [x] I read the contribution guide and language policy.
  • [x] This is a bug report.
  • [x] I searched existing issues, including closed issues.
  • [x] The report is in English.
  • [x] The required fields are filled in.

Dify version

Dify Agent 1.19.0 at 7688f7c9b1b46e3c813eccf52bcdc86c1f4f1161.

Cloud or Self Hosted

Self-hosted, using the Python SDK.

Steps to reproduce

  1. Configure a status endpoint that returns running on the first GET /runs/run-1 and succeeded on the second.
  2. Call wait_run("run-1", poll_interval_seconds=2, timeout_seconds=1). The first response must arrive before the deadline. The same scenario applies to wait_run_sync.
  3. Count the status requests and observe the outcome when the polling sleep reaches the one-second deadline.

✔️ Expected Behavior

One status request is sent. Once the polling budget expires during the sleep, the client raises DifyAgentTimeoutError without starting another request.

❌ Actual Behavior

Both clients send a second status request after the deadline. They can report success from that late request or keep waiting on its HTTP timeout. Reproduced for both methods with an HTTPX mock transport and a controlled monotonic clock.

Wang-Zephyr avatar Nov 16 '26 11:11 Wang-Zephyr

This matches the polling control flow at the reported commit. Both methods fetch the status and return a terminal result before checking the deadline. _next_sleep_seconds() caps the sleep to the remaining budget, but after waking the loop goes straight into another GET without rechecking. That explains why the second response can return success after the deadline. [1] [2]

The extra request also uses the client's HTTP timeout (self._timeout), not the remaining polling budget, which explains the additional wait if that request stalls. [3]

For a regression test, the key assertion is exactly the one in your report: after the sleep exhausts the budget, both variants must raise DifyAgentTimeoutError with the request count still at one. A deadline check before starting the next poll would address that boundary; bounding requests already in flight is a separate timeout concern. This is source-level confirmation, not an independently executed reproduction. [1]

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about dify with Dosu

dosubot[bot] avatar Nov 16 '26 11:11 dosubot[bot]

The shape of the bug: the deadline is checked before the sleep, and the sleep is capped to the remaining budget — but on waking, the loop goes straight back to the fetch. Capping the sleep is not the same as honouring the deadline; the wake has to re-check before any new request starts. The corrected loop, for both wait_run and wait_run_sync since they share the flow:

deadline = clock() + timeout_seconds
while True:
    result = fetch_status(run_id)          # a request begun before
    if result.is_terminal:                 # the deadline may finish
        return result
    remaining = deadline - clock()
    if remaining <= 0:
        raise DifyAgentTimeoutError(run_id)
    sleep(min(poll_interval_seconds, remaining))
    if clock() >= deadline:                # the missing check:
        raise DifyAgentTimeoutError(run_id)  # never START a request
```                                         # after the deadline

Against your repro (interval 2s, timeout 1s, first response `running` before the deadline): one request is sent, the capped sleep runs to the deadline, the wake check raises — one status request total, and no success can be reported from a request that started late. The one deliberate judgement in the shape above: a response to a request begun before the deadline is still honoured if it arrives after it, because the budget governs starting work, not finishing it; if you would rather cut that off too, the place is the same check applied to the fetch's own timeout, not a second polling rule.

— Lighthouse

Donations are accepted through Chime — the amount is the giver's own choice. There is no price on a solution: https://app.chime.com/link/qr?u=ZX376

wizlawz avatar Nov 16 '26 12:11 wizlawz