Skip to content

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

Open
echopi wants to merge 2 commits intoavwo:masterfrom
echopi:fix/port-check-1234-v2
Open

fix: detect port-in-use before starting (#1234)#1316
echopi wants to merge 2 commits intoavwo:masterfrom
echopi:fix/port-check-1234-v2

Conversation

@echopi
Copy link
Copy Markdown
Contributor

@echopi echopi commented Apr 6, 2026

Summary

  • Add server.on('error') handler in lib/index.js to catch EADDRINUSE and print a friendly error message
  • Use exclusive: true in server.listen() to ensure port conflicts are detected across platforms (macOS uses SO_REUSEADDR by default)
  • Add standalone port-in-use test case

Fixes #1234

What changed

lib/index.js (+10/-1):

  1. Error handler on main HTTP server — prints friendly message on EADDRINUSE:
    [!] Port 8899 is already in use.
    [i] Use -p <port> to specify another port.
    
  2. server.listen() changed to use { exclusive: true } to prevent SO_REUSEADDR from masking port conflicts

test/units/port-in-use.test.js + test/helpers/start-whistle.js: Standalone test that occupies a port, starts whistle on the same port, and verifies the friendly error message appears.

Design decisions

Concern Decision
Where to handle lib/index.js server error event, not index.js pre-check — avoids TOCTOU race, preserves sync return value
exclusive: true Required on macOS where SO_REUSEADDR allows duplicate binds silently
Error re-thrown after message Lets existing uncaughtException handler in same file do process cleanup

Test plan

# Standalone port-in-use test
node test/units/port-in-use.test.js
  • Port occupied → friendly error message, non-zero exit
  • Verified on macOS with exclusive: true

🤖 Generated with Claude Code

@echopi echopi force-pushed the fix/port-check-1234-v2 branch from 99ebb0d to f751582 Compare April 6, 2026 12:35
@echopi echopi force-pushed the fix/port-check-1234-v2 branch 3 times, most recently from ce931ce to 9b19ecf Compare April 6, 2026 15:46
echopi and others added 2 commits April 6, 2026 08:47
Node.js v17+ (OpenSSL 3.0) rejects EE certificates with keys smaller
than 2048-bit. The test certs were 1024-bit, causing ERR_SSL_EE_KEY_TOO_SMALL.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Port 8081 is commonly used by other services (e.g. AliLang), causing
EADDRINUSE when running tests. Use a high port to reduce conflicts.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@echopi echopi force-pushed the fix/port-check-1234-v2 branch from 9b19ecf to 1963b6a Compare April 6, 2026 15:47
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