Skip to content

Shutdown lifecycle hooks skip remaining providers when one throws (Promise.all fail-fast) #17039

@asn6878

Description

@asn6878

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

If any provider's onModuleDestroy, beforeApplicationShutdown, or onApplicationShutdown hook throws or rejects, Promise.all fails fast and the following seem to be silently skipped:

remaining providers in the same module
transient-scoped providers in that module
the module class instance's own hook
all subsequent modules in the shutdown sequence

This means DB connections, file handles, message queue connections, etc. could be left open on shutdown.

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-cmw828to?file=src%2Fmain.ts

Steps to reproduce

  1. Register two providers that implement OnModuleDestroy
  2. Make the first provider's hook throw/reject
  3. Send SIGTERM (or call app.close())
  4. Observe that the second provider's hook is never called

In Reproduction Code(CodeSandbox)

  1. Register two providers that implement OnModuleDestroy
  2. Make the first provider's hook throw/reject
  3. Call app.close() (or send SIGTERM)
  4. Observe that "FileService: cleanup called" is never printed in the console. (Which means FileService's onModuleDestroy is never called

Expected behavior

All providers should attempt their cleanup regardless of failures in sibling providers. Individual hook errors should be logged, but shouldn't stop the rest of the shutdown sequence — similar to how Promise.allSettled works.

NestJS version

11.1.24

Packages versions

@nestjs/core: 11.1.10

Node.js version

24.0.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue has not been looked into

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions