Skip to content

fix(microservices): settle the tcp listen callback on any listen error - #17922

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
johnnyhuirilef:fix/tcp-listen-settles-on-any-error
Sep 30, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
johnnyhuirilef:fix/tcp-listen-settles-on-any-error

Conversation

@johnnyhuirilef

@johnnyhuirilef johnnyhuirilef commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

Issue Number: closes #17921

ServerTCP.listen() only passes a listen error to its callback when the code is EADDRINUSE or ECONNREFUSED. Node calls the listen callback on success only, so any other listen failure is logged and nothing settles. await app.listen() stays pending, and the status keeps its previous value.

Measured with the repro on 12.1.1:

listen on 203.0.113.1:3001 (EADDRNOTAVAIL)  ->  hung
listen on 127.0.0.1:80     (EACCES)         ->  hung

What is the new behavior?

The listen callback runs exactly once. It runs with the first listen error, whatever its code, or when the server starts listening. It uses the same listenCallback idiom that ServerMqtt and ServerRMQ use to settle theirs (#17641, #17675).

An error emitted after the server is listening stays with the permanent error listener. It is logged as before, and it neither reaches the callback again nor flips the status to disconnected.

The same repro against a local build of this branch:

listen on 203.0.113.1:3001 (EADDRNOTAVAIL)  ->  rejected
listen on 127.0.0.1:80     (EACCES)         ->  rejected

This is the microservice counterpart of #3209 / #3360, which made the HTTP listen() reject on bind failures.

Does this PR introduce a breaking change?

  • Yes
  • No

app.listen() used to hang forever for these errors. It now rejects, as it already did for EADDRINUSE. Nothing exported changes. EADDRINUSE is still exported from constants.ts; only its import in server-tcp.ts became unused.

Other information

Three of the new tests fail without the source change: EACCES, EADDRNOTAVAIL, and an error without a code. The others pin the behavior that must not change: EADDRINUSE and ECONNREFUSED still reach the callback, and an error after the server is listening neither calls it again nor reports the status as disconnected.

ServerTCP.listen only passed EADDRINUSE and ECONNREFUSED to the callback.
Node runs the listen callback on success only, so every other listen error
(EACCES on a privileged port, EADDRNOTAVAIL for a host address the machine
does not own) was logged and then nothing settled: the promise behind
app.listen() stayed pending forever and the status never left its previous
value.

The callback now runs exactly once, with the first listen error whatever its
code, or when the server starts listening, the way the mqtt and rmq servers
settle theirs. An error emitted after listening stays with the permanent
error listener: it does not reach the callback again and does not flip the
status to disconnected.

Closes nestjs#17921
@johnnyhuirilef
johnnyhuirilef force-pushed the fix/tcp-listen-settles-on-any-error branch from 15f7b37 to fe4dfd3 Compare September 29, 2026 14:22
@kamilmysliwiec
kamilmysliwiec merged commit 1ae3070 into nestjs:master Sep 30, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ServerTCP: await app.listen() never settles when listen fails with an error other than EADDRINUSE

2 participants