-
Notifications
You must be signed in to change notification settings - Fork 285
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
C++ Addon - How to check if running in the main thread or not from a Node.js native addon? #3356
Comments
@JCMais Can you provide a bit more context around what you are looking for exactly? It sounds a bit like you’re not necessarily looking for cleanup on the main-thread level, but for doing cleanup on a process level. In that case, normally I would suggest using static constructors/destructors; however, since you mentioned libcurl, it doesn’t seem like they’re recommending that: https://curl.se/libcurl/c/curl_global_init.html:
https://curl.se/libcurl/c/curl_global_cleanup.html:
I’m surprised to see libcurl being so restrictive, but their docs are basically telling you not to use it from an addon at all. Practically speaking, I think you could probably add a counter that is shared between threads that keeps track of the # of times your addon has been loaded/unloaded and act accordingly? Something like this, basically: class EnsureLibcurlInit {
public:
static void InitLibcurl() {
if (counter++ == 0)
curl_global_init();
}
static void CleanupLibcurl() {
if (--counter == 0)
curl_global_cleanup();
}
private:
static std::atomic_uint32_t counter {0};
};
std::atomic_uint32_t EnsureLibcurlInit::counter {0};
NODE_MODULE_INIT(/* exports, module, context */) {
Isolate* isolate = context->GetIsolate();
EnsureLibcurlInit::InitLibcurl();
AddEnvironmentCleanupHook(isolate, [](void*) {
EnsureLibcurlInit::CleanupLibcurl();
}, nullptr);
// ... initialize JS bindings ...
} |
Hi @addaleax , basically I'm trying to make my library ( Right now So it is easy to make sure that this only runs in the main thread (by checking Now about
I think I missed that part of the documentation, or just ignored it when I first read it. In this case, it seems that it is not feasible to add support for worker threads at all. I will try to get in touch with the maintainers of libcurl and check if this is still the case. In the meantime, ignoring the above point, I will take a look at using a shared counter like you said, thank you for the suggestion! |
That kind of begs the question, what happens if the first time the addon is loaded happens on a worker thread? |
Good thinking, in this case, it is necessary to require the addon or call |
It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. |
It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. |
This is somewhat relevant to #1886
I have an addon that needs to call a cleanup function only in the main thread (libcurl cleanup), currently, the only ways to add a cleanup hook are using
AtExit
orAddEnvironmentCleanupHook
, but those run for each thread, including the main one.Is there any way for me to check if the code is running in the main thread or not?
I see that there is a
is_main_thread()
function available inside theEnvironment
instance (added with Node.js v11): https://github.com/nodejs/node/blob/f607e169612ff60a00f456f3c698a6b41792a8f5/src/env-inl.h#L707-L709However, the add-on code cannot access the Environment class itself, as it is not exposed by Node.js.
Theoretically, I could retrieve this by calling the Node.js
worker_thread
module directly from the addon, but this does not seem right, as it would add unnecessary context-switching.The text was updated successfully, but these errors were encountered: