question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nodemon doesn't wait for `SIGUSR2` graceful shutdown?

See original GitHub issue
  • nodemon -v: 2.0.9
  • node -v: v16.1.0
  • Operating system/terminal environment: macOS Catalina, iTerm 2
  • Using Docker? What image: Yes, but I have a non-docker breaking example
  • Command you ran: yarn run nodemon watch.js

I’m trying to write a shutdown handler to gracefully close my mariadb sql client pool, but it never works because the script is killed too soon. I copied the example from the README and changed the signal to SIGTERM but it never lets the shutdown function finish.

If I manually send a kill -s SIGUSR2 <pid> then it will wait for the shutdown script, but nodemon doesn’t wait.

My shutdown function uses async/await, but I was able to reproduce with out it. All the below info applies to async/await too (meaning it works with manual kill but not nodemon).

Expected behaviour

The script doesn’t get killed by nodemon before it can gracefully shut itself down

Actual behaviour

Nodemon kills the script before my SIGUSR2 script can finish

Steps to reproduce

create a file watch.js:

process.stdin.read();

process.once('SIGUSR2', (signal) => {
  for (let i = 1; i <= 10000; i++) {
    console.log(i);
  }

  process.kill(process.pid, signal);
});

Save watch.js to trigger a restart and notice that the logs never reach 10,000 (for me it never goes past 4k). Send a manual kill -s SIGUSR2 <pid> and notice that the logs go to 10k, the script ends and then nodemon restarts it.


If applicable, please append the --dump flag on your command and include the output here ensuring to remove any sensitive/personal details or tokens.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
rocketeerbkwcommented, Jul 11, 2021

I used git bisect and determined that this is the breaking commit: 47dfb8be3ff06184793a55d32db4b6171fa2993f.

The biggest change I see in that commit is the addition of child.kill(signal); before it calls waitForSubProcesses. If I commend out that line on master then nodemon waits for my SIGUSR2 function to complete as I expect.

2reactions
leppaottcommented, Jun 18, 2022

@lukepolo downgrade nodemon to 2.0.1 same for nodemon-webpack-plugin:

    "nodemon": "2.0.1",
    "nodemon-webpack-plugin": "4.2.1",

After this my above example correctly waits for the cleanup: Change process.once into process.on

start
[nodemon] still waiting for 1 subprocess(es) to finish...
[nodemon] still waiting for 1 subprocess(es) to finish...
end

@remy do you have time for this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nodemon doesn't wait for `SIGUSR2` graceful shutdown?
Nodemon doesn't wait for `SIGUSR2 ` graceful shutdown?
Read more >
Graceful shutdown with nodemon and express - Stack Overflow
I've commented which ones. Apparently, nodemon sends the SIGUSR2 signal when it is restarting, but I tried adding an event to that as...
Read more >
nodemon - npm
If your script exits cleanly, nodemon will continue to monitor the directory (or directories) and restart the script if there are any changes....
Read more >
Automatically Restart Node.js Apps with Nodemon - Morioh
nodemon does not require any additional changes to your code or method of development. ... process.once('SIGUSR2', function () { gracefulShutdown(function ...
Read more >
api documentation for nodemon (v1.11.0)
emit('exit'); // exit the monitor, but do it gracefully if (signal === config.signal) { return restart(); } else if (code === 0) {...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found