Skip to content

Commit

Permalink
fixup! worker: improve integration with native addons
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Feb 20, 2019
1 parent a5fc245 commit 2a474aa
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/node_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ int wrapped_dlclose(void* handle) {
Mutex::ScopedLock lock(dlhandles_mutex);
dl_wrap* wrap = static_cast<dl_wrap*>(handle);
int ret = 0;
CHECK_GE(wrap->refcount, 1);
if (--wrap->refcount == 0) {
ret = dlclose(wrap->real_handle);
if (ret != 0) dlerror_storage = dlerror();
Expand All @@ -183,6 +184,8 @@ int wrapped_dlclose(void* handle) {
}

void* wrapped_dlsym(void* handle, const char* symbol) {
if (handle == RTLD_DEFAULT || handle == RTLD_NEXT)
return dlsym(handle, symbol);
dl_wrap* wrap = static_cast<dl_wrap*>(handle);
return dlsym(wrap->real_handle, symbol);
}
Expand Down Expand Up @@ -292,9 +295,17 @@ bool DLib::Open() {

void DLib::Close() {
if (handle_ == nullptr) return;
if (has_entry_in_global_handle_map_)
global_handle_map.erase(handle_);
dlclose(handle_);
int err = dlclose(handle_);

if (err == 0) {
// musl libc implements dlclose() as a no-op which returns 1.
// As a consequence, trying to re-load a previously closed addon at a later
// point will not call its static constructors, which Node.js uses.
// Therefore, when dlclose() fails, we assume that the shared object
// still exists and keep it in our handle map.
if (has_entry_in_global_handle_map_)
global_handle_map.erase(handle_);
}
handle_ = nullptr;
}

Expand Down

0 comments on commit 2a474aa

Please sign in to comment.