signal-exit vs exit-hook vs node-cleanup
Node.js Process Termination Handling Comparison
1 Year
signal-exitexit-hooknode-cleanup
What's Node.js Process Termination Handling?

These npm packages provide mechanisms to handle process termination signals in Node.js applications. They allow developers to execute cleanup logic or perform specific actions when a Node.js process receives termination signals such as SIGINT or SIGTERM. This is particularly useful for gracefully shutting down applications, releasing resources, and ensuring data integrity before the process exits. Each package has its own approach to managing exit hooks, making them suitable for different use cases.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
signal-exit104,698,23318977 kB62 years agoISC
exit-hook2,816,34328211.3 kB52 years agoMIT
node-cleanup1,374,844168-98 years agoMIT
Feature Comparison: signal-exit vs exit-hook vs node-cleanup

Signal Handling

  • signal-exit:

    signal-exit listens for process termination signals and ensures that a cleanup function is executed when the process is terminated, regardless of how it exits (normal exit or due to a signal). This makes it a reliable choice for applications that need to handle unexpected terminations.

  • exit-hook:

    exit-hook allows you to register multiple exit handlers that can handle various termination signals (like SIGINT, SIGTERM) and execute them in a specified order. This is useful for applications that need to perform multiple cleanup tasks before exiting, ensuring that each task completes before the process terminates.

  • node-cleanup:

    node-cleanup provides a simple way to handle cleanup on process termination. It automatically registers a single cleanup function that is executed when the process exits, making it easy to ensure that necessary cleanup is performed without managing multiple handlers.

Asynchronous Cleanup

  • signal-exit:

    signal-exit does not inherently support asynchronous cleanup functions, but it can be combined with other asynchronous mechanisms. It is primarily designed for synchronous cleanup tasks that need to be executed reliably.

  • exit-hook:

    exit-hook supports asynchronous cleanup functions, allowing you to perform non-blocking operations before the process exits. This is beneficial for applications that may need to wait for I/O operations or other asynchronous tasks to complete during cleanup.

  • node-cleanup:

    node-cleanup's cleanup function is synchronous, meaning it will block the process until the cleanup is complete. This can be useful for simple cleanup tasks but may not be suitable for applications that require non-blocking behavior during shutdown.

Ease of Use

  • signal-exit:

    signal-exit is also easy to implement, requiring just a single function to handle cleanup. However, it may require additional considerations for asynchronous operations, making it slightly less straightforward than node-cleanup.

  • exit-hook:

    exit-hook is user-friendly and allows you to easily register multiple cleanup functions with minimal configuration. Its straightforward API makes it accessible for developers who want to implement exit handling without extensive setup.

  • node-cleanup:

    node-cleanup is extremely easy to use, requiring only a single function to be registered for cleanup. This simplicity makes it an excellent choice for developers who want a quick solution without additional complexity.

Compatibility

  • signal-exit:

    signal-exit is highly compatible and works across different Node.js environments, including when the process is terminated by signals. This makes it a robust choice for applications that need reliable exit handling.

  • exit-hook:

    exit-hook is compatible with various Node.js versions and works well in most environments, making it a versatile choice for different applications.

  • node-cleanup:

    node-cleanup is designed to work seamlessly with Node.js applications and is compatible with all major versions, ensuring broad usability across different projects.

Documentation and Community Support

  • signal-exit:

    signal-exit has good documentation and is widely used in the Node.js community, ensuring that developers can find resources and examples for implementing exit handling effectively.

  • exit-hook:

    exit-hook has comprehensive documentation and a supportive community, providing examples and guidance for implementing exit handling in various scenarios. This makes it easier for developers to get started and troubleshoot issues.

  • node-cleanup:

    node-cleanup offers straightforward documentation, making it easy for developers to understand how to implement cleanup handling. However, it may have a smaller community compared to the other packages, which can affect the availability of community-driven support.

How to Choose: signal-exit vs exit-hook vs node-cleanup
  • signal-exit:

    Select signal-exit if you need a robust solution that works across different environments, including when the process is terminated by signals. It is best for applications that require reliable handling of exit signals and need to ensure that cleanup tasks are executed even in unexpected termination scenarios.

  • exit-hook:

    Choose exit-hook if you need a simple and effective way to register multiple exit handlers that can run asynchronously. It is ideal for applications that require cleanup tasks to be executed in a specific order before exiting.

  • node-cleanup:

    Opt for node-cleanup if you want a straightforward solution that automatically handles cleanup on process termination. It is particularly useful for applications that need to ensure cleanup is performed without worrying about the order of execution.

README for signal-exit

signal-exit

When you want to fire an event no matter how a process exits:

  • reaching the end of execution.
  • explicitly having process.exit(code) called.
  • having process.kill(pid, sig) called.
  • receiving a fatal signal from outside the process

Use signal-exit.

// Hybrid module, either works
import { onExit } from 'signal-exit'
// or:
// const { onExit } = require('signal-exit')

onExit((code, signal) => {
  console.log('process exited!', code, signal)
})

API

remove = onExit((code, signal) => {}, options)

The return value of the function is a function that will remove the handler.

Note that the function only fires for signals if the signal would cause the process to exit. That is, there are no other listeners, and it is a fatal signal.

If the global process object is not suitable for this purpose (ie, it's unset, or doesn't have an emit method, etc.) then the onExit function is a no-op that returns a no-op remove method.

Options

  • alwaysLast: Run this handler after any other signal or exit handlers. This causes process.emit to be monkeypatched.

Capturing Signal Exits

If the handler returns an exact boolean true, and the exit is a due to signal, then the signal will be considered handled, and will not trigger a synthetic process.kill(process.pid, signal) after firing the onExit handlers.

In this case, it your responsibility as the caller to exit with a signal (for example, by calling process.kill()) if you wish to preserve the same exit status that would otherwise have occurred. If you do not, then the process will likely exit gracefully with status 0 at some point, assuming that no other terminating signal or other exit trigger occurs.

Prior to calling handlers, the onExit machinery is unloaded, so any subsequent exits or signals will not be handled, even if the signal is captured and the exit is thus prevented.

Note that numeric code exits may indicate that the process is already committed to exiting, for example due to a fatal exception or unhandled promise rejection, and so there is no way to prevent it safely.

Browser Fallback

The 'signal-exit/browser' module is the same fallback shim that just doesn't do anything, but presents the same function interface.

Patches welcome to add something that hooks onto window.onbeforeunload or similar, but it might just not be a thing that makes sense there.