Skip to content

cli: 'service restart' fails with 'Managed service port ... already in use' and leaves the old service running #59610

Open

Description

@Addin

Description

With the TUI open (managed service on 127.0.0.1:49374), opencode service set env OPENCODE_LOG_LEVEL debug followed by opencode service restart fails instead of restarting:

Error: Server process exited with code 1
Error: Managed service port 49374 on 127.0.0.1 is already in use by PID 7763 (/home/juanprm/.opencode/bin/opencode serve --service). Configure another port with `opencode service set port <port>` and start the service again.
    at <anonymous> (/$bunfs/root/chunk-1z3a4hp2.js:3:7501)
  [cause]: ServeError: ... Error: Failed to start server. Is port 49374 in use?
    at cli.service.restart

The old service survives, so the new env/log level never applies.

A fresh TUI spawn does apply it: the serve --service child has OPENCODE_LOG_LEVEL=debug in /proc/<pid>/environ and logs level=DEBUG; the TUI process itself does not have the variable, so the child gets it from the service config.

The port is shared across profiles: an isolated XDG_CONFIG_HOME / XDG_DATA_HOME / XDG_STATE_HOME still targets port 49374 and fails against the running PID.

Plugins

None configured: no plugin key in opencode.jsonc and no local plugin directories. The service reports plugins=14 (built-ins).

OpenCode version

2.2.0, channel latest, local=false, standalone install at ~/.opencode/bin/opencode.

Steps to reproduce

  1. Open the TUI. opencode service status -> http://127.0.0.1:49374; pgrep -af 'serve --service' -> one PID.
  2. opencode service set env OPENCODE_LOG_LEVEL debug
  3. opencode service get env -> {"OPENCODE_LOG_LEVEL":"debug"}
  4. opencode service restart -> the port-in-use error above; the old service PID survives.
  5. tr '\0' '\n' </proc/<pid>/environ | grep OPENCODE_LOG_LEVEL -> no match; no level=DEBUG lines.
  6. Close and reopen the TUI (fresh spawn): the service now has the env and logs DEBUG.

Screenshot

Not applicable; the log evidence is above.

Operating System

Pop!_OS 24.04, kernel 7.2.6-76070206-generic, x86_64.

Terminal

Ghostty (TERM_PROGRAM=ghostty, TERM=xterm-256color, COLORTERM=truecolor), shell /bin/bash.

Additional context

Activity

  1. opencode-agent commented on Oct 27, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed report. I tried this on 2.2.0 (Linux) and couldn't reproduce it:

    • After opencode service set env OPENCODE_LOG_LEVEL debug and opencode service restart, the new serve --service process had OPENCODE_LOG_LEVEL=debug in /proc/<pid>/environ. That was true with and without a TUI open, and when a fresh TUI launch started the service.
    • A freshly started service logs almost nothing at DEBUG until a project is opened. After opening one (e.g. opencode session list in a repo), level=DEBUG lines such as skill source loaded appeared in opencode.log. So no DEBUG lines right after a restart doesn't by itself mean the level was ignored.
    • --log-level isn't a v2 CLI flag (only --print-logs exists), so it not reaching the child is expected. OPENCODE_LOG_LEVEL is the way to set the level.

    One way I got exactly what you describe: another serve --service (e.g. from a different XDG_STATE_HOME, or one started by the Desktop app) was already holding the service port. service restart then failed with "Managed service port … already in use", and the process left running didn't have the env.

    Could you share:

    1. The full output of opencode service restart. Does it print a URL or an error?
    2. opencode service status, and the output of pgrep -af 'serve --service' plus each pid's parent (ps -o pid,ppid,lstart,cmd -p <pids>) before and after the restart.
    3. Whether the Desktop app is installed or running, and whether you use any other XDG_* or OPENCODE_* env overrides.

    Related: #58141 (the first launch's env applies to every profile), #42690 (the service keeps an old env after it changes), #59597.

  2. changed the title [-]cli: 'service set env' values are not passed to the managed serve --service process[/-] [+]cli: 'service restart' does not apply 'service set env' values (a fresh TUI spawn does)[/+] on Oct 27, 2026
  3. changed the title [-]cli: 'service restart' does not apply 'service set env' values (a fresh TUI spawn does)[/-] [+]cli: 'service restart' fails with 'Managed service port ... already in use' and leaves the old service running[/+] on Oct 27, 2026
  4. Addin commented on Oct 27, 2026

    @Addin
    Author

    @opencode-agent
    Your port hypothesis is right. The env plumbing is fine; the failure is the restart itself:

    Error: Server process exited with code 1
    Error: Managed service port 49374 on 127.0.0.1 is already in use by PID 7763 (/home/juanprm/.opencode/bin/opencode serve --service). Configure another port with `opencode service set port <port>` and start the service again.
    ...
    Error: Failed to start server. Is port 49374 in use?
        at cli.service.restart
    

    Requests:

    1. opencode service status -> http://127.0.0.1:49374; pgrep -af 'serve --service' -> PID 7763, parent 7197 (opencode TUI), started 13:53:43. The restart fails and 7763 survives; no second service appears.
    2. An isolated XDG_CONFIG_HOME / XDG_DATA_HOME / XDG_STATE_HOME still targets port 49374 and fails against 7763, so the port is shared across profiles.
    3. Desktop app is not installed or running; no user systemd units; no XDG_* overrides beyond the session defaults (OPENCODE_SESSION_ID / OPENCODE_TERMINAL only).

    A fresh TUI spawn does apply the env: the current service 7763 has OPENCODE_LOG_LEVEL=debug in /proc/7763/environ; the TUI (7197) does not. level=DEBUG lines appear after a project opens.

    So service restart cannot replace a running service on the same port; the surviving process keeps its old env, which is why the new level appeared not to apply. Reframed title/body accordingly. Related #42690, #58141.

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