Skip to content

Commit 5656ec9

Browse files
addaleaxcodebytere
authored andcommitted
worker: move JoinThread() back into exit callback
de2c68c moved this call to the destructor, under the assumption that that would essentially be equivalent to running it as part of the callback since the worker would be destroyed along with the callback. However, the actual code in `Environment::RunAndClearNativeImmediates()` comes with the subtlety that testing whether a JS exception has been thrown happens between the invocation of the callback and its destruction, leaving a possible exception from `JoinThread()` potentially unhandled (and unintentionally silenced through the `TryCatch`). This affected exceptions thrown from the `'exit'` event of the Worker, and made the `parallel/test-worker-message-type-unknown` test flaky, as the invalid message was sometimes only received during the Worker thread’s exit handler. Fix this by moving the `JoinThread()` call back to where it was before. Refs: #31386 PR-URL: #31468 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
1 parent 3da4d51 commit 5656ec9

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/node_worker.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,6 @@ void Worker::JoinThread() {
438438
}
439439

440440
Worker::~Worker() {
441-
JoinThread();
442-
443441
Mutex::ScopedLock lock(mutex_);
444442

445443
CHECK(stopped_);
@@ -599,6 +597,7 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
599597
[w = std::unique_ptr<Worker>(w)](Environment* env) {
600598
if (w->has_ref_)
601599
env->add_refs(-1);
600+
w->JoinThread();
602601
// implicitly delete w
603602
});
604603
}, static_cast<void*>(w)), 0);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Worker } = require('worker_threads');
4+
5+
process.on('uncaughtException', common.mustCall());
6+
7+
new Worker('', { eval: true })
8+
.on('exit', common.mustCall(() => { throw new Error('foo'); }));

0 commit comments

Comments
 (0)