GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — service: default port 49374 collides with ephemeral TCP source ports · Issue #62891 · anomalyco/opencode

service: default port 49374 collides with ephemeral TCP source ports #62891

New issue
Copy link
New issue
Copy link
Open
Open
service: default port 49374 collides with ephemeral TCP source ports#62891
Copy link
Labels
reproduced

Description

@skrimix
skrimix
opened on Nov 9, 2026
Issue body actions

Description

After updating opencode to v2 it hung on start for 20 seconds, then failed:

❯ opencode          
Starting background server...
Error: Server process exited with code 1
Error: Managed service port 49374 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.

There was nothing listening on the port, but another process had a loopback connection with local address 127.0.0.1:49374:

❯ ss '-tanp' 'sport = :49374' 
State                   Recv-Q                   Send-Q                                     Local Address:Port                                        Peer Address:Port                    Process                                                          
ESTAB                   0                        0                                              127.0.0.1:49374                                          127.0.0.1:40387                    users:(("language_server",pid=2785877,fd=32))      

With a lower port set with opencode service set port 19474 opencode launched without issue.

The default port is set within the ephemeral port range used for connections, and can be handed to any process by the OS:

❯ cat /proc/sys/net/ipv4/ip_local_port_range
32768   60999

opencode.log:

timestamp=2026-11-08T20:00:36.299Z level=INFO run=c1ae7c79 message="cli starting" version=2.2.6 channel=latest local=false args=[] role=cli
timestamp=2026-11-08T20:00:36.332Z level=INFO run=c1ae7c79 message="background service starting" reason=missing previousVersion=undefined role=cli
timestamp=2026-11-08T20:00:36.380Z level=INFO run=f74d02e5 message="cli starting" version=2.2.6 channel=latest local=false args="[\"serve\",\"--service\"]" role=cli
timestamp=2026-11-08T20:00:41.380Z level=INFO run=bc3a927e message="cli starting" version=2.2.6 channel=latest local=false args="[\"serve\",\"--service\"]" role=cli
timestamp=2026-11-08T20:00:51.530Z level=INFO run=f74d02e5 message="spawning process" command=lsof args="[\"-nP\",\"-iTCP:49374\",\"-sTCP:LISTEN\",\"-Fpn\"]" cwd=undefined role=server
timestamp=2026-11-08T20:00:51.656Z level=ERROR run=f74d02e5 message="cli process failed" cause="Cause([Fail(Error: Managed service port 49374 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. (cause: ServeError (cause: Error: Failed to start server. Is port 49374 in use?)))])" args="[\"serve\",\"--service\"]" role=cli
timestamp=2026-11-08T20:00:56.530Z level=INFO run=bc3a927e message="spawning process" command=lsof args="[\"-nP\",\"-iTCP:49374\",\"-sTCP:LISTEN\",\"-Fpn\"]" cwd=undefined role=server
timestamp=2026-11-08T20:00:56.700Z level=ERROR run=bc3a927e message="cli process failed" cause="Cause([Fail(Error: Managed service port 49374 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. (cause: ServeError (cause: Error: Failed to start server. Is port 49374 in use?)))])" args="[\"serve\",\"--service\"]" role=cli
timestamp=2026-11-08T20:00:56.724Z level=ERROR run=c1ae7c79 message="cli process failed" cause="Cause([Fail(Error: Server process exited with code 1\nError: Managed service port 49374 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.\n    at <anonymous> (/$bunfs/root/chunk-kc0ah0qw.js:3:7551) {\n  [cause]: ServeError: \n      at R (/$bunfs/root/chunk-73x25c33.js:52:9999)\n      at applyHandlers (node:events:64:20)\n      at emitError (node:events:44:27)\n      at emitListenErrorNextTick (node:_http_server:166:12)\n      at processTicksAndRejections (native:7:39) {\n    [cause]: Error: Failed to start server. Is port 49374 in use?\n        at serve (unknown)\n        at <anonymous> (node:_http_server:438:35)\n        at <anonymous> (node:_http_server:410:33)\n        at <anonymous> (/$bunfs/root/chunk-73x25c33.js:52:10037)\n  }\n})])" args=[] role=cli

This issue was also mentioned in #41696 (comment).

Plugins

none

OpenCode version

2.2.6

Steps to reproduce

  1. Have a default sysctl net.ipv4.ip_local_port_range like 32768 60999 (or any other covering 49374), opencode service not running.
  2. Have an established outgoing connection using local 49374 port.
  3. Run opencode.

