Skip to content

process: add process.gracefulExit()

Checklist
  • tests and code linting passes
  • a test and/or benchmark is included
  • documentation is changed or added
  • the commit message follows commit guidelines
Affected core subsystem(s)

process

Description of change

Provides a deferrable, async alternative to process.exit(). Whereas process.exit() exits the process as quickly as possible, process.gracefulExit(), by default, will not exit until the next tick of the event loop (scheduled by setImmediate). A callback function can be provided to customize the exit process and invoke the actual exit when desired or appropriate to do so.

Exits with code 1 on the next tick of the event loop:

process.gracefulExit(1);

Exits with code '0' on process.nextTick():

process.gracefulExit(1, (code, exit) => {
  console.log('exiting gracefully on next tick');
  process.nextTick(() => exit(0));
});

Immediately exit if the graceful exit is not completed within 10 seconds:

process.gracefulExit(0, 10000, (code, exit) => {
  // Wait, do nothing. don't call exit.
});

The implementation is such that process.exit() is still called ultimately, so process.on('exit') handlers will be invoked.

In cluster/child-processes with IPC, this does not take the place of process.disconnect().

Note: to a degree this is speculative and yes, it is possible for this to live in userland.

Merge request reports

Loading