Skip to content

Commit 180906e

Browse files
committed
test: replace TS race condition PoC with deterministic JS test
Replace the heavy .mts proof-of-concept test with a lightweight deterministic .js test as suggested by @Renegade334. The new test uses SharedArrayBuffer and Atomics to directly simulate a worker calling process.cwd() during a slow chdir syscall, rather than pummeling workers to force race conditions. This is faster, more reliable, and follows Node.js test conventions.
1 parent 7b093bf commit 180906e

2 files changed

Lines changed: 64 additions & 226 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Flags: --expose-internals --no-warnings
2+
'use strict';
3+
4+
const common = require('../common');
5+
const { internalBinding } = require('internal/test/binding');
6+
7+
const assert = require('assert');
8+
const { Worker } = require('worker_threads');
9+
10+
const processBinding = internalBinding('process_methods');
11+
const originalChdir = processBinding.chdir;
12+
13+
const cwdOriginal = process.cwd();
14+
const i32 = new Int32Array(new SharedArrayBuffer(12));
15+
16+
processBinding.chdir = common.mustCall(function chdir(path) {
17+
// Signal to the worker that we're inside the chdir call
18+
Atomics.store(i32, 0, 1);
19+
Atomics.notify(i32, 0);
20+
21+
// Pause the chdir call while the worker calls process.cwd(),
22+
// to simulate a race condition
23+
Atomics.wait(i32, 1, 0);
24+
25+
return originalChdir(path);
26+
});
27+
28+
const worker = new Worker(`
29+
const {
30+
parentPort,
31+
workerData: { i32 },
32+
} = require('worker_threads');
33+
34+
// Wait until the main thread has entered the chdir call
35+
Atomics.wait(i32, 0, 0);
36+
37+
const cwdDuringChdir = process.cwd();
38+
39+
// Signal the main thread to continue the chdir call
40+
Atomics.store(i32, 1, 1);
41+
Atomics.notify(i32, 1);
42+
43+
// Wait until the main thread has left the chdir call
44+
Atomics.wait(i32, 2, 0);
45+
46+
const cwdAfterChdir = process.cwd();
47+
parentPort.postMessage({ cwdDuringChdir, cwdAfterChdir });
48+
`, {
49+
eval: true,
50+
workerData: { i32 },
51+
});
52+
53+
worker.on('exit', common.mustCall());
54+
worker.on('error', common.mustNotCall());
55+
worker.on('message', common.mustCall(({ cwdDuringChdir, cwdAfterChdir }) => {
56+
assert.strictEqual(cwdDuringChdir, cwdOriginal);
57+
assert.strictEqual(cwdAfterChdir, process.cwd());
58+
}));
59+
60+
process.chdir('..');
61+
62+
// Signal to the worker that the chdir call is completed
63+
Atomics.store(i32, 2, 1);
64+
Atomics.notify(i32, 2);

test/parallel/test-worker-cwd-race-condition.mts

Lines changed: 0 additions & 226 deletions
This file was deleted.

0 commit comments

Comments
 (0)