Skip to content

Starting background server... Error: Background service failed to start #57032

Open

Description

@chrissound

Description

[chris@worksoul:/extra/git-repos-checked-out/temp/chriscopy/acp-http-proxy]$ opencode
Starting background server...
Error: Server process exited with code 1
Error: Managed service port 13420 on 127.0.0.1 is already in use by another process. Configure another port with opencode service set port <port> and start the service again.
at (/$bunfs/root/chunk-s23bgv8n.js:2:8825) {
[cause]: ServeError:
at N (/$bunfs/root/chunk-7vhz0ycc.js:52:7706)
at applyHandlers (node:events:64:20)
at emitError (node:events:44:27)
at emitListenErrorNextTick (node:_http_server:134:12)
at processTicksAndRejections (unknown:7:39) {
[cause]: Error: Failed to start server. Is port 13420 in use?
at serve (unknown)
at (node:_http_server:359:39)
at (node:_http_server:332:33)
at (/$bunfs/root/chunk-7vhz0ycc.js:52:7744)
}
}
at map (unknown:1:11)
at reap (/$bunfs/root/chunk-j3zn1342.js:2:1203)
at (/$bunfs/root/chunk-j3zn1342.js:3:6711)
at service.ensure (/$bunfs/root/chunk-1p8x226j.js:3:690)
at service.ensure (definition) (/$bunfs/root/chunk-j3zn1342.js:3:5523)
at cli.server-connection.resolve (/$bunfs/root/chunk-a3ha88z8.js:96:6563)
at cli.server-connection.resolve (definition) (/$bunfs/root/chunk-1p8x226j.js:2:402)

[chris@worksoul:/extra/git-repos-checked-out/temp/chriscopy/acp-http-proxy]$ pkill opencode

[chris@worksoul:/extra/git-repos-checked-out/temp/chriscopy/acp-http-proxy]$ opencode
Starting background server...
Error: Background service failed to start
at z (/$bunfs/root/chunk-j3zn1342.js:3:3708)
at (/$bunfs/root/chunk-j3zn1342.js:3:6332)
at service.ensure (/$bunfs/root/chunk-1p8x226j.js:3:690)
at service.ensure (definition) (/$bunfs/root/chunk-j3zn1342.js:3:5523)
at cli.server-connection.resolve (/$bunfs/root/chunk-a3ha88z8.js:96:6563)
at cli.server-connection.resolve (definition) (/$bunfs/root/chunk-1p8x226j.js:2:402)

Plugins

No response

OpenCode version

opencode v2.0.27+1f4d07a

Steps to reproduce

No response

Screenshot

No response

Operating System

nixos

Terminal

No response

Activity

  1. opencode-agent commented on Oct 19, 2026

    @opencode-agent
    Contributor

    Thanks for the report! The second error, Background service failed to start, is a generic message. It means the background service started but then reported that its own startup failed, and the real cause isn't in this output. Could you share:

    1. The service log: the most recent entries from ~/.local/share/opencode/log/opencode.log, from around the failed launch. Please remove anything sensitive first.
    2. The output of running the service in the foreground, which usually shows the underlying error directly:
      opencode service stop
      opencode serve --service --print-logs --log-level debug
    3. What opencode service get prints, and whether you set port 13420 yourself. If you didn't, port 13420 isn't the usual default (49374), which suggests your build uses a non-standard release channel.
    4. How you installed OpenCode on NixOS: nixpkgs, a flake or an overlay, and which package or version.
    5. If it happens again, what is holding the port before you run pkill, e.g. ss -ltnp | grep 13420.

    You can reply here with the details, or edit the issue. If you edit it, please also leave a comment saying you've updated it, so we know you're done.

    These existing issues look related, ordered from most to least likely to match. Please check whether one of them already covers your problem:

  2. chrissound commented on Oct 19, 2026

    @chrissound
    Author

    [chris@worksoul:/extra/git-repos-checked-out/temp/chriscopy/acp-http-proxy]$ opencode service stop

    [chris@worksoul:/extra/git-repos-checked-out/temp/chriscopy/acp-http-proxy]$ opencode serve --service --print-logs --log-level debug
    timestamp=2026-10-19T09:25:32.559Z level=ERROR run=578faff3 message="background service boot failed" cause="Cause([Die(Error: EPERM: operation not permitted, chmod '/home/chris/.local/share/opencode/opencode.db')])" role=server

  3. opencode-agent commented on Oct 19, 2026

    @opencode-agent
    Contributor

    Thanks for the log, that showed exactly where it fails. I reproduced it on 2.0.27, and it still happens on the latest dev (ba06ff9).

    Since 2.0.27 (from #52150), the background service tightens opencode.db to mode 0600 with chmod before opening it. If the database file belongs to a different user than the one running opencode, chmod fails with EPERM even when you can still read and write the file. A common cause is an earlier sudo opencode run. The service then stops during startup, and the CLI only shows Background service failed to start. The failed service process also stays running and holds the service port, which is probably why you first saw "port 13420 is already in use".

    What I did (the script below automates it):

    1. As a normal user, start opencode serve --service once, so it creates ~/.local/share/opencode/opencode.db.
    2. chown root:root the database and give it mode 666. You can still read and write it, but you no longer own it.
    3. opencode serve --service --print-logs now fails with EPERM: operation not permitted, chmod '.../opencode.db', and plain opencode prints Error: Background service failed to start.

    Until this is fixed, this should get you running again:

    ls -ln ~/.local/share/opencode/opencode.db*        # check the owner
    pkill -f 'opencode serve --service'                  # stop the stuck service
    sudo chown "$USER": ~/.local/share/opencode/opencode.db*

    If the files are on a filesystem that doesn't allow chmod at all, changing the owner won't help. In that case, please say so here.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Repro for anomalyco/opencode#57032: the v2 background service fails to boot with
    # EPERM from chmod when opencode.db is writable by the user but owned by another user.
    # Run as root on Linux. Needs OPENCODE_BIN (e.g. the `opencode` binary of @opencode/[email protected])
    # copied somewhere the test user can execute.
    set -u
    BIN=${OPENCODE_BIN:-/usr/local/bin/oc227}
    id tester >/dev/null 2>&1 || useradd -m tester
    D=/home/tester/.local/share/opencode
    mkdir -p /tmp/repro-proj && chmod 777 /tmp/repro-proj && cd /tmp/repro-proj
    
    # 1. Normal first start as the user creates opencode.db (works)
    runuser -u tester -- env HOME=/home/tester timeout 15 "$BIN" serve --service --print-logs 2>&1 | grep -E "listening|ERROR"
    pkill -u tester; sleep 1
    
    # 2. Database still readable/writable by the user, but owned by root (e.g. after a `sudo opencode` run)
    rm -f "$D"/opencode.db-wal "$D"/opencode.db-shm
    chown root:root "$D/opencode.db"; chmod 666 "$D/opencode.db"; ls -ln "$D/opencode.db"
    
    # 3. Service now fails at boot:
    #    background service boot failed ... EPERM: operation not permitted, chmod '.../opencode.db'
    runuser -u tester -- env HOME=/home/tester timeout 15 "$BIN" serve --service --print-logs 2>&1 | grep -E "listening|ERROR"
    pkill -u tester; sleep 1
    
    # 4. Plain `opencode` only prints "Error: Background service failed to start",
    #    and the failed `serve --service` process stays alive holding the port.
    runuser -u tester -- env HOME=/home/tester timeout 40 script -qc "$BIN" /dev/null 2>&1 | head -3
    pgrep -u tester -a
    pkill -u tester

    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