Skip to content

Commit abe1b67

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 Backport-PR-URL: #66128 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 22f47ff commit abe1b67

2 files changed

Lines changed: 75 additions & 10 deletions

File tree

‎src/api/hooks.cc‎

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

130+
// TODO(addaleax): Having this extra set of data structures is far from
131+
// ideal, but unfortunately the public synchronous cleanup hook API was
132+
// slightly mis-designed; in particular, RemoveEnvironmentCleanupHook() needs
133+
// to keep working when the Isolate either has no active context (such as
134+
// during GC) or that context is associated with another Node.js Environment.
135+
// We should align this with the asynchronous API, which handles this properly
136+
// through an explicit reference to the cleanup hook instead of requiring
137+
// lookups in internal maps.
138+
struct CleanupHookThunk final {
139+
Isolate* isolate;
140+
Environment* env;
141+
CleanupHook fun;
142+
void* arg;
143+
144+
bool operator==(const CleanupHookThunk& other) const {
145+
// `env` is intentionally not part of this comparison
146+
return isolate == other.isolate && fun == other.fun && arg == other.arg;
147+
}
148+
};
149+
struct CleanupHookThunkHash {
150+
size_t operator()(const CleanupHookThunk& thunk) const {
151+
return std::hash<void*>()(thunk.arg);
152+
}
153+
};
154+
using CleanupHookRegistry =
155+
std::unordered_set<CleanupHookThunk, CleanupHookThunkHash>;
156+
static ExclusiveAccess<CleanupHookRegistry> cleanup_hook_registry;
157+
158+
static void CleanupHookThunkRun(void* arg) {
159+
const CleanupHookThunk* thunk = static_cast<CleanupHookThunk*>(arg);
160+
thunk->fun(thunk->arg);
161+
RemoveEnvironmentCleanupHook(thunk->isolate, thunk->fun, thunk->arg);
162+
}
163+
130164
void AddEnvironmentCleanupHook(Isolate* isolate,
131165
CleanupHook fun,
132166
void* arg) {
133167
Environment* env = Environment::GetCurrent(isolate);
134168
CHECK_NOT_NULL(env);
135-
env->AddCleanupHook(fun, arg);
169+
void* wrapped_arg;
170+
{
171+
ExclusiveAccess<CleanupHookRegistry>::Scoped registry(
172+
&cleanup_hook_registry);
173+
auto result = registry->insert({isolate, env, fun, arg});
174+
CHECK(result.second);
175+
wrapped_arg = const_cast<CleanupHookThunk*>(&*result.first);
176+
}
177+
env->AddCleanupHook(CleanupHookThunkRun, wrapped_arg);
136178
}
137179

138180
void RemoveEnvironmentCleanupHook(Isolate* isolate,
139181
CleanupHook fun,
140182
void* arg) {
141-
Environment* env = Environment::GetCurrent(isolate);
142-
CHECK_NOT_NULL(env);
143-
env->RemoveCleanupHook(fun, arg);
183+
CleanupHookThunk thunk;
184+
void* wrapped_arg;
185+
{
186+
ExclusiveAccess<CleanupHookRegistry>::Scoped registry(
187+
&cleanup_hook_registry);
188+
auto result = registry->find({isolate, nullptr, fun, arg});
189+
if (result == registry->end()) return;
190+
wrapped_arg = const_cast<CleanupHookThunk*>(&*result);
191+
thunk = *result;
192+
registry->erase(result);
193+
}
194+
thunk.env->RemoveCleanupHook(CleanupHookThunkRun, wrapped_arg);
144195
}
145196

146197
static void FinishAsyncCleanupHook(void* arg) {

‎test/addons/worker-addon/binding.cc‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,33 @@ void Cleanup(void* str) {
5151
void Initialize(Local<Object> exports,
5252
Local<Value> module,
5353
Local<Context> context) {
54+
Isolate* isolate = context->GetIsolate();
5455
node::AddEnvironmentCleanupHook(
55-
context->GetIsolate(),
56-
Cleanup,
57-
const_cast<void*>(static_cast<const void*>("cleanup")));
58-
node::AddEnvironmentCleanupHook(context->GetIsolate(), Dummy, nullptr);
59-
node::RemoveEnvironmentCleanupHook(context->GetIsolate(), Dummy, nullptr);
56+
isolate, Cleanup, const_cast<void*>(static_cast<const void*>("cleanup")));
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+
}
6074

6175
if (getenv("addExtraItemToEventLoop") != nullptr) {
6276
// Add an item to the event loop that we do not clean up in order to make
6377
// sure that for the main thread, this addon's memory persists even after
6478
// the Environment instance has been destroyed.
6579
static uv_async_t extra_async;
66-
uv_loop_t* loop = node::GetCurrentEventLoop(context->GetIsolate());
80+
uv_loop_t* loop = node::GetCurrentEventLoop(isolate);
6781
int err = uv_async_init(loop, &extra_async, [](uv_async_t*) {});
6882
assert(err == 0);
6983
uv_unref(reinterpret_cast<uv_handle_t*>(&extra_async));

0 commit comments

Comments
 (0)