From dbc29904c4cdd813b9ff179734175b6bb6cc06e2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 5 Jul 2023 18:41:30 +0200 Subject: [PATCH] fix: wait for workers exit at worket set stop MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/worker/WorkerSet.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index a2e6faf0..d761a12a 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -72,7 +72,13 @@ export class WorkerSet extends WorkerAbstract { /** @inheritDoc */ public async stop(): Promise { for (const workerSetElement of this.workerSet) { + const workerExitPromise = new Promise((resolve) => { + workerSetElement.worker.on('exit', () => { + resolve(); + }); + }); await workerSetElement.worker.terminate(); + await workerExitPromise; } this.workerSet.clear(); } @@ -126,12 +132,18 @@ export class WorkerSet extends WorkerAbstract { 'exit', this.workerOptions?.poolOptions?.exitHandler ?? WorkerConstants.EMPTY_FUNCTION ); - worker.once('exit', () => this.workerSet.delete(this.getWorkerSetElementByWorker(worker))); + worker.once('exit', () => + this.removeWorkerSetElement(this.getWorkerSetElementByWorker(worker)) + ); const workerSetElement: WorkerSetElement = { worker, numberOfWorkerElements: 0 }; this.workerSet.add(workerSetElement); return workerSetElement; } + private removeWorkerSetElement(workerSetElement: WorkerSetElement): void { + this.workerSet.delete(workerSetElement); + } + private async getWorkerSetElement(): Promise { let chosenWorkerSetElement: WorkerSetElement; for (const workerSetElement of this.workerSet) { -- 2.34.1