preview_start: autoPort always fails on Windows/Git Bash with "port in use" for all free ports

Resolved 💬 0 comments Opened Jun 12, 2026 by hidamiki Closed Jun 12, 2026

Environment

  • OS: Windows 10 Home China 10.0.19045
  • Shell: Git Bash (MSYS2 MINGW64_NT-10.0-19045)
  • Claude Code: 2.1.139
  • lsof available: No (which lsof returns nothing, ss -tlnp produces empty output)
  • netstat available: Yes (netstat -ano works correctly)

Bug Description

preview_start consistently reports "Port X is in use" for ALL ports, even when the port is confirmed free by both netstat -ano and PowerShell Get-NetTCPConnection.

Reproduction Steps

  1. Ensure no process is running on port 52358 (verify with netstat and Get-NetTCPConnection)
  2. Create a minimal launch.json:
{
  "version": "0.0.1",
  "configurations": [
    {
      "name": "test-server",
      "runtimeExecutable": "python",
      "runtimeArgs": ["-m", "http.server", "52358", "--bind", "127.0.0.1"],
      "cwd": "${workspaceFolder}",
      "port": 52358
    }
  ]
}
  1. Call preview_start with name "test-server"

Expected Behavior

Server starts on port 52358 (which is confirmed free).

Actual Behavior

preview_start reports: Port 52358 is in use by another process (not a preview server).

Additional Details

  • Using autoPort: true + port: 0 does NOT help — autoPort selects different ports each time, but ALL are falsely reported as "in use"
  • Direct Bash startup (python -m http.server <port>) works fine — the port binding succeeds
  • This suggests the port detection logic inside preview_start relies on lsof/ss which don't work properly in Git Bash on Windows
  • The only time preview_start correctly identified a port as occupied was when a node.exe process was actively LISTENING on port 12345 — in that case it correctly reported the PID and process name

Potential Root Cause

The port detection likely uses lsof or ss on Unix, which are non-functional in MSYS2/Git Bash on Windows. The fallback to netstat (which DOES work) may not be implemented or is failing silently.

View original on GitHub ↗