Skip to content

Commit bb59bc8

Browse files
committed
add test for process.exit for import running for worker thread
1 parent ba9529e commit bb59bc8

File tree

8 files changed

+84
-3
lines changed

8 files changed

+84
-3
lines changed

lib/internal/modules/esm/hooks.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,6 @@ class HooksProxy {
502502

503503
constructor() {
504504
const { InternalWorker, hooksPort } = require('internal/worker');
505-
MessageChannel ??= require('internal/worker/io').MessageChannel;
506-
507505
const lock = new SharedArrayBuffer(SHARED_MEMORY_BYTE_LENGTH);
508506
this.#lock = new Int32Array(lock);
509507

lib/internal/worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class Worker extends EventEmitter {
297297

298298
if (!isInternal && hasCustomizations) {
299299
// - send the second side of the channel to the hooks thread
300-
loaderModule.getHooksProxy().makeAsyncRequest(
300+
loaderModule.getHooksProxy().makeSyncRequest(
301301
'#registerWorkerClient', [toWorkerThread], toWorkerThread);
302302
}
303303
// Use this to cache the Worker's loopStart value once available.

test/es-module/test-esm-loader-threads.mjs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,40 @@ describe('off-thread hooks', { concurrency: true }, () => {
3535
strictEqual(code, 0);
3636
strictEqual(signal, null);
3737
});
38+
39+
it('propagates the exit code from worker thread import exiting from resolve hook', async () => {
40+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
41+
'--no-warnings',
42+
'--import',
43+
`data:text/javascript,${encodeURIComponent(`
44+
import { register } from 'node:module';
45+
register(${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-exit-worker.mjs'))});
46+
`)}`,
47+
fixtures.path('es-module-loaders/worker-log-fail-worker-resolve.mjs'),
48+
]);
49+
50+
strictEqual(stderr, '');
51+
strictEqual(stdout.split('\n').filter((line) => line.startsWith('resolve process-exit-module-resolve')).length, 1);
52+
strictEqual(code, 42);
53+
strictEqual(signal, null);
54+
});
55+
56+
it('propagates the exit code from worker thread import exiting from load hook', async () => {
57+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
58+
'--no-warnings',
59+
'--import',
60+
`data:text/javascript,${encodeURIComponent(`
61+
import { register } from 'node:module';
62+
register(${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-exit-worker.mjs'))});
63+
`)}`,
64+
fixtures.path('es-module-loaders/worker-log-fail-worker-load.mjs'),
65+
]);
66+
67+
strictEqual(stderr, '');
68+
strictEqual(stdout.split('\n').filter((line) => line.startsWith('resolve process-exit-module-load')).length, 1);
69+
strictEqual(stdout.split('\n').filter((line) => line.startsWith('load process-exit-on-load:///')).length, 1);
70+
strictEqual(code, 43);
71+
strictEqual(signal, null);
72+
});
73+
3874
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { writeFileSync } from 'node:fs';
2+
3+
export function resolve(specifier, context, next) {
4+
writeFileSync(1, `resolve ${specifier}\n`);
5+
if (specifier === 'process-exit-module-resolve') {
6+
process.exit(42);
7+
}
8+
9+
if (specifier === 'process-exit-module-load') {
10+
return { __proto__: null, shortCircuit: true, url: 'process-exit-on-load:///' }
11+
}
12+
return next(specifier, context);
13+
}
14+
15+
export function load(url, context, next) {
16+
writeFileSync(1, `load ${url}\n`);
17+
if (url === 'process-exit-on-load:///') {
18+
process.exit(43);
19+
}
20+
return next(url, context);
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'process-exit-module-load';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'process-exit-module-resolve';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Worker } from 'worker_threads';
2+
import { foo } from './module-named-exports.mjs';
3+
4+
const workerURLFailOnLoad = new URL('./worker-fail-on-load.mjs', import.meta.url);
5+
console.log(foo);
6+
7+
// Spawn a worker that will fail to import a dependant module
8+
new Worker(workerURLFailOnLoad);
9+
10+
process.on('exit', (code) => {
11+
console.log(`process exit code: ${code}`)
12+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Worker } from 'worker_threads';
2+
import { foo } from './module-named-exports.mjs';
3+
4+
const workerURLFailOnResolve = new URL('./worker-fail-on-resolve.mjs', import.meta.url);
5+
console.log(foo);
6+
7+
// Spawn a worker that will fail to import a dependant module
8+
new Worker(workerURLFailOnResolve);
9+
10+
process.on('exit', (code) => {
11+
console.log(`process exit code: ${code}`)
12+
});

0 commit comments

Comments
 (0)