Skip to content

fix(app.listen): remove the listen error handler after a successful start - #7501

Open
gophersg wants to merge 2 commits into
expressjs:masterfrom
gophersg:fix/listen-error-listener
Open

gophersg wants to merge 2 commits into
expressjs:masterfrom
gophersg:fix/listen-error-listener

Conversation

@gophersg

@gophersg gophersg commented Oct 2, 2026 •

Copy link
Copy Markdown

app.listen(..., callback) leaves its temporary error listener attached after a successful start. The callback is wrapped with once, so the leftover listener does nothing when a later error arrives. If the application has no other error listener, that first error is silently consumed.

A concrete example is starting a server, closing it, then calling server.listen() again on a port occupied by another server. The operating system reports EADDRINUSE, but the stale listener hides it. The process can remain running even though this server is no longer listening.

Remove the temporary error listener on listening, before the user callback runs. If binding fails, remove the pending listening handler and deliver the error to the callback. Later errors follow normal Node.js behavior: application error handlers receive them, or they are unhandled if no handler exists.

The tests check cleanup after successful startup and generate a real EADDRINUSE by attempting to listen again on an occupied local port. They verify that no startup error listener remains when that error occurs. Servers are closed before assertions so failures do not leave open handles.

Validation:

  • All 7 tests in test/app.listen.js pass on Node 18 and 26; ESLint passes.
  • Reverting the production fix in an isolated copy makes the new regression test fail and exit normally, without forcing process termination.

@kgeminicdev kgeminicdev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, the fix looks right to me. I compared master and this branch with a small script:

master this PR
'error' listeners after a successful start 1 0
'error' emitted after start silently swallowed thrown (normal EventEmitter behavior)
port already in use callback gets EADDRINUSE callback gets EADDRINUSE

test/app.listen.js passes on this branch (7 passing). Two small suggestions inline: a History.md note for the behavior change, and making the new test fail cleanly instead of hanging.

Comment thread lib/application.js Resolved
Comment thread test/app.listen.js Resolved
…tart

Signed-off-by: gophersg <gopher@2980.com>
@gophersg
gophersg force-pushed the fix/listen-error-listener branch from 4ff1ece to 2f5799c Compare October 8, 2026 14:48
@gophersg

gophersg commented Oct 8, 2026

Copy link
Copy Markdown
Author

Thanks for this, the fix looks right to me. I compared master and this branch with a small script:

master this PR
'error' listeners after a successful start 1 0
'error' emitted after start silently swallowed thrown (normal EventEmitter behavior)
port already in use callback gets EADDRINUSE callback gets EADDRINUSE
test/app.listen.js passes on this branch (7 passing). Two small suggestions inline: a History.md note for the behavior change, and making the new test fail cleanly instead of hanging.

@kgeminicdev Thanks for the review! I added an Unreleased Changes entry and wrapped the post-listen assertion in try/finally so the server is closed even if the assertion fails. I also remove the pending listening handler when the bind fails. I’ve pushed the updates as a single commit.

Replace the synthetic post-listen error with EADDRINUSE from listening again on an occupied local port. Check for stale startup error listeners before they run, and close the servers before making assertions.
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.

3 participants