Skip to content

Background shells die partway under OpenCode 2.3: the process daemon kills the supervisor's process group #191

Closed

Description

@nkoynov

What happens

Under OpenCode 2.3.0, a Cursor background shell (background_shell_spawn_args, or a soft-background shell_stream_args after its foreground window) is stopped partway through. The terminal record stays status: running with no footer, and the provider posts "Background shell N ended without reporting an exit code; its supervisor stopped before the command finished". How long the job lives depends on how many other shell commands run in that OpenCode instance: 3 to 68 minutes on a busy machine. On one host it hit 83 of 173 background shells in a day on 2.3.0, and 0 of 109 the day before on 2.2.x.

Why

OpenCode 2.3.0 runs Unix shell-tool commands through its process daemon ("Shell commands now survive server handoffs"). The daemon:

  • spawns each command with process_group(0) (packages/process-daemon/src/service/pipe/unix.rs, the Command builder),
  • keeps the exited leader unreaped so its PGID stays reserved,
  • keeps the last EXITED_LIMIT = 25 exited commands per shell service (packages/core/src/shell.ts), and when it forgets the oldest, Shell.remove → pty.terminatePipe → daemon Process::kill → killpg(pid, SIGKILL). The comment there: "This also kills what is left of its group, even after a successful exit, which a spawned command would leave running".

createManagedTerminal (src/terminal-runner.ts) starts the supervisor with

nohup "$cursor_runner" "$cursor_job/runner.sh" >"$cursor_job/launcher.log" 2>&1 </dev/null &

so runner.sh (and its updater and watchdog subshells) stay in the launcher's process group. 25 shell exits later the daemon SIGKILLs that group. The supervisor dies without finalizing, group.sh's lifeline watcher sees EOF and stops the payload (command-exit 143), and the provider reports the lost supervisor. A live probe showed the supervisor's PGID equal to the launcher, which was [sh] <defunct> with parent opencode-pty daemon.

Repro without OpenCode

# launcher as a group leader, as the daemon runs it
setsid sh -c "$LAUNCHER"     # a backgroundSpawn launcher for: sleep 5; echo done
kill -KILL -<launcher pid>   # what the daemon does once 25 later commands have exited
# 7fa8f95 / upstream 2ea6994: the record stays "running"; the job is gone

Fix we use (fork branch fix/supervisor-own-session, nkoynov/cursor-opencode-provider@7fa8f95...fix/supervisor-own-session)

  • Start the runner in its own session where setsid exists (nohup setsid "$cursor_runner" …; setsid doesn't fork for a non-leader child, so $! stays the supervisor). Without setsid (macOS) nothing changes.
  • Keep cancellation: the launcher holds the only write end of a handoff FIFO and writes detached on every normal exit (spawn handle, window exit, timeout, handoff). A watcher in the supervisor that reads EOF without detached (Stop, tool timeout, any kill of the launcher) TERMs the supervisor, which stops the command and finalizes the record as aborted / 143, also when the launch is cancelled before ready.
  • The unmanaged buildBackgroundShellCommand fallback also uses setsid; the unmanaged soft-background fallback still shares the launcher's group.

Tests: launcher as group leader → group SIGKILL after exit (spawn and soft handoff, sh/bash/zsh) keeps the job running to its real exit code; group SIGKILL, group TERM and launcher-only TERM during the foreground window, and before ready, give an aborted record with no orphans. Checked on a VM with OpenCode 2.3.0: a 400 s background job outlived 30 later foreground shells and completed with exit 0 (it died on the old build); Stop during a soft-background window still aborts the command.

Happy to open a PR against main if you want it.

Activity

  1. nkoynov commented on Nov 17, 2026

    @nkoynov
    ContributorAuthor

    OpenCode fixed the delayed kill on their side: anomalyco/opencode#64266 (opencode-pty 0.6.0) and #64251 (core pins it, merged to dev today, so it ships after 2.3.0). The daemon now reaps a command when it exits; if the command succeeded, whatever it left behind keeps running, and if it failed, the group gets SIGTERM at exit and anything still alive is killed 3 s later. So the supervisor won't die partway any more once that release is out, but 2.3.0 users still hit this, and starting the supervisor in its own session still keeps it out of the failed-command case.

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