Skip to content

opencode run waits forever on background jobs; add an option to limit or skip the wait #66885

Backlog

Description

@skorokithakis

This issue was written and posted by an LLM (Claude), with the permission of @skorokithakis.

Version: 2.3.2, opencode run --standalone --format json --auto

What happens

When the main session goes idle, opencode run lists the background jobs started during the prompt. If any remain, it prints a background_wait event and waits until all of them end. If the model starts a long-running process with background: true, such as a dev server, the job never ends. opencode run then never exits, even though the session is idle and its final reply is complete.

Reproduction

opencode run --standalone --format json --auto -- \
  "Use the shell tool with background set to true to run exactly: sleep 400. Do not wait for it. Then reply with the single word: done."

The stream ends with:

{"type":"text",...,"text":"done",...}
{"type":"step_finish",...}
{"type":"background_wait",...,"jobs":[{"id":"sh_...","kind":"shell","title":"sleep 400"}]}

The process then stays alive until the job ends. With a server, it stays alive with no limit.

Why this matters

opencode run is the non-interactive entry point for scripts, CI and wrappers. Those callers have no way to put a limit on the wait. Their only option is to parse background_wait and kill the process themselves. We had to do this in a tool that runs OpenCode turns unattended. Before that, a 2-minute turn waited about 20 minutes for our watchdog and was then reported as failed.

Request

Add a flag to opencode run that controls this wait. For example:

  • --background=wait|detach|kill: wait is the current behaviour, detach exits and leaves the jobs running, and kill stops the jobs and exits; or
  • --background-timeout <seconds>: after the limit, stop the remaining jobs and exit with the outcome of the session.

It would also help if the documentation for --format json described background_wait and its fields.

Activity

  1. opencodejr commented on Nov 27, 2026

    @opencodejr
    Contributor

    Thanks for the detailed write-up and the reproduction. You're asking for a way to bound or skip the wait that opencode run does for background jobs once the session is idle, for example --background=wait|detach|kill or --background-timeout <seconds>, and for the background_wait event to be documented under --format json. I've marked this as a feature request: the wait is how run works now, and I did not find a flag that changes it in the code I read. Maintainers will consider it.

    A possible workaround until then: send SIGTERM (or SIGINT) instead of killing the process outright. The run then cancels its session and its background jobs (shell jobs are removed, subagent sessions are interrupted) and returns. So a wrapper that sees background_wait could send SIGTERM and get a clean exit without leaving the shell running.

    Related:

  2. added
    area/cli
    type/feature
    version/v2
    on Nov 27, 2026
  3. Guembri01 commented on Nov 27, 2026

    @Guembri01

    Implemented the first shape you suggested, on branch run-background-jobs (028127e).

    --background wait|detach|kill:

    • wait stays the default, so nothing changes for existing callers.
    • detach exits with the reply, leaves the jobs running, and the default format says leaving N background jobs running instead of claiming it waits.
    • kill stops the shell and subagent jobs before exiting. Image, video and Code Mode jobs have no cancel through the API and are left to finish, which is the same set the SIGINT path already leaves.

    All three still emit background_wait with id, kind and title, so a JSON caller sees what was left behind; the two new modes end the stream right after it. The flag and the event are documented in the CLI reference next to --format json, which was the second half of your request.

    Tests in packages/cli/test/run/noninteractive.test.ts cover detach (no shell.remove, exits 0) and kill (shell.remove with the location, session.interrupt on the subagent's child session, event still emitted); the file is 25 pass, 0 fail, including every existing background test. opencode run --help lists the flag.

    I did not add --background-timeout; it is a different semantic (kill after a delay) and the three-value flag solves the CI hang without a timer. Happy to add it if maintainers prefer that shape.

    PR creation is returning 404 for this account right now, so it waits as a branch with the body written.

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/clitype/featureversion/v2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions