From e629366fc6c41b81e554fea0413ad1fac53adf36 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 2 Apr 2020 23:43:20 +0200 Subject: [PATCH] src,test: add regression test for nested Worker termination This adds a regression test for terminating a Worker inside which another Worker is running. PR-URL: https://github.com/nodejs/node/pull/32623 Refs: https://github.com/nodejs/node/pull/32531 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Gireesh Punathil --- src/env.cc | 2 ++ test/parallel/test-worker-terminate-nested.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 test/parallel/test-worker-terminate-nested.js diff --git a/src/env.cc b/src/env.cc index 3d8d69238ccbee..bee70ae58e22f6 100644 --- a/src/env.cc +++ b/src/env.cc @@ -1014,6 +1014,8 @@ void Environment::Exit(int exit_code) { } void Environment::stop_sub_worker_contexts() { + DCHECK_EQ(Isolate::GetCurrent(), isolate()); + while (!sub_worker_contexts_.empty()) { Worker* w = *sub_worker_contexts_.begin(); remove_sub_worker_context(w); diff --git a/test/parallel/test-worker-terminate-nested.js b/test/parallel/test-worker-terminate-nested.js new file mode 100644 index 00000000000000..3924528cea1ccb --- /dev/null +++ b/test/parallel/test-worker-terminate-nested.js @@ -0,0 +1,15 @@ +'use strict'; +const common = require('../common'); +const { Worker } = require('worker_threads'); + +// Check that a Worker that's running another Worker can be terminated. + +const worker = new Worker(` +const { Worker, parentPort } = require('worker_threads'); +const worker = new Worker('setInterval(() => {}, 10);', { eval: true }); +worker.on('online', () => { + parentPort.postMessage({}); +}); +`, { eval: true }); + +worker.on('message', common.mustCall(() => worker.terminate()));