Unitech / Unitech/pm2

Occasionally pm2 managed script will error and pm2 will attempt to automatically restart, but forked child processes are not getting killed. How can I make sure they are killed before pm2 attempts to restart the main script?

Open
#5,290 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
43.3k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

I use pm2 to run my custom-written media server on my server.
It runs round the clock all day, and it's difficult to test locally for such long periods. So every now and then it will error with some bad code I've written, which I will promptly fix.

I want it to restart upon erroring, which it does...
But my script also calls child_process.fork on a few other scripts to run under my main app, and these processes need to communicate between one another with IPC messaging.

When I encounter an error in the main process, the child processes get disconnected from the main process and remain running. So pm2 tries to restart the main script (and one of these forked scripts requires a specific port not to be occupied), then it runs into trouble again.

I have to manually end the floating processes and then run pm2 restart <script>

So what's the solution? (besides write better code)

I read that process's uncaughtException event should only be used as a last resort... which I believe this is - so if I set up a listener in the main script with a function to terminate the child processes with force, will pm2 wait for them to close before starting the main process again?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading PM2's restart and child-process handling behavior alongside Node.js child_process.fork and IPC documentation. Reproduce the failure with a forked child that holds a port, then verify that all child processes are terminated and the port is released before PM2 restarts the main script.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend, devops
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.