Skip to content

Conversation

@manureja64
Copy link
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?

Currently, when an application shuts down:

By default, the server stops accepting new connections but keeps existing keep-alive connections open indefinitely, potentially hanging the shutdown process (e.g., preventing a Kubernetes pod from terminating).

If forceCloseConnections is set to true, the server destroys all open sockets immediately. This kills in-flight requests, causing clients to experience "socket hang up" or ECONNREFUSED errors.

Issue Number: #11416

What is the new behavior?

This PR adds a gracefulShutdown property to NestApplicationOptions.

When gracefulShutdown: true is enabled (currently implemented for the Express adapter):

Upon calling app.close(), a flag isShuttingDown is set to true.

A middleware intercepts any new requests (even those arriving on existing keep-alive connections) and immediately responds with 503 Service Unavailable and a Connection: close header.

In-flight requests are allowed to proceed and complete naturally.

This allows load balancers and clients to gracefully back off while ensuring no active work is interrupted.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

This implementation follows a pattern similar to Fastify's return503OnClosing option, providing a middle ground between hanging indefinitely and forcefully killing connections.

Verified with a new integration test suite in integration/graceful-shutdown.

Override root tsconfig.json exclusion so TypeScript project service finds
e2e specs. Also set target, strict, and
esModuleInterop compiler options.
Remove leftover debug logging statements to reduce console noise in the adapter.
@coveralls
Copy link

coveralls commented Jan 4, 2026

Pull Request Test Coverage Report for Build 860a113c-21c5-4c9d-a0ba-6c7554faadd1

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 89.759%

Totals Coverage Status
Change from base Build a41a5432-6384-48b4-9662-8f7d90ec0de6: 0.0%
Covered Lines: 7441
Relevant Lines: 8290

💛 - Coveralls

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.

2 participants