Skip to content

Commit c5de212

Browse files
addaleaxMylesBorins
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 Backport-PR-URL: #32301 PR-URL: #31468 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
1 parent cb16aab commit c5de212

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
@@ -454,8 +454,6 @@ void Worker::JoinThread() {
454454
}
455455

456456
Worker::~Worker() {
457-
JoinThread();
458-
459457
Mutex::ScopedLock lock(mutex_);
460458

461459
CHECK(stopped_);
@@ -655,6 +653,7 @@ void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
655653
[w = std::unique_ptr<Worker>(w)](Environment* env) {
656654
if (w->has_ref_)
657655
env->add_refs(-1);
656+
w->JoinThread();
658657
// implicitly delete w
659658
});
660659
}, 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)