From 8feec47b872dd3fac5104102d631eb0b57d58937 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 29 Mar 2020 14:58:07 +0200 Subject: [PATCH 1/2] worker: do not emit 'exit' events during process.exit() Do not emit `'exit'` events caused by recursively stopping all running Workers from inside the `process.exit()` call. --- src/env.cc | 1 + .../test-worker-nested-on-process-exit.js | 20 ++++++++++++++++ test/parallel/test-worker-on-process-exit.js | 23 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 test/parallel/test-worker-nested-on-process-exit.js create mode 100644 test/parallel/test-worker-on-process-exit.js diff --git a/src/env.cc b/src/env.cc index 7d76ee8186ad28..d3271d20419ced 100644 --- a/src/env.cc +++ b/src/env.cc @@ -986,6 +986,7 @@ void Environment::Exit(int exit_code) { isolate(), stack_trace_limit(), StackTrace::kDetailed)); } if (is_main_thread()) { + set_can_call_into_js(false); stop_sub_worker_contexts(); DisposePlatform(); exit(exit_code); diff --git a/test/parallel/test-worker-nested-on-process-exit.js b/test/parallel/test-worker-nested-on-process-exit.js new file mode 100644 index 00000000000000..aa544fa3289a11 --- /dev/null +++ b/test/parallel/test-worker-nested-on-process-exit.js @@ -0,0 +1,20 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { Worker, workerData } = require('worker_threads'); + +// Test that 'exit' events for nested Workers are not received when a Worker +// terminates itself through process.exit(). + +if (workerData === null) { + const nestedWorkerExitCounter = new Int32Array(new SharedArrayBuffer(4)); + const w = new Worker(__filename, { workerData: nestedWorkerExitCounter }); + w.on('exit', common.mustCall(() => { + assert.strictEqual(nestedWorkerExitCounter[0], 0); + })); +} else { + const nestedWorker = new Worker('setInterval(() => {}, 100)', { eval: true }); + // The counter should never be increased here. + nestedWorker.on('exit', () => workerData[0]++); + nestedWorker.on('online', () => process.exit()); +} diff --git a/test/parallel/test-worker-on-process-exit.js b/test/parallel/test-worker-on-process-exit.js new file mode 100644 index 00000000000000..64d304376adb10 --- /dev/null +++ b/test/parallel/test-worker-on-process-exit.js @@ -0,0 +1,23 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { spawnSync } = require('child_process'); +const { Worker } = require('worker_threads'); + +// Test that 'exit' events for Workers are not received when the main thread +// terminates itself through process.exit(). + +if (process.argv[2] !== 'child') { + const { + stdout, stderr, status + } = spawnSync(process.execPath, [__filename, 'child'], { encoding: 'utf8' }); + assert.strictEqual(stderr, ''); + assert.strictEqual(stdout, ''); + assert.strictEqual(status, 0); +} else { + const nestedWorker = new Worker('setInterval(() => {}, 100)', { eval: true }); + // This console.log() should never fire. + nestedWorker.on('exit', () => console.log('exit event received')); + nestedWorker.on('online', () => process.exit()); +} + From 6cc7965e8ea3129cf2be8756d153dc2b017c77c6 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 29 Mar 2020 15:13:35 +0200 Subject: [PATCH 2/2] fixup! worker: do not emit 'exit' events during process.exit() --- test/parallel/test-worker-on-process-exit.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/parallel/test-worker-on-process-exit.js b/test/parallel/test-worker-on-process-exit.js index 64d304376adb10..ec1c4affd1cc6f 100644 --- a/test/parallel/test-worker-on-process-exit.js +++ b/test/parallel/test-worker-on-process-exit.js @@ -1,5 +1,5 @@ 'use strict'; -const common = require('../common'); +require('../common'); const assert = require('assert'); const { spawnSync } = require('child_process'); const { Worker } = require('worker_threads'); @@ -20,4 +20,3 @@ if (process.argv[2] !== 'child') { nestedWorker.on('exit', () => console.log('exit event received')); nestedWorker.on('online', () => process.exit()); } -