fix: wait for worker to be ready before initializing (#13270)

fix: wait for worker to be ready before initializing

The test dispatcher waits until a worker is ready before sending the
`init` message. This guarantees that the worker process is listening for the
message. Otherwise, the worker process might miss the `init` message and
the subsequent `run` message would crash the worker.
This commit is contained in:
Thomas Scholtes 2022-04-04 22:56:57 +02:00 committed by GitHub
parent 4e1fb1728f
commit 3e65ef35cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -466,6 +466,7 @@ class Worker extends EventEmitter {
private _didSendStop = false;
private _didFail = false;
private didExit = false;
private _ready: Promise<void>;
constructor(hash: string, parallelIndex: number) {
super();
@ -494,9 +495,15 @@ class Worker extends EventEmitter {
const { method, params } = message;
this.emit(method, params);
});
this._ready = new Promise((resolve, reject) => {
this.process.once('exit', () => reject(new Error('worker exited before it became ready')));
this.once('ready', () => resolve());
});
}
async init(testGroup: TestGroup, loaderData: SerializedLoaderData) {
await this._ready;
const params: WorkerInitParams = {
workerIndex: this.workerIndex,
parallelIndex: this.parallelIndex,
@ -505,7 +512,6 @@ class Worker extends EventEmitter {
loader: loaderData,
};
this.send({ method: 'init', params });
await new Promise(f => this.process.once('message', f)); // Ready ack
}
run(testGroup: TestGroup) {