Repository navigation
Conversation
kgeminicdev
left a comment
There was a problem hiding this comment.
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.
…tart Signed-off-by: gophersg <gopher@2980.com>
4ff1ece to
2f5799c
Compare
@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.
app.listen(..., callback)leaves its temporaryerrorlistener attached after a successful start. The callback is wrapped withonce, 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 reportsEADDRINUSE, 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
EADDRINUSEby 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:
test/app.listen.jspass on Node 18 and 26; ESLint passes.