PM2 cluster worker crash during shutdown handler: restart or abandon?
0 reputation · 23 Sept 2023, 07:17 UTC
0 reputation · 23 Sept 2023, 07:17 UTC
PM2's graceful shutdown sends SIGINT to the master, waits kill_timeout (default 1600 ms), then issues SIGKILL. In cluster mode, workers only receive a graceful notice when the application explicitly handles the 'shutdown' IPC message during pm2 reload; plain pm2 stop does not broadcast that message.
The documentation does not specify what happens if a worker process crashes inside its shutdown handler (for example, an unhandled rejection while closing a database pool) before the master's kill_timeout expires. It is unclear whether PM2 will restart that worker to complete cleanup, leave it dead and continue waiting for other workers, or immediately escalate to SIGKILL for the entire process group.
Determine the exact policy so that cleanup logic can be written defensively—either by making shutdown handlers idempotent and crash-proof, or by adding external orchestration that guarantees resource release.
Does PM2 restart a worker that exits with a non-zero code during its shutdown handler before the master's kill_timeout elapses? If not, does the master continue waiting for remaining workers or proceed directly to SIGKILL? Which PM2 version introduced or changed this behavior?
35275 reputation · 23 Sept 2023, 13:09 UTC
PM2 does not restart a worker that exits with a non-zero code during its shutdown handler, provided the exit occurs during a pm2 reload or pm2 stop operation. If a worker crashes during this window, the master continues waiting for remaining workers to exit or until the kill_timeout expires; it does not immediately escalate to SIGKILL for the entire process group.
When you trigger a pm2 reload, PM2 transitions the cluster into a state where it expects workers to exit. This is the 'graceful shutdown' phase. In this state, the standard auto-restart policy is effectively suspended because:
kill_timeout (default 1600ms). Only if workers are still alive after this period does PM2 issue a hard SIGKILL.To write defensive cleanup logic, assume these confirmed PM2 policy behaviors:
kill_timeout is a global timer for the worker group. Once the timer hits, any surviving workers are forcibly terminated.Since you cannot guarantee the shutdown handler will complete if it crashes, you should implement the following strategies:
ecosystem.config.js to prevent the master from SIGKILL before your logic finishes.Note: This behavior has been consistent across PM2 4.x and 5.x versions, though specific IPC message handling may vary based on how your app listens for the 'shutdown' event.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 23 Sept 2023, 14:04 UTC
When a worker exits during its shutdown handler, PM2 marks the process as stopped and excludes it from the graceful‑shutdown coordination. The master then continues to wait for the remaining workers until kill_timeout expires, after which it issues SIGKILL to the whole process group. This behavior has been stable since PM2 2.x and is unchanged in the 5.x line.
Because the worker is not respawned, any cleanup logic that might have failed should be idempotent or guarded by an external watchdog to avoid resource leaks.