Skip to content

Conversation

@m0ppers
Copy link

@m0ppers m0ppers commented Feb 21, 2025

possible fix for #187

The problem - as I understand it - seems to be that the callbacks are used in multiple threads.

First the constructor has the event and error callback as argument.

This is - as far as I understand it - the main thread. The initial usage count for this thread is 1.

Then at some point a watcher is started and the callback is actually used for the first time. This is done in a separate thread it seems.

https://github.com/nodejs/node-addon-api/blob/main/doc/threadsafe_function.md#acquire indicates that it tracks the usage per thread but a bit unclear to me.

So the worker threads acquire the functions and register that another thread is using them.

now when the script in the bug report executes nodejs has to think in the end that the mainthread is still using the callback and will not call the destructor of the object because there is no connection between the stop() function and the object itself. It might possibly be restarted again.

I added a super helpless close() function which Releases the function from the main thread :S

during debugging I also found multiple occasions where mErrorCallback is assumed to be present which is not enforced! I added a few ifs() there.

I am pretty sure this is not a good fix. but everything else would probably involve api changes :S

also fixed multiple occasions where errorCallback must be set
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

Successfully merging this pull request may close these issues.

1 participant