Description
On Windows 11, opencode web --port 4096 (and opencode serve --port 4096) fails with only:
Error: Unexpected error
ServeError
This is the same generic output reported in #46263 / #48784 / #43110 / #38738, but the underlying situation here is different and much harder for a user to diagnose: the port is held by an orphaned socket whose owning process no longer exists, so no listener can be found and killed.
Diagnostics
> netstat -ano | findstr :4096
TCP 127.0.0.1:4096 0.0.0.0:0 LISTENING 26524
TCP 127.0.0.1:4096 127.0.0.1:7914 CLOSE_WAIT 26524
TCP 127.0.0.1:4096 127.0.0.1:8583 CLOSE_WAIT 26524
... (several more CLOSE_WAIT entries)
> tasklist /FI "PID eq 26524"
INFO: No tasks are running which match the specified criteria.
> powershell> Stop-Process -Id 26524 -Force
Stop-Process : Cannot find a process with the process identifier 26524.
The TCP table still has a LISTENING entry attributed to PID 26524, but that PID is not in the process table (tasklist, Get-Process, Get-CimInstance Win32_Process all empty). Sysinternals handle64.exe cannot map the socket back to a live owner either (AFD socket handles are reported as \Device\Afd with no port information).
A raw bind test proves this is a real address conflict and not an opencode bug:
TcpListener(127.0.0.1, 4096).Start() -> SocketException WSAEADDRINUSE (10048)
"Only one usage of each socket address (protocol/network address/port) is normally permitted"
TcpListener(127.0.0.1, 4097).Start() -> OK
opencode serve --port 4097 -> starts fine, serves requests
opencode serve --port 4096 -> Error: Unexpected error / ServeError
Get-NetTCPConnection also reports a stale CreationTime for this socket (it predates the last system boot), which points at an orphaned/duplicated socket handle kept alive by another process (a firewall/HIPS or shell-injection tool that duplicated the handle, in my case).
Environment
- OpenCode
1.18.31 (scoop install)
- Windows 11 build 26200.9457 (PowerShell 5.1, Windows Terminal)
Steps to reproduce
- Start
opencode web --port 4096, then terminate it in a way that leaves a duplicated/inherited socket handle behind (the port ends up attributed to a PID that no longer exists).
opencode web --port 4096 again.
- Observe
Error: Unexpected error / ServeError with no errno, no address/port, no hint that the port is occupied.
What would help
- Surface the underlying listen errno and the bound address/port, e.g.
EADDRINUSE/WSAEADDRINUSE: 127.0.0.1:4096 already in use. opencode serve: bind failures print bare "Unexpected error / ServeError" — the listen errno is never surfaced #38738/opencode serve shows "Unexpected error" with no details when startup fails #48784 already describe this part.
- When
--port is explicitly given and the bind fails, say so clearly instead of ServeError, and on Windows point at netstat -ano | findstr :<port>.
- Detect the Windows-specific case where the TCP table shows a
LISTENING entry whose OwningProcess no longer exists and print it explicitly, e.g. port 4096 is held by a stale/orphaned socket (owner PID 26524 is gone); a reboot is required to clear it.
- Optionally, when a fixed
--port cannot be bound, offer/perform a fallback to a free port and print the URL it actually used (with a note that the browser origin changes, which resets web-UI local settings).
Note that #49006 (VS Code extension hardcoding --port=4096) makes this failure mode more likely to hit users, since 4096 becomes a hot port.