Skip to content

Commit

Permalink
Fix synchronization between main thread and worker (#14541)
Browse files Browse the repository at this point in the history
* fix: eslint-parser worker

* Update eslint/babel-eslint-parser/src/client.cjs

* Add debug logs

* Always use a different SAB

* Only run eslint tests

* Always log

* Run all tests

* Update eslint/babel-eslint-parser/src/client.cjs

* Revert as many changes as possible

* Remove test loop

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
liuxingbaoyu and nicolo-ribaudo committed May 22, 2022
1 parent 8f904e0 commit 1fcc571
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions eslint/babel-eslint-parser/src/client.cjs
Expand Up @@ -60,19 +60,22 @@ exports.WorkerClient = class WorkerClient extends Client {
{ env: WorkerClient.#worker_threads.SHARE_ENV },
);

#signal = new Int32Array(new SharedArrayBuffer(4));

constructor() {
super((action, payload) => {
this.#signal[0] = 0;
// We create a new SharedArrayBuffer every time rather than reusing
// the same one, otherwise sometimes its contents get corrupted and
// Atomics.wait wakes up too early.
// https://github.com/babel/babel/pull/14541
const signal = new Int32Array(new SharedArrayBuffer(8));

const subChannel = new WorkerClient.#worker_threads.MessageChannel();

this.#worker.postMessage(
{ signal: this.#signal, port: subChannel.port1, action, payload },
{ signal, port: subChannel.port1, action, payload },
[subChannel.port1],
);

Atomics.wait(this.#signal, 0, 0);
Atomics.wait(signal, 0, 0);
const { message } = WorkerClient.#worker_threads.receiveMessageOnPort(
subChannel.port2,
);
Expand Down

0 comments on commit 1fcc571

Please sign in to comment.