From bb59bc87cedbad9595a6f63b4d6a36c7195225ce Mon Sep 17 00:00:00 2001 From: Gabriel Bota Date: Mon, 29 Apr 2024 13:16:13 +0200 Subject: [PATCH] add test for process.exit for import running for worker thread --- lib/internal/modules/esm/hooks.js | 2 -- lib/internal/worker.js | 2 +- test/es-module/test-esm-loader-threads.mjs | 36 +++++++++++++++++++ .../es-module-loaders/hooks-exit-worker.mjs | 21 +++++++++++ .../es-module-loaders/worker-fail-on-load.mjs | 1 + .../worker-fail-on-resolve.mjs | 1 + .../worker-log-fail-worker-load.mjs | 12 +++++++ .../worker-log-fail-worker-resolve.mjs | 12 +++++++ 8 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/es-module-loaders/hooks-exit-worker.mjs create mode 100644 test/fixtures/es-module-loaders/worker-fail-on-load.mjs create mode 100644 test/fixtures/es-module-loaders/worker-fail-on-resolve.mjs create mode 100644 test/fixtures/es-module-loaders/worker-log-fail-worker-load.mjs create mode 100644 test/fixtures/es-module-loaders/worker-log-fail-worker-resolve.mjs diff --git a/lib/internal/modules/esm/hooks.js b/lib/internal/modules/esm/hooks.js index 82e94d583c8688..f5833ad61cdb75 100644 --- a/lib/internal/modules/esm/hooks.js +++ b/lib/internal/modules/esm/hooks.js @@ -502,8 +502,6 @@ class HooksProxy { constructor() { const { InternalWorker, hooksPort } = require('internal/worker'); - MessageChannel ??= require('internal/worker/io').MessageChannel; - const lock = new SharedArrayBuffer(SHARED_MEMORY_BYTE_LENGTH); this.#lock = new Int32Array(lock); diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 70b7ffca0465b3..896bbe2ac8c71b 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -297,7 +297,7 @@ class Worker extends EventEmitter { if (!isInternal && hasCustomizations) { // - send the second side of the channel to the hooks thread - loaderModule.getHooksProxy().makeAsyncRequest( + loaderModule.getHooksProxy().makeSyncRequest( '#registerWorkerClient', [toWorkerThread], toWorkerThread); } // Use this to cache the Worker's loopStart value once available. diff --git a/test/es-module/test-esm-loader-threads.mjs b/test/es-module/test-esm-loader-threads.mjs index f99493e124a32c..7310a9ac5b54ac 100644 --- a/test/es-module/test-esm-loader-threads.mjs +++ b/test/es-module/test-esm-loader-threads.mjs @@ -35,4 +35,40 @@ describe('off-thread hooks', { concurrency: true }, () => { strictEqual(code, 0); strictEqual(signal, null); }); + + it('propagates the exit code from worker thread import exiting from resolve hook', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--import', + `data:text/javascript,${encodeURIComponent(` + import { register } from 'node:module'; + register(${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-exit-worker.mjs'))}); + `)}`, + fixtures.path('es-module-loaders/worker-log-fail-worker-resolve.mjs'), + ]); + + strictEqual(stderr, ''); + strictEqual(stdout.split('\n').filter((line) => line.startsWith('resolve process-exit-module-resolve')).length, 1); + strictEqual(code, 42); + strictEqual(signal, null); + }); + + it('propagates the exit code from worker thread import exiting from load hook', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--import', + `data:text/javascript,${encodeURIComponent(` + import { register } from 'node:module'; + register(${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-exit-worker.mjs'))}); + `)}`, + fixtures.path('es-module-loaders/worker-log-fail-worker-load.mjs'), + ]); + + strictEqual(stderr, ''); + strictEqual(stdout.split('\n').filter((line) => line.startsWith('resolve process-exit-module-load')).length, 1); + strictEqual(stdout.split('\n').filter((line) => line.startsWith('load process-exit-on-load:///')).length, 1); + strictEqual(code, 43); + strictEqual(signal, null); + }); + }); diff --git a/test/fixtures/es-module-loaders/hooks-exit-worker.mjs b/test/fixtures/es-module-loaders/hooks-exit-worker.mjs new file mode 100644 index 00000000000000..d499a835e6456c --- /dev/null +++ b/test/fixtures/es-module-loaders/hooks-exit-worker.mjs @@ -0,0 +1,21 @@ +import { writeFileSync } from 'node:fs'; + +export function resolve(specifier, context, next) { + writeFileSync(1, `resolve ${specifier}\n`); + if (specifier === 'process-exit-module-resolve') { + process.exit(42); + } + + if (specifier === 'process-exit-module-load') { + return { __proto__: null, shortCircuit: true, url: 'process-exit-on-load:///' } + } + return next(specifier, context); +} + +export function load(url, context, next) { + writeFileSync(1, `load ${url}\n`); + if (url === 'process-exit-on-load:///') { + process.exit(43); + } + return next(url, context); +} diff --git a/test/fixtures/es-module-loaders/worker-fail-on-load.mjs b/test/fixtures/es-module-loaders/worker-fail-on-load.mjs new file mode 100644 index 00000000000000..46e88664a03c5c --- /dev/null +++ b/test/fixtures/es-module-loaders/worker-fail-on-load.mjs @@ -0,0 +1 @@ +import 'process-exit-module-load'; diff --git a/test/fixtures/es-module-loaders/worker-fail-on-resolve.mjs b/test/fixtures/es-module-loaders/worker-fail-on-resolve.mjs new file mode 100644 index 00000000000000..e8e7adde42585f --- /dev/null +++ b/test/fixtures/es-module-loaders/worker-fail-on-resolve.mjs @@ -0,0 +1 @@ +import 'process-exit-module-resolve'; diff --git a/test/fixtures/es-module-loaders/worker-log-fail-worker-load.mjs b/test/fixtures/es-module-loaders/worker-log-fail-worker-load.mjs new file mode 100644 index 00000000000000..81797da392cb7a --- /dev/null +++ b/test/fixtures/es-module-loaders/worker-log-fail-worker-load.mjs @@ -0,0 +1,12 @@ +import { Worker } from 'worker_threads'; +import { foo } from './module-named-exports.mjs'; + +const workerURLFailOnLoad = new URL('./worker-fail-on-load.mjs', import.meta.url); +console.log(foo); + +// Spawn a worker that will fail to import a dependant module +new Worker(workerURLFailOnLoad); + +process.on('exit', (code) => { + console.log(`process exit code: ${code}`) +}); diff --git a/test/fixtures/es-module-loaders/worker-log-fail-worker-resolve.mjs b/test/fixtures/es-module-loaders/worker-log-fail-worker-resolve.mjs new file mode 100644 index 00000000000000..b5ff238967f4ef --- /dev/null +++ b/test/fixtures/es-module-loaders/worker-log-fail-worker-resolve.mjs @@ -0,0 +1,12 @@ +import { Worker } from 'worker_threads'; +import { foo } from './module-named-exports.mjs'; + +const workerURLFailOnResolve = new URL('./worker-fail-on-resolve.mjs', import.meta.url); +console.log(foo); + +// Spawn a worker that will fail to import a dependant module +new Worker(workerURLFailOnResolve); + +process.on('exit', (code) => { + console.log(`process exit code: ${code}`) +});