nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.44k stars 276 forks source link

It would be nice to know when node wants to exit #4311

Closed d3x0r closed 3 months ago

d3x0r commented 6 months ago

Details

I recently added persistence to database connections, which, when opening the connection, the JS code registers a callback to provide initialization for a newly opened connection. This requires a uv_async_loop to be created, but I don't know when to close it. I have similiar issues with GUI applications. The extra event loops prevent Node from closing, and it would be nice to know when Node is considering closing, so I can close the loops, which would then allow Node to complete exiting.

Maybe a method to register the event loops as a weak reference? I suppose it could be a rather noisy process if the process was more like a webserver where the event loop is keeping node alive indefinitely.

Node.js version

Any?

Example code

It's not really practical to include example code, since it requires a specific native addon.

Operating system

Any

Scope

Other?

Module and version

Not applicable.

d3x0r commented 3 months ago

After digging a little I found I can use uv_unref() to make the async handles not count as 'Active' in a queue. This allows Node to exit even while these events are scheduled and available to be triggered to run.

It doesn't solve the 'node wants to exit' issue, but does solve the real issue, which was that some events don't know when they can be closed, and those event handles prevented node from exiting, and required special code to be run by the user of an addon in order to gracefully exit.