Skip to content
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

RFC process.on("shutdown") proposal #20804

Closed
pravdomil opened this issue May 17, 2018 · 8 comments
Closed

RFC process.on("shutdown") proposal #20804

pravdomil opened this issue May 17, 2018 · 8 comments
Labels
feature request Issues that request new features to be added to Node.js. process Issues and PRs related to the process subsystem. wontfix Issues that will not be fixed.

Comments

@pravdomil
Copy link

pravdomil commented May 17, 2018

See this popular question on so https://stackoverflow.com/q/14031763/3748498

By now there is no clean API to do cleanup when node.js exits, in order to catch all situation you need to do following:

for (const event of ["exit", "SIGINT", "SIGUSR1", "SIGUSR2", "uncaughtException", "SIGTERM"]) {
  process.on(event, cleanUp)
}

This issue is a place for discussion how to fix it.

My proposal is process.on("shutdown", cleanUp).

@Fishrock123
Copy link
Member

There's an npm module that does this, right? Does it need to be in core? Would it be that much more helpful?

@Fishrock123 Fishrock123 added feature request Issues that request new features to be added to Node.js. process Issues and PRs related to the process subsystem. labels May 17, 2018
@devsnek
Copy link
Member

devsnek commented May 17, 2018

there should definitely be a One True Way to tell if node is about to exit. (as its worth noting that receiving some of those events does not guarantee that node will exit in every situation) i think we should just streamline {things in c++ that make node stop running} to always go through a call to js if possible.

@jasnell
Copy link
Member

jasnell commented May 17, 2018

My preference would be to make process.on('exit') always run in every case.

@mscdex
Copy link
Contributor

mscdex commented May 17, 2018

Changing the behavior of 'exit' could cause breakage. I would prefer a new event to avoid such issues. However there is a problem if you'd be using such a new event for "cleanup" because you wouldn't be able to do anything asynchronous, unless you additionally have a 'beforeShutdown' event or something similar.

@addaleax
Copy link
Member

There are reasons why this isn’t the default for process.on('exit') – libuv’s signal handling is asynchronous (as opposed to synchronous OS-enforced exit by default), so adding listeners for these events would avoid mean that we can’t break out of e.g. infinite loops without significantly more complex signal handling code.

@Horaddrim
Copy link

And maybe be confusing one unique event, because if I listen to the event uncaughtException, maybe I use a different handler to a SIGTERM event. And @addaleax is extremely right to point that, maybe the assertion of this event can break some applications if you really depends on the ending of some other process.

@gireeshpunathil
Copy link
Member

is there an actionable on this issue? Is there any forward path justified / identified?

@bnoordhuis
Copy link
Member

It is technically possible to make this happen but, as Anna mentions, doing it properly significantly complicates signal handling; think signal masks, watchdog threads and asynchronous termination exceptions. We'd also need to carefully audit the code base for termination exception safety.

I'm going to say the cost/benefit ratio currently isn't worth it and close this out. If someone volunteers, we can reconsider. And who knows, maybe it'll fall out as a side effect of worker-ifying core someday.

@bnoordhuis bnoordhuis added the wontfix Issues that will not be fixed. label Dec 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. process Issues and PRs related to the process subsystem. wontfix Issues that will not be fixed.
Projects
None yet
Development

No branches or pull requests

9 participants