-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix process not ending correctly with shutdown hooks #14433
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I'm following.
process.exit
(force exit) shouldn't be necessary. We're also overriding the signal (the reason to kill the process) hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There "about to exit" is called. I don't understand why my unit test is failing without changing to process.exit(0).
I understand you'd want to wait for node to start the shutdown process - maybe the problem is coming from nestjs/cli / commander not waiting or not emitting the exit event on the child process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work even if you run
node dist/main
(no NestJS CLI involved)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code
Node v20
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still cannot reproduce your issue with the following code:
With
node dist/main
:With
nest start
Same with
nest start --watch
(npm run start:dev)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you just released your own json logger - so it might not be your issue.
In the end I only care the exit listener because pino is using https://www.npmjs.com/package/on-exit-leak-free to flush the logs in the shutdown scenario to clear the buffers of the logger. Pino is using a worker thread to do the writing to stdout to be more performant. One alternative solution I came up with was calling logger.flush() before the kill.
iamolegga/nestjs-pino#1859
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will try out my example again and upload it to github - maybe it's something that got changed with v11
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you accidentally found the issue 🤯
I created /~https://github.com/thomaschaaf/debug-nest-js-on-exit you can start it with either
SIGTERM=1 ./node_modules/.bin/nest start
or
SIGTERM=0 ./node_modules/.bin/nest start
This toggles: /~https://github.com/thomaschaaf/debug-nest-js-on-exit/blob/main/src/main.ts#L30-L33
I have also added an extra console log in pino: /~https://github.com/thomaschaaf/debug-nest-js-on-exit/blob/main/node_modules/pino/lib/transport.js#L62
The interesting bit is if you have no handler for sigterm the process.on('exit') is not called. I don't understand why.. With the change in the pr it is "fixed" but I understand it seems to be more of a workaround. I guess we will just add a handler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can replicate the exact same behavior using plain Node.js (without the Nest framework/CLI), so I'm not sure if there's anything we need to change on our end.