Skip to content

Commit 0934abd

Browse files
committed
src: restrict unloading addons to Worker threads
Unloading native addons from the main thread was an (presumably unintended) significant breaking change, because addons may rely on their memory being available after an `Environment` exits. This patch only restricts this to Worker threads, at least for the time being, and thus matches the behaviour from nodejs#23319. Refs: nodejs#24861 Refs: nodejs#23319
1 parent 38ab1e9 commit 0934abd

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/env.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,11 @@ Environment::~Environment() {
276276
TRACE_EVENT_NESTABLE_ASYNC_END0(
277277
TRACING_CATEGORY_NODE1(environment), "Environment", this);
278278

279-
// Dereference all addons that were loaded into this environment.
280-
for (binding::DLib& addon : loaded_addons_) {
281-
addon.Close();
279+
if (!is_main_thread()) {
280+
// Dereference all addons that were loaded into this environment.
281+
for (binding::DLib& addon : loaded_addons_) {
282+
addon.Close();
283+
}
282284
}
283285
}
284286

test/addons/worker-addon/binding.cc

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stdio.h>
44
#include <stdlib.h>
55
#include <v8.h>
6+
#include <uv.h>
67

78
using v8::Context;
89
using v8::HandleScope;
@@ -41,6 +42,16 @@ void Initialize(Local<Object> exports,
4142
const_cast<void*>(static_cast<const void*>("cleanup")));
4243
node::AddEnvironmentCleanupHook(context->GetIsolate(), Dummy, nullptr);
4344
node::RemoveEnvironmentCleanupHook(context->GetIsolate(), Dummy, nullptr);
45+
46+
if (getenv("addExtraItemToEventLoop") != nullptr) {
47+
// Add an item to the event loop that we do not clean up in order to make
48+
// sure that for the main thread, this addon's memory persists even after
49+
// the Environment instance has been destroyed.
50+
static uv_async_t extra_async;
51+
uv_loop_t* loop = node::GetCurrentEventLoop(context->GetIsolate());
52+
uv_async_init(loop, &extra_async, [](uv_async_t*) {});
53+
uv_unref(reinterpret_cast<uv_handle_t*>(&extra_async));
54+
}
4455
}
4556

4657
NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize)

test/addons/worker-addon/test.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ const path = require('path');
66
const { Worker } = require('worker_threads');
77
const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
88

9-
if (process.argv[2] === 'child') {
9+
if (process.argv[2] === 'worker') {
1010
new Worker(`require(${JSON.stringify(binding)});`, { eval: true });
11-
} else {
11+
return;
12+
} else if (process.argv[2] === 'main-thread') {
13+
process.env.addExtraItemToEventLoop = 'yes';
14+
require(binding);
15+
return;
16+
}
17+
18+
for (const test of ['worker', 'main-thread']) {
1219
const proc = child_process.spawnSync(process.execPath, [
1320
__filename,
14-
'child'
21+
test
1522
]);
1623
assert.strictEqual(proc.stderr.toString(), '');
1724
assert.strictEqual(proc.stdout.toString(), 'ctor cleanup dtor');

0 commit comments

Comments
 (0)