Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
59 changes: 55 additions & 4 deletions src/api/hooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,71 @@ struct ACHHandle final {
// this.
void DeleteACHHandle::operator ()(ACHHandle* handle) const { delete handle; }

// TODO(addaleax): Having this extra set of data structures is far from
// ideal, but unfortunately the public synchronous cleanup hook API was
// slightly mis-designed; in particular, RemoveEnvironmentCleanupHook() needs
// to keep working when the Isolate either has no active context (such as
// during GC) or that context is associated with another Node.js Environment.
// We should align this with the asynchronous API, which handles this properly
// through an explicit reference to the cleanup hook instead of requiring
// lookups in internal maps.
struct CleanupHookThunk final {
Isolate* isolate;
Environment* env;
CleanupHook fun;
void* arg;

bool operator==(const CleanupHookThunk& other) const {
// `env` is intentionally not part of this comparison
return isolate == other.isolate && fun == other.fun && arg == other.arg;
}
};
struct CleanupHookThunkHash {
size_t operator()(const CleanupHookThunk& thunk) const {
return std::hash<void*>()(thunk.arg);
}
};
using CleanupHookRegistry =
std::unordered_set<CleanupHookThunk, CleanupHookThunkHash>;
static ExclusiveAccess<CleanupHookRegistry> cleanup_hook_registry;

static void CleanupHookThunkRun(void* arg) {
const CleanupHookThunk* thunk = static_cast<CleanupHookThunk*>(arg);
thunk->fun(thunk->arg);
RemoveEnvironmentCleanupHook(thunk->isolate, thunk->fun, thunk->arg);
}

void AddEnvironmentCleanupHook(Isolate* isolate,
CleanupHook fun,
void* arg) {
Environment* env = Environment::GetCurrent(isolate);
CHECK_NOT_NULL(env);
env->AddCleanupHook(fun, arg);
void* wrapped_arg;
{
ExclusiveAccess<CleanupHookRegistry>::Scoped registry(
&cleanup_hook_registry);
auto result = registry->insert({isolate, env, fun, arg});
CHECK(result.second);
wrapped_arg = const_cast<CleanupHookThunk*>(&*result.first);
}
env->AddCleanupHook(CleanupHookThunkRun, wrapped_arg);
}

void RemoveEnvironmentCleanupHook(Isolate* isolate,
CleanupHook fun,
void* arg) {
Environment* env = Environment::GetCurrent(isolate);
CHECK_NOT_NULL(env);
env->RemoveCleanupHook(fun, arg);
CleanupHookThunk thunk;
void* wrapped_arg;
{
ExclusiveAccess<CleanupHookRegistry>::Scoped registry(
&cleanup_hook_registry);
auto result = registry->find({isolate, nullptr, fun, arg});
if (result == registry->end()) return;
wrapped_arg = const_cast<CleanupHookThunk*>(&*result);
thunk = *result;
registry->erase(result);
}
thunk.env->RemoveCleanupHook(CleanupHookThunkRun, wrapped_arg);
}

static void FinishAsyncCleanupHook(void* arg) {
Expand Down
19 changes: 17 additions & 2 deletions test/addons/worker-addon/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,23 @@ void Initialize(Local<Object> exports,
Isolate* isolate = Isolate::GetCurrent();
node::AddEnvironmentCleanupHook(
isolate, Cleanup, const_cast<void*>(static_cast<const void*>("cleanup")));
node::AddEnvironmentCleanupHook(isolate, Dummy, nullptr);
node::RemoveEnvironmentCleanupHook(isolate, Dummy, nullptr);

// Test that adding and removing a cleanup hook works as expected
{
node::AddEnvironmentCleanupHook(isolate, Dummy, nullptr);
node::RemoveEnvironmentCleanupHook(isolate, Dummy, nullptr);
}

// Test that adding and removing a cleanup hook also works if there
// is no active context during removal
{
node::AddEnvironmentCleanupHook(isolate, Dummy, nullptr);
{
context->Exit();
node::RemoveEnvironmentCleanupHook(isolate, Dummy, nullptr);
context->Enter();
}
}

if (getenv("addExtraItemToEventLoop") != nullptr) {
// Add an item to the event loop that we do not clean up in order to make
Expand Down
Loading