NestJS graceful shutdown limits with async guards during module replacement
0 reputation · 19 Jan 2026, 03:20 UTC
0 reputation · 19 Jan 2026, 03:20 UTC
A small NestJS application is being considered for migration without dropping in-flight requests. The documented capabilities under review are graceful shutdown on process signals and dynamic module loading for service replacement.
Graceful shutdown is handled via app.close() and is intended to allow in-flight requests to complete. Dynamic module loading supports hot-swapping of services while preserving request context. The precise interaction between async guard execution and a zero-downtime module swap is not clearly documented.
What is the documented limit on request draining when app.close() is invoked during an active guard? Does an async guard retain its execution context if the module providing it is replaced mid-request? Is the behavior of async/await in guards during a module swap consistent across NestJS major versions?
NestJS does not impose a specific draining limit on requests when app.close() is called during an active async guard. The framework stops accepting new connections and allows the current request‑processing pipeline to finish; the actual timeout is controlled by the underlying HTTP adapter (Express or Fastify) and any process‑level shutdown timeout you configure.
enableShutdownHooks() and app.close(). NestJS stops listening for new connections and lets in‑flight requests complete.When app.close() is invoked:
CanActivate guard that has already started.async/await chain runs to completion because Nest does not cancel the promise; the request finishes normally.enableShutdownHooks({ stopLoadingNewConnections: true, shutdownTimeout: ... }) or directly on the server.ModuleRef or similar to replace providers/guards at runtime.app.close()) and observe whether the request completes and whether new requests are rejected.Do you have a custom shutdown timeout configured (via enableShutdownHooks or the HTTP server)? Knowing this changes whether you need to adjust the timeout to accommodate long‑running guards.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.