paddybyers / node

evented I/O for v8 javascript
http://nodejs.org/
Other
91 stars 24 forks source link

Simulate delivery of signals to node #10

Open paddybyers opened 12 years ago

paddybyers commented 12 years ago

When running as a library inside an Android Java application, it is not possible to send signals to node in the usual way without killing the Java VM.

However, there is a desire to be able to programmatically simulate the delivery of these signals. In particular, there needs to be a way of asynchronously terminating an instance from a separate thread while the main thread is blocked inside libev in the event loop.

The proposal is to expose an API at the top--level that calls the relevant functionality in libev.

Suggested commit is here:

https://github.com/paddybyers/node/commit/9f6f9e0c8ce7cd87dbca4d06f4ced07348e9307f

paddybyers commented 12 years ago

It's more difficult than this in general because of the issues of signals and multiple threads. In libev, only one event loop can be associated with any given signal. This means that even with ev_feed_signal you cannot programmatically deliver an simulated signal to an arbitrary loop. It also means that SIGCHILD processing (for child_watcher) is only possible in the default event loop, which isn't good enough for us.

Now asynchronous programmatic termination is being done by a combination of steps.

First, an async watcher is created and initialised when the isolate starts, but the watcher is not started. This means that the existence of the watcher, by itself, does not prevent a normal exit.

Termination performs two distinct operations:

There is possibly an issue here with events that were pending prior to termination of the isolate - I need to check what happens in this case. I think I need a prepare watcher to do the break to guarantee that no max priority pending events can be run.