From: Jérôme Benoit Date: Wed, 23 Aug 2023 11:02:53 +0000 (+0200) Subject: fix: ensure no task can be executed on destroyed pool X-Git-Tag: v2.6.32~5 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=15b176e0295f49da9ddc609e03b328cb8963eee1;hp=251d6ac2d29d7361d671a8df17af51b414ea3b83;p=poolifier.git fix: ensure no task can be executed on destroyed pool Signed-off-by: Jérôme Benoit --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fe43abd..c1e65992 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Ensure no task can be executed when the pool is destroyed. + ### Added - Add `queueMaxSize` option to tasks queue options. diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index f0304364..7310e9ef 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -93,6 +93,10 @@ export abstract class AbstractPool< * Whether the pool is starting or not. */ private readonly starting: boolean + /** + * Whether the pool is started or not. + */ + private started: boolean /** * The start timestamp of the pool. */ @@ -141,6 +145,7 @@ export abstract class AbstractPool< this.starting = true this.startPool() this.starting = false + this.started = true this.startTimestamp = performance.now() } @@ -723,6 +728,9 @@ export abstract class AbstractPool< transferList?: TransferListItem[] ): Promise { return await new Promise((resolve, reject) => { + if (!this.started) { + reject(new Error('Cannot execute a task on destroyed pool')) + } if (name != null && typeof name !== 'string') { reject(new TypeError('name argument must be a string')) } @@ -783,6 +791,7 @@ export abstract class AbstractPool< }) ) this.emitter?.emit(PoolEvents.destroy, this.info) + this.started = false } protected async sendKillMessageToWorker ( @@ -1050,7 +1059,11 @@ export abstract class AbstractPool< workerInfo.ready = false this.workerNodes[workerNodeKey].closeChannel() this.emitter?.emit(PoolEvents.error, error) - if (this.opts.restartWorkerOnError === true && !this.starting) { + if ( + this.opts.restartWorkerOnError === true && + !this.starting && + this.started + ) { if (workerInfo.dynamic) { this.createAndSetupDynamicWorkerNode() } else {