Skip to content

[Windows] web/serve with a fixed --port fails with a bare Unexpected error / ServeError when the port is held by an orphaned socket whose owner PID no longer exists #51899

Closed as not planned

Description

@ling9ling7

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

  1. 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).
  2. opencode web --port 4096 again.
  3. Observe Error: Unexpected error / ServeError with no errno, no address/port, no hint that the port is occupied.

What would help

  1. 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.
  2. When --port is explicitly given and the bind fails, say so clearly instead of ServeError, and on Windows point at netstat -ano | findstr :<port>.
  3. 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.
  4. 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.

Activity

  1. github-actions commented on Sep 28, 2026

    @github-actions
    Contributor

    This issue might overlap with existing issues. Please check:

    • #32932: Windows opencode serve restart fails with ServeError when a dead PID still appears to hold the listening port.
    • #46263: web and serve show a generic error when the requested port is already in use.
    • #38738 and #48784: bind/startup failures surface only as a generic ServeError with no useful details.

    The orphaned-socket cause described here may be distinct, but these issues cover closely related symptoms and error reporting.

  2. thdxr commented on Oct 11, 2026

    @thdxr
    Member

    Thanks for the report! OpenCode 2.0 is now out and development is focused on V2, so we're closing issues filed against OpenCode 1. Please upgrade (opencode upgrade --major, details: https://anoma.ly/notes/opencode-2-0), and if this is still relevant on 2.0, reopen this issue or open a new one with your 2.0 version.

    — from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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