Skip to content

fix(core): reset the listening state when the application closes - #17951

Merged
kamilmysliwiec merged 2 commits into
nestjs:masterfrom
Chaoqi31:fix/listening-reset-on-close
Sep 30, 2026
Merged

kamilmysliwiec merged 2 commits into
nestjs:masterfrom
Chaoqi31:fix/listening-reset-on-close

Conversation

@Chaoqi31

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

await app.listen(3000);
await app.close();

app.get(HttpAdapterHost).listening; // true
await app.getUrl();
// TypeError: Cannot read properties of null (reading 'port')

NestApplication.listen() sets isListening and HttpAdapterHost.listening to true, but dispose() never sets them back. After close(), HttpAdapterHost.listening, documented as whether the application is listening for incoming requests, still reports true. getUrl() passes its CALL_LISTEN_FIRST guard because isListening is still set, reads the closed server's address() (null) and fails with a TypeError. This shows up in tests or tools that stop and restart apps, and in code that checks listening.

The new spec against the current nest-application.ts:

 × NestApplication > close > should reset the listening state
AssertionError: expected true to be false // Object.is equality

Issue Number: N/A

What is the new behavior?

dispose() sets isListening and HttpAdapterHost.listening to false right after the HTTP adapter closes, so after close() the flag reports false and getUrl() rejects with app.listen() needs to be called before calling app.getUrl(). listen$ only emits when the flag becomes true, so it does not emit or complete on close. If the adapter's close() throws, the flags stay as they were, since the server may still be running.

The new test listens on a random local port and closes the app. A throwaway spec with the real Express and Fastify adapters showed the same result on both.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

npx vitest run packages/core: 1400 passed.

`listen()` sets `isListening` and `HttpAdapterHost.listening` to true,
but closing the application never reset them. After `app.close()`,
`HttpAdapterHost.listening` still reported true, and `app.getUrl()`
skipped its "call listen first" guard and rejected with
`TypeError: Cannot read properties of null (reading 'port')`.

Reset both flags once the HTTP adapter has closed.
@kamilmysliwiec
kamilmysliwiec merged commit c9ee47a 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.

2 participants