Skip to content

rtk proxy never returns after the child exits when a descendant keeps the stdout/stderr pipes open (Windows repro, same mechanism as #2320) #2829

Open

Description

@isacgalvao

Summary

When the command wrapped by rtk proxy (or by the unknown-command fallback) spawns any background/daemon process, rtk keeps running after the wrapped command has already exited. All output is printed, the direct child is gone, but rtk never returns, so the calling shell (or the coding agent driving it through the hook) waits indefinitely. Killed instances accumulate as zombie rtk processes; after one evening of agent sessions I found 27 of them alive on a single machine.

This is the same mechanism as #2320 (reported for rtk dotnet on Linux), but it is not specific to dotnet or Linux: it lives in the generic proxy path in src/main.rs and reproduces deterministically on Windows.

Environment

  • rtk 0.43.0 (winget, rtk-x86_64-pc-windows-msvc)
  • Windows 11 Pro 10.0.26200
  • The mechanism is platform-independent (fd/handle inheritance) — a unix repro is included below.

Deterministic reproduction

Windows (PowerShell):

PS> Measure-Command { rtk proxy cmd /c "start /b waitfor ZZZ /t 20 & exit /b 0" }
# rtk returns only after ~20.2s — exactly when the background waitfor dies.

PS> Measure-Command { cmd /c "start /b waitfor ZZZ /t 20 & exit /b 0" }
# ~0.1s without rtk. cmd.exe exits immediately in both cases.

Linux/macOS equivalent:

$ time rtk proxy sh -c 'echo ready; sleep 20 & exit 0'   # ~20s
$ time sh -c 'echo ready; sleep 20 & exit 0'             # ~0s

Replace the 20-second sleeper with a process that never exits (a build server, a file watcher, a background sync helper) and rtk never returns at all. That is the real-world case: the wrapped command finishes, its output is fully printed, and rtk hangs forever.

Root cause

In the proxy arm of src/main.rs (lines ~2531–2587 on current develop):

  1. The child is spawned with Stdio::piped() for stdout and stderr.
  2. Two reader threads pump the pipes until EOF.
  3. After child.wait() returns, both threads are join()ed unconditionally.

EOF on those pipes does not arrive when the child exits — it arrives when the last handle to the write end closes. Any descendant of the child inherits the write ends (handle inheritance is table-wide on Windows; fds are inherited across fork on unix). A daemon or background job left behind by the child keeps the pipes open, so the join() blocks forever even though the wrapped command finished and its exit status is already known.

The unknown-command fallback with a matching TOML filter (run_fallback → .output() on a piped child) has the same exposure, since output() also drains the pipes to EOF. The stream::run_streaming capture paths share the pattern as well — that is the #2320 variant.

Impact

  • Coding-agent setups that route shell commands through rtk (the primary use case) hang on every proxied command that leaves a daemon behind: build servers, watchers, language servers, package-manager helpers.
  • Each hang leaks one live rtk process; under concurrent agent workloads they pile up quickly and every one of them looks to the agent like a command that "never finishes".

Suggested fix

Once child.wait() has returned, the command is done and the exit code is known. Join the reader threads with a short grace period (enough to drain anything still buffered in the pipes), then detach any reader still blocked on an orphaned pipe. main() already ends with std::process::exit, which reaps detached threads. I have a PR ready for the proxy path and will link it here.

Activity

  1. pszymkowiak commented on Jul 5, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 93%

    Summary

    Generic rtk proxy/fallback path blocks forever joining stdout/stderr reader threads when a descendant process inherits and keeps the pipe write-ends open after the direct child exits; fix is to join with a grace period then detach orphaned readers (PR #2830 already open).

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/main.rs

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  2. isacgalvao commented on Jul 5, 2026

    @isacgalvao
    Author

    Fix: #2830

  3. whoisthiagosantana commented on Aug 10, 2026

    @whoisthiagosantana

    Confirming this on Linux with rtk 0.44.0, and adding one piece of information that I think raises the priority: LLM-agent hooks turn this from a niche bug into a per-command one.

    Repro (Linux, rtk 0.44.0)

    Your unix repro reproduces exactly as written:

    $ time sh -c 'echo ready; sleep 8 & exit 0'          #  0.0s
    $ time rtk proxy sh -c 'echo ready; sleep 8 & exit 0' #  8.0s
    $ time rtk proxy bash -c 'echo ready; sleep 8 & exit 0' # 8.0s
    

    rtk returns exactly when the backgrounded process dies, not when the direct child exits — so with a daemon, watcher or dev server in place of sleep, it does not return at all.

    Also worth noting for anyone landing here: v0.45.0 (2026-08-07) does not contain the fix. PR #2830 is still open against develop.

    Why this is worse than it looks: the hook amplification

    The documented way to get a command tracked without filtering it is rtk proxy. That makes it the natural building block for a PreToolUse hook that wants full measurement coverage — commands with no dedicated filter are otherwise recorded with input_tokens=0 / output_tokens=0, which is a meaningful share of history.db (23.5% of rows on my machine).

    So the obvious hook is: ask rtk hook claude for a rewrite, and when there is none, wrap the command in rtk proxy bash -c '<original>'.

    That works, and it is what I deployed. But it means every unfiltered command the agent runs now goes through the proxy path — so the exposure is no longer "users who wrap dotnet or a build server", it is "any agent command that happens to leave a background process holding stdout/stderr". The failure mode is also unusually expensive in an agent context: the tool call blocks, the model waits, and nothing in the output indicates that rtk is the reason. It looks like the command hung.

    Workaround, for whoever needs it before #2830 lands

    In the hook, refuse to wrap when the command can leave something in the background — a & that is not && and not part of >&, or nohup/setsid/disown/screen/tmux:

    (?<![&>])&(?!&)
    \b(nohup|setsid|disown|screen|tmux)\b
    

    Measured over 33,290 agent Bash commands, this drops 1.25% of the otherwise-wrapped commands back to unmeasured. That trade is very asymmetric and worth taking as-is: a false positive costs one unmeasured command, a false negative hangs the session.

    It is a heuristic, not a fix — a command that daemonizes without any of those tokens still hits the bug. The fix is #2830.

    Is there anything blocking that PR? Happy to test a build on Linux if that helps.

  4. AnshulJ999 commented on Sep 9, 2026

    @AnshulJ999

    OpenCode agents hang permanently on this - adding the OpenCode data point to the same mechanism.

    • rtk 0.48.0, Windows 11 Pro, driven by OpenCode 1.18.30's bash tool.
    • Our agent instructions route most commands through rtk, so a test matrix with a
      deliberately-left-alive background process pinned the combination:
      rtk wrapping the command (unknown-command fallback path, same as rtk proxy)
      • a descendant still alive when the direct child exits
      • OpenCode's shell tool.
        Remove any one and it completes. With all three, the agent hangs indefinitely -
        OpenCode resolves tool completion on the stdio close event rather than process
        exit, and OpenCode's 2-minute tool timeout does not settle the tool part, so real
        sessions stalled 18-390 minutes before we isolated it with a test matrix.

    Since OpenCode's shell-tool side is now fixed on their v2 branch
    (anomalyco/opencode#46085), rtk is the remaining half for the 1.x ecosystem.
    PR #2830 (grace period + detach after child exit) addresses exactly this and has
    been open since July - would a review be possible? Happy to test the branch.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clibugpriority:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions