fix(core): prevent partial resource cleanup when a shutdown hook rejects#17040
Open
asn6878 wants to merge 2 commits into
Open
fix(core): prevent partial resource cleanup when a shutdown hook rejects#17040asn6878 wants to merge 2 commits into
asn6878 wants to merge 2 commits into
Conversation
Member
|
Could you please target v12.0.0? |
Closed
4 tasks
Author
|
@kamilmysliwiec I’ll handle the conflicts later today. |
Prevents a failing provider from skipping the remaining cleanup sequence. Closes nestjs#17039
ca7d12b to
0af8c0f
Compare
Author
|
Rebased on v12.0.0 and adapted to the new structure and test infrastructure. The issue was filed against v11 but the same Promise.all fail-fast behavior is still there in v12.0.0's hierarchy-level for loop, so this fix applies here too. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a shutdown reliability bug in @nestjs/core where rejected shutdown lifecycle hooks could cause fail-fast behavior and skip remaining cleanup work across providers/modules.
Changes:
- Switch shutdown hook execution from
Promise.alltoPromise.allSettledforonModuleDestroy,beforeApplicationShutdown, andonApplicationShutdown. - Log rejected hook errors during shutdown instead of failing fast.
- Add unit tests asserting non-throwing behavior, continued execution, and error logging for rejected provider hooks.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/core/hooks/on-module-destroy.hook.ts | Uses Promise.allSettled for provider destroy hooks and logs rejections. |
| packages/core/hooks/on-app-shutdown.hook.ts | Uses Promise.allSettled for provider shutdown hooks and logs rejections. |
| packages/core/hooks/before-app-shutdown.hook.ts | Uses Promise.allSettled for provider pre-shutdown hooks and logs rejections. |
| packages/core/test/hooks/on-module-destroy.hook.spec.ts | Adds tests for rejected provider destroy hooks (and suggested coverage for module hook rejection). |
| packages/core/test/hooks/on-app-shutdown.hook.spec.ts | Adds tests for rejected provider shutdown hooks (and suggested coverage for module hook rejection). |
| packages/core/test/hooks/before-app-shutdown.hook.spec.ts | Adds tests for rejected provider pre-shutdown hooks (and suggested coverage for module hook rejection). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
wrap the module class lifecycle call in try/catch so a rejecting module-level hook no longer aborts the remaining shutdown sequence.
micalevisk
approved these changes
May 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
PR Type
What is the current behavior?
Closes #17039.
When a shutdown lifecycle hook (
onModuleDestroy,beforeApplicationShutdown,onApplicationShutdown) rejects,Promise.allfails fast and all remaining provider cleanup in the same module — as well as all subsequent modules — is silently skipped.What is the new behavior?
Replace
Promise.allwithPromise.allSettledin the three cleanup hook functions. All providers now run their cleanup regardless of sibling failures. Rejected hooks are logged viaLogger.errorwith stack trace. Startup hooks (onModuleInit,onApplicationBootstrap) are unchanged — fail-fast on startup remains intentional.Does this PR introduce a breaking change?