opencode icon indicating copy to clipboard operation
opencode copied to clipboard

service: Windows-reserved (excluded) port is reported as "already in use by another process"

Open igorsaevets opened this issue 3 days ago • 3 comments

Description

On Windows, if the service port is inside a TCP range that Windows has excluded, opencode service start reports it as in use by another process. Nothing holds it: netstat -ano shows no socket on the port, and bind() fails with WSAEACCES (10013). People then look for a process that does not exist (#53351, #55086, #55918).

Moving the default below 49152 (#55547, #57428, #66613 for WSL) helps only with Windows' default dynamic port range, 49152-65535. Here the range starts at 1024, and Hyper-V/WinNAT blocks sit below 32768 too (list below). A Visual Studio install has left the same setting on GitHub's Windows runner image (discussion). The configured port here, 19478, is in one of the blocks, and nothing keeps 28894 or 24798 out of them on such a machine.

The error cannot tell the two cases apart: Bun creates Failed to start server. Is port N in use? with code: "EADDRINUSE" for every listen failure except EACCES on Linux (mod.rs), and PortOwner.describe returns undefined on Windows (port-owner.ts).

Suggestion: on Windows, after a failed bind, check netsh interface ipv4 show excludedportrange protocol=tcp (no admin needed) and say that Windows reserved the port, with the range; netstat -ano -p TCP gives the PID when a process holds it. The docs line "Windows reserves ports from 49152 up" (web.mdx) holds only for the default range.

The reserved-port message was suggested in a comment on #54267; that issue is about a service that has not exited, so this one is separate. Related: #50672.

Plugins

None (fresh config in a scratch home folder)

OpenCode version

2.4.2 (@opencode/cli from npm, the current latest). Code links are to dev at e1b5c45.

Steps to reproduce

  1. Check the dynamic port range and the exclusions (no admin needed):
    > netsh int ipv4 show dynamicport tcp
    Start Port      : 1024
    Number of Ports : 64511
    
  2. Pick a port inside one of the excluded blocks: opencode service set port 19478 (block 19405-19504).
  3. opencode service start fails after about 20 s:
    Error: Server process exited with code 1
    Error: Managed service port 19478 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 19478 in use?
            at serve (unknown)
            at <anonymous> (node:_http_server:438:35)
    
  4. netstat -ano -p TCP has no :19478 line; a plain bind() to 127.0.0.1:19478 fails with WSAEACCES (10013).
  5. With opencode service set port 19620 (outside the blocks), opencode service start prints http://127.0.0.1:19620 at once.
netsh int ipv4 show excludedportrange protocol=tcp on this machine
Start Port    End Port
----------    --------
      5357        5357
     13508       13508
     18905       19004
     19005       19104
     19105       19204
     19205       19304
     19305       19404
     19405       19504
     19505       19604
     19663       19762
     25399       25498
     25510       25609
     38498       38597
     49823       49922
     50000       50059     *
     50060       50159
     50160       50259
     50260       50359
     50360       50459
* - Administered port exclusions.

Screenshot

No response

Operating System

Windows 11 Pro 10.0.26200

Terminal

Not terminal-specific: opencode service start fails before any UI (reproduced from a script).

igorsaevets avatar Nov 28 '26 10:11 igorsaevets

Thanks for the detailed report. I couldn't reproduce this here: it needs Windows with an excluded TCP port range, and my environment only runs Linux, which has no excluded port ranges.

On the latest dev (8870cab), the code that produces this message has not changed. When the bind still fails after the retries, the service reports the port as already in use by the process it found, or another process if it found none. On Windows, the lookup for that process returns nothing straight away, so the message always says another process. Whether a reserved port reaches that path depends on Bun reporting WSAEACCES as EADDRINUSE, as your error output shows; I could not check that here.

A maintainer with a Windows machine will need to confirm it. Your steps already cover what they need: an excluded range from netsh int ipv4 show excludedportrange protocol=tcp, opencode service set port with a port inside it, and opencode service start.

Related issues and pull requests:

  • #53351, #55918: the same message on earlier default ports; both were resolved by moving the default port, not by changing the message
  • #54267: where a reserved-port message was suggested
  • #62532 and open PR #66613: the WSL default port in the Windows reserved range
  • #50672, #62891: other problems with the fixed default port

opencodejr[bot] avatar Nov 28 '26 10:11 opencodejr[bot]

I'd like to work on this.

Reading packages/cli/src/server-process.ts:142 and packages/cli/src/services/port-owner.ts on dev, the message is produced whenever the listen fails and no owner is found, and PortOwner.describe returns nothing on Windows, so WSAEACCES (10013) and a real holder look identical there.

Plan: on Windows only, when the listen fails and no owner is found, read netsh interface ipv4 show excludedportrange protocol=tcp (and the ipv6 list when it covers the port) and report that Windows reserved the port, naming the range, instead of the "already in use by another process" wording. When a process does hold the port, the current message stays. The netsh output parsing goes into a small pure function so it can be unit tested without Windows, covering reserved, held, and netsh-unavailable.

One question before I build: is an extra netsh call on a failed start acceptable to you, or would you rather the message stay generic and only the docs line change?

argszero avatar Nov 28 '26 10:11 argszero

Confirmed, this is a real bug in the error path, not a wrong diagnosis of the port state.

I checked the code: on Windows PortOwner.describe returns undefined unconditionally (packages/cli/src/services/port-owner.ts), so server-process.ts always falls back to "already in use by another process" when a managed-service bind fails with EADDRINUSE. A port in an excluded range can never have a listener, so that message sends people hunting for a process that does not exist, exactly as you describe. Your netstat + bind() WSAEACCES evidence matches: nothing holds 19478, Windows just refuses the bind.

I am working on a fix: after a failed bind on Windows, check netsh interface ipv4 show excludedportrange protocol=tcp and report the port as Windows-reserved with the range, instead of claiming another process holds it. Will link the PR here when it is up.

fanyzp avatar Nov 28 '26 10:11 fanyzp

Checked the open point from the bot on this machine: opencode 2.4.2 runs Bun 1.4.4 (BUN_BE_BUN=1 opencode --version), and its Bun.serve reports an excluded port and a held port the same way, while node:net in the same runtime does not:

listen on 127.0.0.1 excluded port (19478) port another process listens on (19620)
Bun.serve EADDRINUSE, errno 0 EADDRINUSE, errno 0
node:net EACCES, errno 10013 EADDRINUSE, errno 10048

So a node:net listen on the port after the failure could tell the two cases apart without netsh. If netsh stays in the plans above: its header text depends on how it is started. On this Windows with a Russian display language it printed Russian headings when started from Python and English ones when started from Bun or pwsh, so matching only the rows of two numbers is safer than matching Start Port.

Probe: BUN_BE_BUN=1 opencode probe.mjs 19478 19620 while another process listens on 19620
import net from "node:net"
for (const port of process.argv.slice(2).map(Number)) {
  try {
    Bun.serve({ port, hostname: "127.0.0.1", fetch: () => new Response() }).stop(true)
    console.log(port, "Bun.serve ok")
  } catch (e) {
    console.log(port, "Bun.serve", e.code, e.errno)
  }
  await new Promise((done) => {
    const srv = net.createServer()
    srv.once("error", (e) => (console.log(port, "node:net", e.code, e.errno), done()))
    srv.listen(port, "127.0.0.1", () => (console.log(port, "node:net ok"), srv.close(), done()))
  })
}
19478 Bun.serve EADDRINUSE 0
19478 node:net EACCES 10013
19620 Bun.serve EADDRINUSE 0
19620 node:net EADDRINUSE 10048

igorsaevets avatar Nov 28 '26 10:11 igorsaevets