Skip to content

Commit 1723773

Browse files
addaleaxaduh95
authored andcommitted
src: keep global list of addon-provided cleanup hooks
A recent change, 215027c, introduced flakiness into our test suite that exposed an issue with the cleanup hook API design. Specifically, the signatures of `AddEnvironmentCleanupHook()` and `RemoveEnvironmentCleanupHook()` are problematic. Both functions take `Isolate*` arguments, as addons are not generally expected to have to care about the Node.js `Environment` as a first-class scope provider. However, this model made the incorrect assumption that in the situations in which `RemoveEnvironmentCleanupHook()` would be invoked an `Environment` would always be associated with the current `Isolate` (via the current V8 `Context`, if there is one). This occasionally breaks down when `RemoveEnvironmentCleanupHook()` is called during garbage collection -- which would be an expected use case of the functionality, but one that has not been covered through our tests before 215027c. Since Node.js guarantees API and ABI stability within a major version, and this is a bug that is independent from the aforementioned change, this commit resolves it by adding global mutable state to keep track off cleanup hooks registered through the Node.js public API. Obviously, this solution does not represent a desirable long-term state, and a semver-minor follow up should add an API that does not require modifications to these data structures, likely based on the async cleanup hook API which already solves this issue properly. Refs: #63642 Fixes: #63923 Signed-off-by: Anna Henningsen <anna@addaleax.net> PR-URL: #63985 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent ea63005 commit 1723773

2 files changed

Lines changed: 72 additions & 6 deletions

File tree

src/api/hooks.cc

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,71 @@ struct ACHHandle final {
115115
// this.
116116
void DeleteACHHandle::operator ()(ACHHandle* handle) const { delete handle; }
117117

118+
// TODO(addaleax): Having this extra set of data structures is far from
119+
// ideal, but unfortunately the public synchronous cleanup hook API was
120+
// slightly mis-designed; in particular, RemoveEnvironmentCleanupHook() needs
121+
// to keep working when the Isolate either has no active context (such as
122+
// during GC) or that context is associated with another Node.js Environment.
123+
// We should align this with the asynchronous API, which handles this properly
124+
// through an explicit reference to the cleanup hook instead of requiring
125+
// lookups in internal maps.
126+
struct CleanupHookThunk final {
127+
Isolate* isolate;
128+
Environment* env;
129+
CleanupHook fun;
130+
void* arg;
131+
132+
bool operator==(const CleanupHookThunk& other) const {
133+
// `env` is intentionally not part of this comparison
134+
return isolate == other.isolate && fun == other.fun && arg == other.arg;
135+
}
136+
};
137+
struct CleanupHookThunkHash {
138+
size_t operator()(const CleanupHookThunk& thunk) const {
139+
return std::hash<void*>()(thunk.arg);
140+
}
141+
};
142+
using CleanupHookRegistry =
143+
std::unordered_set<CleanupHookThunk, CleanupHookThunkHash>;
144+
static ExclusiveAccess<CleanupHookRegistry> cleanup_hook_registry;
145+
146+
static void CleanupHookThunkRun(void* arg) {
147+
const CleanupHookThunk* thunk = static_cast<CleanupHookThunk*>(arg);
148+
thunk->fun(thunk->arg);
149+
RemoveEnvironmentCleanupHook(thunk->isolate, thunk->fun, thunk->arg);
150+
}
151+
118152
void AddEnvironmentCleanupHook(Isolate* isolate,
119153
CleanupHook fun,
120154
void* arg) {
121155
Environment* env = Environment::GetCurrent(isolate);
122156
CHECK_NOT_NULL(env);
123-
env->AddCleanupHook(fun, arg);
157+
void* wrapped_arg;
158+
{
159+
ExclusiveAccess<CleanupHookRegistry>::Scoped registry(
160+
&cleanup_hook_registry);
161+
auto result = registry->insert({isolate, env, fun, arg});
162+
CHECK(result.second);
163+
wrapped_arg = const_cast<CleanupHookThunk*>(&*result.first);
164+
}
165+
env->AddCleanupHook(CleanupHookThunkRun, wrapped_arg);
124166
}
125167

126168
void RemoveEnvironmentCleanupHook(Isolate* isolate,
127169
CleanupHook fun,
128170
void* arg) {
129-
Environment* env = Environment::GetCurrent(isolate);
130-
CHECK_NOT_NULL(env);
131-
env->RemoveCleanupHook(fun, arg);
171+
CleanupHookThunk thunk;
172+
void* wrapped_arg;
173+
{
174+
ExclusiveAccess<CleanupHookRegistry>::Scoped registry(
175+
&cleanup_hook_registry);
176+
auto result = registry->find({isolate, nullptr, fun, arg});
177+
if (result == registry->end()) return;
178+
wrapped_arg = const_cast<CleanupHookThunk*>(&*result);
179+
thunk = *result;
180+
registry->erase(result);
181+
}
182+
thunk.env->RemoveCleanupHook(CleanupHookThunkRun, wrapped_arg);
132183
}
133184

134185
static void FinishAsyncCleanupHook(void* arg) {

test/addons/worker-addon/binding.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,23 @@ void Initialize(Local<Object> exports,
5454
Isolate* isolate = Isolate::GetCurrent();
5555
node::AddEnvironmentCleanupHook(
5656
isolate, Cleanup, const_cast<void*>(static_cast<const void*>("cleanup")));
57-
node::AddEnvironmentCleanupHook(isolate, Dummy, nullptr);
58-
node::RemoveEnvironmentCleanupHook(isolate, Dummy, nullptr);
57+
58+
// Test that adding and removing a cleanup hook works as expected
59+
{
60+
node::AddEnvironmentCleanupHook(isolate, Dummy, nullptr);
61+
node::RemoveEnvironmentCleanupHook(isolate, Dummy, nullptr);
62+
}
63+
64+
// Test that adding and removing a cleanup hook also works if there
65+
// is no active context during removal
66+
{
67+
node::AddEnvironmentCleanupHook(isolate, Dummy, nullptr);
68+
{
69+
context->Exit();
70+
node::RemoveEnvironmentCleanupHook(isolate, Dummy, nullptr);
71+
context->Enter();
72+
}
73+
}
5974

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

0 commit comments

Comments
 (0)