Skip to content

Commit 91b4ce3

Browse files
authored
Fix thread synchronization issues in @babel/register (#17915)
fix
1 parent b0e3517 commit 91b4ce3

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

‎packages/babel-register/src/worker-client.cts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,21 @@ class WorkerClient extends Client {
4747
{ env: markInRegisterWorker(process.env) },
4848
);
4949

50-
#signal = new Int32Array(new SharedArrayBuffer(4));
51-
5250
constructor() {
5351
super((action, payload) => {
54-
this.#signal[0] = 0;
52+
// We create a new SharedArrayBuffer every time rather than reusing
53+
// the same one, otherwise sometimes its contents get corrupted and
54+
// Atomics.wait wakes up too early.
55+
// https://github.com/babel/babel/pull/14541
56+
const signal = new Int32Array(new SharedArrayBuffer(4));
5557
const subChannel = new worker_threads.MessageChannel();
5658

5759
this.#worker.postMessage(
58-
{ signal: this.#signal, port: subChannel.port1, action, payload },
60+
{ signal, port: subChannel.port1, action, payload },
5961
[subChannel.port1],
6062
);
6163

62-
Atomics.wait(this.#signal, 0, 0);
64+
Atomics.wait(signal, 0, 0);
6365
const { message } = worker_threads.receiveMessageOnPort(
6466
subChannel.port2,
6567
)!;

0 commit comments

Comments
 (0)