Screenshot

No response

Operating System

Arch Linux

Terminal

Konsole

Activity

  1. opencodejr commented on Nov 9, 2026

    @opencodejr
    opencodejrBot
    on Nov 9, 2026
    Contributor
    More actions

    Thanks for the detailed report. I reproduced this on 2.2.6 on Linux. The script below holds an outgoing loopback connection whose local port is 49374, with nothing listening on that port. opencode service start then hangs for about 20 seconds and fails with Managed service port 49374 on 127.0.0.1 is already in use by another process…. With opencode service set port 19474, the service starts immediately.

    It still happens on the latest dev (05099ea). A source build uses the local channel port 49375, and holding a connection on that port fails the same way. The default-port code only moves the port out of the ephemeral range on Windows, not on Linux or macOS.

    Related: #50672 (the same default port, but taken by another user's or WSL client's listener) and #62532 / #62533 (the WSL/Windows reserved range). Until there's a fix, opencode service set port <port below 32768> works around it.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Reproduces #62891: an ESTABLISHED outgoing loopback connection whose *local*
    # port is 49374 (nothing listening on it) blocks the managed service from starting.
    # Usage: ./repro.sh            -> uses `opencode` from PATH
    #        OC_DEV=/path/to/repo ./repro.sh -> runs the CLI from a source checkout
    set -u
    PORT=${PORT:-49374}
    WORK=$(mktemp -d /tmp/opencode/oc62891.XXXX)
    export HOME=$WORK/home XDG_STATE_HOME=$WORK/state XDG_DATA_HOME=$WORK/data XDG_CONFIG_HOME=$WORK/config XDG_CACHE_HOME=$WORK/cache
    mkdir -p "$HOME"
    if [ -n "${OC_DEV:-}" ]; then
      OC=(bun --conditions=browser --preload="$OC_DEV/packages/tui/node_modules/@opentui/solid/scripts/preload.js" "$OC_DEV/packages/cli/src/index.ts")
    else
      OC=(opencode)
    fi
    "${OC[@]}" --version
    
    # Hold a client socket bound to local port 49374, connected to a listener on 40387.
    python3 - <<PY &
    import socket, time
    srv = socket.socket(); srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    srv.bind(("127.0.0.1", 40387)); srv.listen(1)
    c = socket.socket(); c.bind(("127.0.0.1", $PORT)); c.connect(("127.0.0.1", 40387))
    a, _ = srv.accept()
    print("holding ESTABLISHED 127.0.0.1:$PORT -> 127.0.0.1:40387", flush=True)
    time.sleep(120)
    PY
    PYPID=$!
    sleep 1
    echo "--- sockets on :$PORT (note: no LISTEN)"; (command -v ss >/dev/null && ss -tan "sport = :$PORT") || grep -i ":$(printf %04X $PORT) " /proc/net/tcp
    echo "--- opencode service start"
    cd "$WORK" && time timeout 90 "${OC[@]}" service start; echo "exit=$?"
    echo "--- control: same thing with port 19474"
    "${OC[@]}" service set port 19474 && timeout 60 "${OC[@]}" service start; echo "exit=$?"
    "${OC[@]}" service stop >/dev/null 2>&1
    kill $PYPID 2>/dev/null

    Run it: bash repro.sh

  2. opencodejr
    added
    reproduced
    on Nov 9, 2026
  3. iMumd commented on Nov 9, 2026

    @iMumd
    iMumd
    on Nov 9, 2026
    More actions

    Root cause: defaultPort() in packages/cli/src/services/service-config.ts only moves the service port out of the high range on Windows:

    if (platform === "win32" && port >= 0x8000) return 0x7000 | (port & 0xfff)

    The default channel port is 0xC0DE (49374), which also sits inside the ephemeral source-port range on the other platforms — Linux net.ipv4.ip_local_port_range defaults to 32768–60999 and macOS to 49152–65535 — so the kernel can hand 49374 to an outgoing connection, and the service then fails to bind it as "in use" while nothing is listening on it (exactly the ss output in the report).

    Fix: apply the existing 0x7000-block remap on every platform, not just Windows, so 0xC0DE → 0x70DE (28894), below 32768 and outside the ephemeral range on Linux, macOS and Windows alike. It keeps the port stable per channel and reuses the pattern already shipped for Windows.

    Opened a PR for this.

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

    reproduced

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.