diff --git a/src/env.cc b/src/env.cc index ba5e9cd5a05218..19c29c2fcab950 100644 --- a/src/env.cc +++ b/src/env.cc @@ -698,6 +698,8 @@ void Environment::RunAndClearNativeImmediates(bool only_refed) { if (head->is_refed() || !only_refed) head->Call(this); + head.reset(); // Destroy now so that this is also observed by try_catch. + if (UNLIKELY(try_catch.HasCaught())) { if (!try_catch.HasTerminated() && can_call_into_js()) errors::TriggerUncaughtException(isolate(), try_catch); diff --git a/src/node_worker.cc b/src/node_worker.cc index 785f2783c22346..078b6ac5bbf648 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -438,8 +438,6 @@ void Worker::JoinThread() { } Worker::~Worker() { - JoinThread(); - Mutex::ScopedLock lock(mutex_); CHECK(stopped_); @@ -599,6 +597,7 @@ void Worker::StartThread(const FunctionCallbackInfo& args) { [w = std::unique_ptr(w)](Environment* env) { if (w->has_ref_) env->add_refs(-1); + w->JoinThread(); // implicitly delete w }); }, static_cast(w)), 0); diff --git a/test/parallel/test-worker-exit-event-error.js b/test/parallel/test-worker-exit-event-error.js new file mode 100644 index 00000000000000..e2427c7dff726b --- /dev/null +++ b/test/parallel/test-worker-exit-event-error.js @@ -0,0 +1,8 @@ +'use strict'; +const common = require('../common'); +const { Worker } = require('worker_threads'); + +process.on('uncaughtException', common.mustCall()); + +new Worker('', { eval: true }) + .on('exit', common.mustCall(() => { throw new Error('foo'); }));