From f95df6f7d7aa59f5b425a1da611deaea2d13b0e8 Mon Sep 17 00:00:00 2001 From: Sugden <28943913+NotSugden@users.noreply.github.com> Date: Sat, 29 Feb 2020 13:18:57 +0000 Subject: [PATCH] fix(Shard): cleanup after settling spawn promise (#3799) * clear sharding ready timeout * fix oops * update typings * commited to the wrong branch * fix(Shard): cleanup after settling the spawn promise Co-authored-by: SpaceEEC --- src/sharding/Shard.js | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/sharding/Shard.js b/src/sharding/Shard.js index 7e016fbee..f394b9746 100644 --- a/src/sharding/Shard.js +++ b/src/sharding/Shard.js @@ -131,10 +131,37 @@ class Shard extends EventEmitter { if (spawnTimeout === -1 || spawnTimeout === Infinity) return this.process || this.worker; await new Promise((resolve, reject) => { - this.once('ready', resolve); - this.once('disconnect', () => reject(new Error('SHARDING_READY_DISCONNECTED', this.id))); - this.once('death', () => reject(new Error('SHARDING_READY_DIED', this.id))); - setTimeout(() => reject(new Error('SHARDING_READY_TIMEOUT', this.id)), spawnTimeout); + const cleanup = () => { + clearTimeout(spawnTimeoutTimer); + this.off('ready', onReady); + this.off('disconnect', onDisconnect); + this.off('death', onDeath); + }; + + const onReady = () => { + cleanup(); + resolve(); + }; + + const onDisconnect = () => { + cleanup(); + reject(new Error('SHARDING_READY_DISCONNECTED', this.id)); + }; + + const onDeath = () => { + cleanup(); + reject(new Error('SHARDING_READY_DIED', this.id)); + }; + + const onTimeout = () => { + cleanup(); + reject(new Error('SHARDING_READY_TIMEOUT', this.id)); + }; + + const spawnTimeoutTimer = setTimeout(onTimeout, spawnTimeout); + this.once('ready', onReady); + this.once('disconnect', onDisconnect); + this.once('death', onDeath); }); return this.process || this.worker; }