Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: fix crash when .unref() is called during exit #33394

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ void Worker::StopThread(const FunctionCallbackInfo<Value>& args) {
void Worker::Ref(const FunctionCallbackInfo<Value>& args) {
Worker* w;
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
if (!w->has_ref_) {
if (!w->has_ref_ && !w->thread_joined_) {
w->has_ref_ = true;
w->env()->add_refs(1);
}
Expand All @@ -669,7 +669,7 @@ void Worker::Ref(const FunctionCallbackInfo<Value>& args) {
void Worker::Unref(const FunctionCallbackInfo<Value>& args) {
Worker* w;
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
if (w->has_ref_) {
if (w->has_ref_ && !w->thread_joined_) {
w->has_ref_ = false;
w->env()->add_refs(-1);
}
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-worker-unref-from-message-during-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
const common = require('../common');
const { Worker } = require('worker_threads');

addaleax marked this conversation as resolved.
Show resolved Hide resolved
const w = new Worker(`
require('worker_threads').parentPort.postMessage({});
`, { eval: true });
w.on('message', common.mustCall(() => {
w.unref();
}));

// Wait a bit so that the 'message' event is emitted while the Worker exits.
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100);