GeezerVison is showing github.com — worker: fix crash when .unref() is called during exit · nodejs/node@9d1e577
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/node_worker.cc +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -665,7 +665,7 @@ void Worker::StopThread(const FunctionCallbackInfo<Value>& args) { |
665 | 665 | void Worker::Ref(const FunctionCallbackInfo<Value>& args) { |
666 | 666 | Worker* w; |
667 | 667 | ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); |
668 | | - if (!w->has_ref_) { |
| 668 | + if (!w->has_ref_ && !w->thread_joined_) { |
669 | 669 | w->has_ref_ = true; |
670 | 670 | w->env()->add_refs(1); |
671 | 671 | } |
@@ -674,7 +674,7 @@ void Worker::Ref(const FunctionCallbackInfo<Value>& args) { |
674 | 674 | void Worker::Unref(const FunctionCallbackInfo<Value>& args) { |
675 | 675 | Worker* w; |
676 | 676 | ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); |
677 | | - if (w->has_ref_) { |
| 677 | + if (w->has_ref_ && !w->thread_joined_) { |
678 | 678 | w->has_ref_ = false; |
679 | 679 | w->env()->add_refs(-1); |
680 | 680 | } |
|
Collapse file
Copy file name to clipboard +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 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); |