Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! worker: improve integration with native addons
  • Loading branch information
addaleax committed Feb 20, 2019
commit d1b8fd987a785104bef81e0de20677cb38a7c02b
10 changes: 9 additions & 1 deletion src/node_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ static struct global_handle_map_t {
Mutex::ScopedLock lock(mutex_);

map_[handle].module = mod;
// We need to store this flag internally to avoid a chicken-and-egg problem
// during cleanup. By the time we actually use the flag's value,
// the shared object has been unloaded, and its memory would be gone,
// making it impossible to access fields of `mod` --
// unless `mod` *is* dynamically allocated, but we cannot know that
// without checking the flag.
map_[handle].wants_delete_module = mod->nm_flags & NM_F_DELETEME;
map_[handle].refcount++;
}

Expand All @@ -274,7 +281,7 @@ static struct global_handle_map_t {
if (it == map_.end()) return;
CHECK_GE(it->second.refcount, 1);
if (--it->second.refcount == 0) {
if (it->second.module->nm_flags & NM_F_DELETEME)
if (it->second.wants_delete_module)
delete it->second.module;
map_.erase(handle);
}
Expand All @@ -284,6 +291,7 @@ static struct global_handle_map_t {
Mutex mutex_;
struct Entry {
unsigned int refcount;
bool wants_delete_module;
node_module* module;
};
std::unordered_map<void*, Entry> map_;
Expand Down