Skip to content

Commit 9d1e577

Browse files
addaleaxcodebytere
authored andcommitted
worker: fix crash when .unref() is called during exit
To be more precise, fix a crash when `worker.unref()` is called from a message on the Worker that is not emitted before the Worker thread has stopped. PR-URL: #33394 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5e5aa0b commit 9d1e577

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/node_worker.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ void Worker::StopThread(const FunctionCallbackInfo<Value>& args) {
665665
void Worker::Ref(const FunctionCallbackInfo<Value>& args) {
666666
Worker* w;
667667
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
668-
if (!w->has_ref_) {
668+
if (!w->has_ref_ && !w->thread_joined_) {
669669
w->has_ref_ = true;
670670
w->env()->add_refs(1);
671671
}
@@ -674,7 +674,7 @@ void Worker::Ref(const FunctionCallbackInfo<Value>& args) {
674674
void Worker::Unref(const FunctionCallbackInfo<Value>& args) {
675675
Worker* w;
676676
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
677-
if (w->has_ref_) {
677+
if (w->has_ref_ && !w->thread_joined_) {
678678
w->has_ref_ = false;
679679
w->env()->add_refs(-1);
680680
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Worker } = require('worker_threads');
4+
5+
// This used to crash because the `.unref()` was unexpected while the Worker
6+
// was exiting.
7+
8+
const w = new Worker(`
9+
require('worker_threads').parentPort.postMessage({});
10+
`, { eval: true });
11+
w.on('message', common.mustCall(() => {
12+
w.unref();
13+
}));
14+
15+
// Wait a bit so that the 'message' event is emitted while the Worker exits.
16+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100);

0 commit comments

Comments
 (0)