Report a clear error and exit cleanly when the HTTP server can't bind its port#469
Merged
Conversation
… its port
app.listen emits its 'error' event asynchronously, after startHttpServer() has
already returned, so index.ts's main().catch never sees it. With no 'error'
listener, a net.Server error (EADDRINUSE, EACCES) surfaces as an uncaught
exception with a raw stack trace and a nonzero-but-noisy exit.
Attach an error handler that logs an actionable message ("… is already in use",
"permission denied binding …", or the raw message for anything else) and exits 1.
The handler is extracted as handleListenError with an injectable onFatal so it is
unit-testable without exiting the test process.
Also guard the success log: Express fires the app.listen callback even when the
bind failed (httpServer.listening is false then), which would otherwise print a
misleading "listening on …" line right before the failure. A bind failure now
produces only the clear error.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
alistair3149
marked this pull request as ready for review
July 23, 2026 05:13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
app.listenemits its'error'event asynchronously — afterstartHttpServer()has already returned — soindex.ts'smain().catchnever sees it. With no'error'listener, a bind failure (EADDRINUSE,EACCES) surfaced as an uncaught exception with a raw stack trace.This attaches an error handler so a bind failure produces a clear, actionable message and a clean
exit(1):What changed
handleListenError(err, host, port, onFatal?)instreamableHttp.ts: logsEADDRINUSE/EACCES/ generic distinctly and terminates.onFatalis injectable so it is unit-testable without exiting the test process. Wired viahttpServer.on('error', …)instartHttpServer().app.listencallback even when the bind failed (httpServer.listeningisfalsethen), which would otherwise print a misleadinglistening on …line right before the failure. A bind failure now logs only the clear error.This is the deferred follow-up from the boot-extraction (#466), now unblocked since
startHttpServer()is on master.Testing
handleListenError(EADDRINUSE / EACCES / generic → correct message +onFatal(1)).1, logs onlyCannot start HTTP server: … is already in use., and prints no stack trace and no misleadinglisteningline; a successfully-bound server still logslistening.fmt:check,lint,tsc, tests) green.🤖 Generated with Claude Code