Skip to content

fix: detect port-in-use before starting (#1234)#1313

Closed
echopi wants to merge 1 commit intoavwo:masterfrom
echopi:fix/port-check-1234
Closed

fix: detect port-in-use before starting (#1234)#1313
echopi wants to merge 1 commit intoavwo:masterfrom
echopi:fix/port-check-1234

Conversation

@echopi
Copy link
Copy Markdown
Contributor

@echopi echopi commented Apr 6, 2026

Summary

Fixes #1234w2 start silently starts even when the default port (8899) is already occupied, causing confusing behavior.

Changes:

  • Add checkPortAvailable() pre-flight check in index.js before starting whistle
  • If port is in use, print a clear error and exit:
    [!] Port 8899 is already in use.
    [i] Use -p <port> to specify another port.
    
  • Skip the check when a custom server is provided (already bound)
  • Existing EADDRINUSE handling in bin/whistle.js preserved as fallback

Test plan

  • Start another service on port 8899, then run w2 start — should see error and exit
  • Run w2 start -p 9999 with 8899 occupied — should start normally on 9999
  • Run w2 start with port free — should start normally (no behavior change)

🤖 Generated with Claude Code

@echopi echopi force-pushed the fix/port-check-1234 branch from c7878f5 to f587831 Compare April 6, 2026 12:05
Add a pre-flight port availability check in cluster/daemon mode to catch
port conflicts early, before whistle silently starts on a partially
occupied port.

- checkPortAvailable() probes 0.0.0.0 to detect IPv4 port conflicts
  even when the real server would bind to IPv6
- Errors propagate naturally via throw (no process.exit in library code)
- Non-cluster mode preserves synchronous return (programmatic API compat)
- Skip check when a custom server is provided (already bound)
- Existing EADDRINUSE fallback in bin/whistle.js preserved

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@echopi echopi force-pushed the fix/port-check-1234 branch from f587831 to 3c6c03f Compare April 6, 2026 12:11
echopi added a commit to echopi/whistle that referenced this pull request Apr 6, 2026
Add a port availability pre-check before starting the whistle server.
When the port is already in use, output a friendly error message to
stderr and exit, instead of crashing with an uncaught exception.

Compared to the previous attempt (PR avwo#1313), this fix:
- Works for all startup modes, not just cluster mode
- Uses console.error + process.exit instead of throw in async callback
- Does not include unrelated changes (cluster mode, config loading, etc.)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@echopi
Copy link
Copy Markdown
Contributor Author

echopi commented Apr 6, 2026

Superseded by #1316 — fix moved to lib/index.js with proper error handling, no TOCTOU, no return value regression.

@echopi echopi closed this Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

w2 start时检测一下8899端口是否被占用

1 participant