Skip to content

Commit ed9990d

Browse files
committed
test: reduce flakiness of different-registry-per-thread
There was an assumption that the `WeakRef` would be kept alive but it seems to not always be true on Windows. This commit makes sure it's kept alive.
1 parent bbf51ad commit ed9990d

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import { isMainThread, Worker } from 'node:worker_threads';
22

3+
const registeredRefs = [];
4+
5+
const ref = { foo: 'foo' };
6+
registeredRefs.push(ref);
7+
38
if (isMainThread) {
4-
process.finalization.register({ foo: 'foo' }, () => {
9+
process.finalization.register(ref, () => {
510
process.stdout.write('shutdown on main thread\n');
611
});
712

813
const worker = new Worker(import.meta.filename);
914

15+
worker.on('error', (err) => {
16+
// Referencing `registeredRefs` here to avoid `ref` being GCed before the worker exits.
17+
console.log(registeredRefs);
18+
throw err;
19+
});
20+
1021
worker.postMessage('ping');
1122
} else {
12-
process.finalization.register({ foo: 'bar' }, () => {
23+
process.finalization.register(ref, () => {
1324
process.stdout.write('shutdown on worker\n');
1425
});
1526
}

0 commit comments

Comments
 (0)