Graceful Shutdown for Node.js in two easy steps

Node.js has integrated web server capabilities, but the out of the box settings don’t include a good way to initiate a graceful shutdown. Our experience in hosting a large number of decoupled sites led us to find a simple solution to this problem. This fix will help not only our clients and other Lagoon users, but anyone running Node.js servers in a containerized environment.


During scaling and deployment we’ve noticed errors that shouldn’t happen. After a container sends the signal to shut down a Node.js server, it should have 30 seconds to complete tasks and keep running, but that wasn’t happening in practice. While everything was configured correctly on the Docker side for hosting Node.js, the Node.js server was still shutting down immediately and killing all active connections, resulting in errors.


Step One

It’s pretty simple to tell our Node.js servers to finish all requests before shutting down by adding the following code:



Step Two

But as we discovered, this alone doesn’t always fix the problem. The key is that many Node.js systems are built with Yarn or NPM. These provide package management for Node.js but also script management, meaning we can run applications simply by telling them to start in Dockerfiles. Unfortunately, this means that if Yarn or NPM gets a SIGNT or SIGTEM signal they forward that signal to Node.js but don’t wait for the processes to finish.

There are open support issues for this in both Yarn and NMP but they haven’t been addressed yet.


The real solution is to not use either to start your application but instead to use node directly:

Pairing this with the code in step one, you can ensure a graceful shutdown every time. For even more detail you can read our documentation here or get in touch with us today!


Writer