Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodemon doesn't wait for async graceful exit handlers #2117

Closed
simon-abbott opened this issue Jun 21, 2023 · 18 comments
Closed

Nodemon doesn't wait for async graceful exit handlers #2117

simon-abbott opened this issue Jun 21, 2023 · 18 comments
Labels
stale no activity for 2 weeks

Comments

@simon-abbott
Copy link

  • Versions: node@v18.14.0, darwin@22.5.0
  • nodemon -v: 2.0.22
  • Operating system/terminal environment (powershell, gitshell, etc): Terminal.app on macOS, running zsh
  • Using Docker? What image: No
  • Command you ran: npx nodemon hello.js

Expected behaviour

The process should keep running in the foreground until the shutdown handler is finished.

Actual behaviour

The process continues in the foreground until the first await, then "soft exists" (causes the terminal prompt to be displayed), but doesn't kill the process.

Screen.Recording.2023-06-21.at.10.40.23.AM.mov

Steps to reproduce

Run this file via nodemon, then ctrl+c it to shut it down:

/* file: hello.js */
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const interval = setInterval(() => console.log('tick'), 1000);

async function shutdown() {
  console.log('shutting down');
  await sleep(1000);
  clearInterval(interval);
  console.log('shutdown complete');
  process.exit(0);
}

process.on('SIGHUP', () => shutdown());
process.on('SIGINT', () => shutdown());
process.on('SIGTERM', () => shutdown());
@github-actions
Copy link

github-actions bot commented Jul 5, 2023

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@github-actions github-actions bot added the stale no activity for 2 weeks label Jul 5, 2023
@simon-abbott
Copy link
Author

This is definitely not stale, it's a real pain when running our webserver in dev mode, and it keeps messing with my terminal.

@github-actions github-actions bot removed the stale no activity for 2 weeks label Jul 5, 2023
@github-actions
Copy link

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@github-actions github-actions bot added the stale no activity for 2 weeks label Jul 19, 2023
@simon-abbott
Copy link
Author

Still not fixed, please keep open.

@github-actions github-actions bot removed the stale no activity for 2 weeks label Jul 21, 2023
@remy
Copy link
Owner

remy commented Aug 2, 2023

I've just been giving this a test, and without npx - I get the desired result.

i.e. globally installed, or called directly.

The problem, I'm guessing, is that the signal is caught by npx (or npm when running with npm run X) and then passed to nodemon then passed to the sub process. nodemon intentionally sets timers to check if the process is still running (or needs to timeout), but npx isn't doing that and exits back to the terminal.

I'm not sure what to do here since it's outside of nodemon that the exit to shell is occurring, unless you have any suggestions? (I'd say this is a "not a bug", but it's definitely annoying, I can see that).

@simon-abbott
Copy link
Author

Huh. That's interesting. I get the same result. Running nodemon directly works (though it sends SIGINT 3 times rather than 1 for some reason, but that's a separate issue). However running the test file directly through node or npx also works as expected. It's only when running nodemon through npx / npm run that it fails, so it still feels like it's more likely a nodemon bug to me.

Since npx / npm run work properly on the test file that suggests to me that they know how to be patient with scripts not exiting properly. Perhaps there's some logic bug with use of child_process? That seems the most likely culprit to me. Aside from that I also have nothing to go on without digging in and debugging the actual shutdown process, which I really do not have time for soon, unfortunately.

And before someone suggests "just run nodemon directly then", all the places we call nodemon are in npm scripts in our package.json. Refactoring that is not really an option for us.

@sujeet-agrahari
Copy link

sujeet-agrahari commented Aug 12, 2023

Having the same issue!

❯ pnpm start

> node-fastify-architecture@1.0.0 start /Users/sujeet/github/node-fastify-architecture
> NODE_ENV=development nodemon --require=dotenv/config  src/server.js -L

[nodemon] 3.0.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node --require=dotenv/config src/server.js`
[11:19:13.312] INFO (54382): Database connected!
[11:19:13.345] INFO (54382): 
 └── /
    ├── fastify-overview-ui/
    │   └── * (HEAD, GET)
    ├── json-overview-ui (GET, HEAD)
    ├── api/v1/auth/login (POST)
    └── health (GET, HEAD)

[11:19:13.346] INFO (54382): Server listening at http://127.0.0.1:3000
^C ELIFECYCLE  Command failed.

 ❯ [11:19:16.065] INFO (54382): Got SIGINT! Shutting down gracefully      ✘ INT  4s  system
[11:19:16.075] INFO (54382): Database connection closed!

It just hangs in there and the process never gets closed!

Workaround:
Don't use nodemon, use node --watch instead if you are using node v18

@github-actions
Copy link

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@github-actions github-actions bot added the stale no activity for 2 weeks label Aug 26, 2023
@simon-abbott
Copy link
Author

Please don't close <3

@github-actions github-actions bot removed the stale no activity for 2 weeks label Aug 28, 2023
@github-actions
Copy link

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@github-actions github-actions bot added the stale no activity for 2 weeks label Sep 11, 2023
@simon-abbott
Copy link
Author

Don't close yet please!

@github-actions github-actions bot removed the stale no activity for 2 weeks label Sep 11, 2023
@github-actions
Copy link

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@github-actions github-actions bot added the stale no activity for 2 weeks label Sep 25, 2023
@simon-abbott
Copy link
Author

Still an issue, please don't close!

@github-actions
Copy link

github-actions bot commented Oct 9, 2023

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@github-actions github-actions bot added the stale no activity for 2 weeks label Oct 9, 2023
@simon-abbott
Copy link
Author

Don't close please.

@github-actions github-actions bot removed the stale no activity for 2 weeks label Oct 11, 2023
@remy
Copy link
Owner

remy commented Oct 15, 2023

So the problem is down to the use of npx - as described in an earlier comment. Nodemon can only (currently) offer documented work around - unless someone has a code fix?

I can add it to the faq...

@github-actions
Copy link

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@github-actions github-actions bot added the stale no activity for 2 weeks label Oct 29, 2023
Copy link

Automatically closing this issue due to lack of activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale no activity for 2 weeks
Projects
None yet
Development

No branches or pull requests

3 participants