Skip to content

Bash tool never returns when a spawned command leaves a descendant holding its stdio pipes #50316

Open

Description

@LoikStyle

Description

The bash tool never returns for a command that starts a background service. The command's own process exits, the service comes up, and the tool call waits forever. Two independent waits keep it open:

  1. packages/core/src/cross-spawn-spawner.ts resolves the exit deferred on close. Node only emits close after the child's stdio streams have closed, and a detached descendant that inherits stdout/stderr keeps those pipes open — so for a server, close never fires. @effect/platform-node resolves the same signal on exit.

  2. packages/core/src/process.ts reads handle.all / handle.stdout / handle.stderr to end of stream. Those reads wait on the same held-open pipes, so fixing (1) alone does not unblock run — verified by reverting either half on its own: the hang comes back either way.

Steps to reproduce

node -e 'const {spawn}=require("node:child_process");const c=spawn(process.execPath,["-e","setTimeout(()=>{},60000)"],{stdio:["ignore","inherit","inherit"],detached:true});c.unref();console.log("done")'

The command prints done and exits, but the tool call does not return.

Measurements

  • Same command shape: exit fires at ~125 ms, close at ~3160 ms when the grandchild lives 3 s. When the descendant is a server, close never fires at all. That gap is the hang.
  • Stopping the output read at exit does not truncate output: outstanding bytes at exit measured 0 for payloads up to 64 MiB (a child cannot exit before it has finished writing, because it blocks on the pipe buffer until the reader drains it).
  • A regression test that spawns a detached grandchild and expects the run to settle with its output intact fails (times out) if either half above is reverted on its own.

Suggested fix

  1. Resolve the spawner's exit deferred on exit (close kept only as a fallback).
  2. Stop reading output at exit, e.g. handle.all.pipe(Stream.interruptWhen(handle.exitCode)), at all four collection sites in process.ts: combineOutput, stdout, stderr, and runStream.

Related

Open PRs in this area: #42275 and #42756 (spawner only), #44601 (both files), #45407, #49857. Per the measurements above, the spawner-only ones are not sufficient on their own.

OpenCode version

dev @ c101347

Operating System

Windows

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions