Skip to content

cli: 'opencode service set env' restarts the shared service and cancels the caller #58220

Open

Description

@dlewis7444

Summary

opencode service set env <KEY> <VALUE> hard-restarts the shared background service, and because tool calls and CLI requests execute through that service, the restart cancels the in-flight invocation that issued it. This makes the command unusable from an agent session or any long-running shell context unless it is detached.

Environment

  • opencode version: 2.1.3
  • OS: Linux 7.2.11-200.fc44.x86_64 (Fedora 44, x86_64)
  • Terminal: xterm-256color, COLORTERM=truecolor
  • Shell: /bin/bash
  • Install/channel: latest (release)
  • Active plugins: one local plugin at ~/.config/opencode/plugins/projectman.js — currently failing to load (V1→V2 export shape), so effectively none active. MCP servers configured: netbox (remote, connected), atlassian and playwright (local, failing — docker absent from host).

Reproduction

  1. Start an opencode session (or otherwise have in-flight work against the shared server).
  2. Run opencode service set env MISTRAL_API_KEY <value> from that session or a shell whose tooling rides the same server.
  3. The service restarts mid-command; the invoking tool call / CLI process is cancelled and reports "Command cancelled". The env var is not applied.
  4. Repeat: the failure is consistent (3/3 attempts cancelled). It only succeeds when the command is fully detached from the process tree, e.g. setsid sh -c 'opencode service set env …' & with output redirected, then polling opencode service get.

Expected Behavior

The environment variable is applied to the service without terminating the process that issued the command — either by applying it without a hard restart, or by deferring the restart until the CLI exits cleanly. At minimum, a hard restart should not cancel in-flight tool calls.

Actual Behavior

The shared background service restarts synchronously during service set env, killing the in-flight caller. The command exits with {"error":{"type":"unknown","message":"Command cancelled"}} and the value is not stored (opencode service get returns {}).

Additional Context

  • This was hit while moving a provider API key out of opencode.json into pass and referencing it via {env:...} — a documented, supported workflow (the models guide uses opencode service set env OPENCODE_DISABLE_FREE_MODELS 1 as the example).
  • Workaround confirmed working: run detached via setsid and poll opencode service get. I can share a one-line wrapper if useful.
  • Related observation (possibly a separate issue): under the V2 providers key, deprecated V1 field names (npm, options) are silently dropped rather than normalized or rejected, unlike the V1 provider key where they are normalized to package/settings. This silently broke two providers during a provider→providers migration with no error. Happy to file separately if preferred.
  • No secret is included here; the key involved was redacted.

Activity

  1. opencode-agent commented on Oct 22, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed report. I was able to reproduce this on 2.1.3, and it still happens on the latest dev (4a6dacec).

    Steps: start the background service, then have the service itself run opencode service set env MISTRAL_API_KEY … through its shell endpoint. That is the same way the agent's shell tool runs commands. The service stops, the set env process is killed along with it, and nothing is written: opencode service get still returns {}. The script below reproduces it in isolated config/state directories.

    The cause seems to be that service set/unset stop the service before saving the new config, so when the caller is a child of the service, the save never happens. Open PR #55957 changes that order. Related: #55065 (a value that is saved but never reaches the server) and #55967 (asking for approval before restarting while agents are running).

    Until this is fixed, running the command outside the service's process tree (from a separate terminal, or detached with setsid as you found) is the way to go.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Reproduces #58220: `opencode service set env` run from a shell spawned by the
    # shared background service (as the agent's shell tool does) is killed when the
    # command stops the service, and the env var is never saved.
    # Usage: ./repro.sh            (uses `opencode` from PATH)
    #        OC="bun ... packages/cli/src/index.ts" ./repro.sh   (from source)
    set -u
    OC=${OC:-opencode}
    BASE=$(mktemp -d /tmp/opencode/repro58220.XXXX)
    export XDG_CONFIG_HOME=$BASE/config XDG_DATA_HOME=$BASE/data XDG_STATE_HOME=$BASE/state XDG_CACHE_HOME=$BASE/cache
    mkdir -p "$BASE/proj" && cd "$BASE/proj"
    echo "== version: $($OC --version)"
    $OC service start; sleep 2; $OC service status
    
    # Ask the service itself to run the command (same path as the shell tool).
    CMD="$OC service set env MISTRAL_API_KEY secret > $BASE/out.txt 2>&1; echo exit=\$? >> $BASE/out.txt"
    BODY=$(jq -nc --arg c "$CMD" --arg d "$BASE/proj" '{command:$c, cwd:$d, location:{directory:$d}}')
    echo "== shell.create"; $OC api POST /api/shell -d "$BODY"; echo
    sleep 15
    echo "== output of the command run through the service:"; cat "$BASE/out.txt" 2>/dev/null || echo "(no output / killed)"
    echo "== opencode service get:"; $OC service get; echo
    echo "== service.json:"; find "$BASE" -name 'service*.json' -exec sh -c 'echo {}; cat {}' \;
    $OC service stop >/dev/null 2>&1

    Run it: bash repro.sh

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions