Open
Conversation
|
I too am affected by this: the SIGINT and SIGTERM listeners installed by threads.js terminate Node.js before my cleanup code finishes running. I don't think this pull request is the correct fix. Installing any signal handler inhibits the default behavior (which is to terminate node). See #198 for the reason why What is needed is the ability to terminate workers without inhibiting the default signal handling behavior. Unfortunately there is no way to do this. Maybe this hacky approach is good enough: const terminateWorkersAndMaster = (signal) => {
Promise.all(allWorkers.map(worker => worker.terminate().catch(() => {}))).then(() => {
if (process.listenerCount(signal) > 1) {
// Assume that one of the other signal listeners will take care of calling process.exit().
// This breaks down if all of the other listeners are making the same assumption.
// Unfortunately Node.js does not provide a way to run code when a signal arrives without
// inhibiting the default signal handler. (The 'exit' event isn't usable for this because it
// is not emitted when the default signal handler terminates the process.)
return
}
// Right now this is the only signal listener, so assume that this listener is to blame for
// inhibiting the default signal handler. (This assumption fails if the number of listeners
// changes during signal handling. This can happen if a listener was added by process.once().)
// Mimic the default behavior, which is to exit with a non-0 code.
process.exit(1)
})
allWorkers = []
} |
Owner
|
I opened pull request #329 with my suggested approach. |
|
I agree you should not install any signal listeners |
Merged
43 tasks
kurtmc
approved these changes
Sep 8, 2025
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.
There may be graceful shutdown happening elsewhere - other processes can be orphaned by doing this.