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

How to make Node.js exit regardless of a native module async call pending? #2218

Closed
o2genum opened this issue Oct 4, 2019 · 3 comments
Closed

Comments

@o2genum
Copy link

o2genum commented Oct 4, 2019

  • Node.js Version: 10.11.0
  • OS: Windows 10
  • Scope: native modules

I'm writing a native module node-winapi. I'm trying to implement an asynchronous function WaitForProcessToExit(pid), which infinitely waits for a certain process to terminate. I'm using Napi::AsyncWorker.

Everything works as intended, with a caveat: this async call prevents Node.js from exiting. I want Node.js to exit when it has nothing else to do, like it does with child_process.unref(). How do I do that?

@o2genum
Copy link
Author

o2genum commented Oct 4, 2019

I saw lots of cool native modules by @addaleax
Sorry for annoyance, maybe you know if such thing is possible?

@addaleax
Copy link
Member

addaleax commented Oct 4, 2019

@o2genum Napi::AsyncWorker doesn’t really support this – it’s built more with CPU-bound functions that eventually come to an end in mind.

I’m afraid you might have to use libuv directly here; calling uv_unref() on an uv_async_t would do the same thing as child_process.unref(), and you may have to create a separate thread for calling WaitForProcessToExit() manually. In order to cancel the call when Node.js exits, I think you could then register a N-API cleanup hook that stops the thread manually (which iirc works well for Windows).

I hope this helps, but feel free to follow up :)

@o2genum
Copy link
Author

o2genum commented Oct 12, 2019

Ok, I'm posting the link to my solution. Hope it helps someone.
https://stackoverflow.com/a/58356953/633969

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

No branches or pull requests

2 participants