Description
I spent the last few hours tracking down a deadlock one of my users was running into with my library. It was 100% a bug in my code - but I think napi could pretty easily protect module writers against the mistake I was making.
The problem was it turns out that (sometimes) I've been calling napi_call_threadsafe_function(fn, ctx, napi_tsfn_blocking)
from nodejs's main thread. The problem is if you call that function with napi_tsfn_blocking
when the queue is full, nodejs's main thread will end up deadlocked. (The call to call_threadsafe_function will block until there's room in the queue - but it can't clear room in the queue while its blocking waiting for room.)
Anyway, I think we should add a check in ThreadsafeFunction::Push to return an error if:
- Its called from nodejs's main thread.
- Its called with
napi_tsfn_blocking
, and the queue has limited size
This would have saved me some time; and I suspect I'm not the only one who will accidentally misuse napi_call_threadsafe_function
and land in trouble.
The compatibility constraints on this are interesting - its always wrong to call call_threadsafe_function like this, but many programs will run for awhile anyway, timebomb and all.