Skip to content

fix(delegation): timeout stuck async child runners (rebased from #60234)#71096

Open
DavidMetcalfe wants to merge 1 commit into
NousResearch:mainfrom
DavidMetcalfe:fix/async-delegation-watchdog-60203
Open

fix(delegation): timeout stuck async child runners (rebased from #60234)#71096
DavidMetcalfe wants to merge 1 commit into
NousResearch:mainfrom
DavidMetcalfe:fix/async-delegation-watchdog-60203

Conversation

@DavidMetcalfe

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes a stuck lifecycle path for delegate_task(background=true): if the detached async delegation runner wedges before it returns and enqueues a completion event, gateway sessions can keep seeing only the original dispatched handle while the async slot remains occupied.

This PR makes the async delegation registry enforce the existing delegation.child_timeout_seconds setting for detached runners. When a still-running async delegation exceeds that timeout, the registry now emits one terminal timeout completion event, releases the async slot, drops the interrupt closure, and best-effort interrupts the detached child. Late worker returns are ignored so the parent cannot receive duplicate completion events.

This is a rebase of @izumi0uu's PR #60234 onto current main (commit 5a3e153 cherry-picked with authorship preserved). The original PR passed all CI checks and cross-vendor review (Gemini 3.6 Flash + GPT-OSS) but had merge conflicts with upstream changes. This rebase resolves those conflicts and preserves the full fix.

Related Issue

Fixes #60203

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • Tests (adding or improving test coverage)

Changes Made

  • tools/async_delegation.py: add per-dispatch timeout watchdogs for single and batch async delegations.
  • tools/async_delegation.py: refactor _finalize into _begin_finalization + _push_completion_event + _finish_finalization for atomic running→finalizing transitions that both worker and watchdog paths share.
  • tools/async_delegation.py: make normal finalizers no-op after timeout/interruption so a late runner cannot enqueue a second completion event.
  • tools/delegate_tool.py: pass the configured child timeout into the background async registry path.
  • tests/tools/test_async_delegation.py: cover stuck single runners, stuck batch runners, timeout durability through persistence block, process restart recovery, and the delegate-tool timeout handoff.

How to Test

.venv/bin/python -m pytest tests/tools/test_async_delegation.py tests/tools/test_delegate_subagent_timeout_diagnostic.py tests/tools/test_delegate.py -q

Merge conflicts resolved

  • tools/async_delegation.py: dispatch_async_delegation_batch gained delegation_id param upstream; kept both delegation_id and timeout_seconds.
  • tools/delegate_tool.py: upstream added delegation_id=live_deleg_id kwarg; kept both kwargs.

Review

  • Original PR fix(delegation): timeout stuck async child runners #60234 by @izumi0uu
  • Cross-vendor review (Gemini 3.6 Flash Medium + GPT-OSS 120B Medium): both reviewers confirmed the core logic is correct. Flash rated all 7 review questions ACCEPTABLE. GPT-OSS rated 6/7 ACCEPTABLE/NIT with one SHOULD-FIX about resource cleanup — however the concern is about pre-existing interrupt behavior in delegate_tool._run_single_child, not introduced by this PR.
  • All 26 CI checks pass on the original PR; 197 targeted tests pass locally after rebase.

Notes

Open question: The reporter's signature (multi-day gateway uptime, all-provider hang at first API call) differs from issue #61778 (mid-session Codex-specific 0-event hang). This PR addresses the delegation lifecycle gap (child_timeout_seconds not enforced for background runners) which covers the #60203 symptom regardless of which specific provider/event-loop degradation caused the runner to wedge. The root cause of the per-thread event-loop degradation in long-lived daemon threads remains uninvestigated and may warrant a separate diagnostic issue.

Async background delegation can leave gateway sessions holding only a dispatched handle when the detached runner wedges before it can return and enqueue a completion. Enforce the configured child timeout in the async registry so the parent observes a terminal timeout event and the async slot is released.

Constraint: Issue NousResearch#60203 reports long-lived gateway processes with background child delegates that never produce completion events despite child_timeout_seconds being configured.

Rejected: Relying only on _run_single_child timeout handling | it cannot finalize the async registry when the outer runner thread itself never reaches normal completion.

Confidence: high

Scope-risk: narrow

Directive: Keep background delegation completion owned by the async registry whenever detached workers can outlive the caller's immediate control.

Tested: .venv/bin/python -m pytest tests/tools/test_async_delegation.py tests/tools/test_delegate_subagent_timeout_diagnostic.py tests/tools/test_delegate.py -q

Tested: .venv/bin/python -m ruff check tools/async_delegation.py tools/delegate_tool.py tests/tools/test_async_delegation.py

Tested: git diff --check

Not-tested: Multi-day real gateway degradation; covered with deterministic stuck-runner registry tests.
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists tool/delegate Subagent delegation duplicate This issue or pull request already exists sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state labels Jul 25, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Duplicate of #60234. The rebased diff retains the same async-runner timeout watchdog, slot-release, and late-completion suppression mechanism; #60234 remains the open canonical PR.

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

Labels

duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state tool/delegate Subagent delegation type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: delegate_task children hang at first API call after days of gateway uptime; process restart restores

3 participants