Skip to content

fix(start): wait for async shutdown hooks before exiting#3313

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
maruthang:fix/issue-3158-async-shutdown-hooks
Apr 7, 2026
Merged

fix(start): wait for async shutdown hooks before exiting#3313
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
maruthang:fix/issue-3158-async-shutdown-hooks

Conversation

@maruthang

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?

[x] 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?

When running nest start, pressing Ctrl+C (SIGINT) causes the parent CLI process to immediately kill the child NestJS application via treeKillSync, without giving it time to execute async shutdown hooks (onModuleDestroy, onApplicationShutdown). This means graceful shutdown logic (closing DB connections, flushing logs, etc.) never runs.

Issue Number: #3158

What is the new behavior?

The parent process now installs SIGINT and SIGTERM handlers that forward the signal to the child process via childProcessRef.kill(signal) instead of killing it immediately. The parent stays alive until the child exits naturally, giving the NestJS application time to run its async shutdown lifecycle hooks before the process terminates.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

Files changed:

  • actions/start.action.ts — Added signal forwarding handlers that send SIGINT/SIGTERM to the child process instead of killing it immediately
  • test/actions/start.action.spec.ts — Added 4 regression tests verifying signal forwarding behavior

@kamilmysliwiec
kamilmysliwiec merged commit e3101fc into nestjs:master Apr 7, 2026
1 check passed
@kamilmysliwiec

Copy link
Copy Markdown
Member

this PR introduced 2 regressions, reverting..

kamilmysliwiec added a commit that referenced this pull request Apr 15, 2026
kamilmysliwiec pushed a commit that referenced this pull request May 1, 2026
re-apply signal forwarding from prs #3313/#3338 that was reverted
in 705bb7d, and explicitly exit when the spawned child exits during
shutdown so the chokidar/tsc watcher does not keep the event loop
alive.

closes #3158
refs #3391
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