Skip to content

Commit

Permalink
fix(Shard): use provided timeout when respawning (#6735)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceEEC committed Oct 3, 2021
1 parent 34b2ad0 commit 905d100
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/sharding/Shard.js
Expand Up @@ -92,7 +92,7 @@ class Shard extends EventEmitter {
* @type {Function}
* @private
*/
this._exitListener = this._handleExit.bind(this, undefined);
this._exitListener = null;
}

/**
Expand All @@ -106,6 +106,8 @@ class Shard extends EventEmitter {
if (this.process) throw new Error('SHARDING_PROCESS_EXISTS', this.id);
if (this.worker) throw new Error('SHARDING_WORKER_EXISTS', this.id);

this._exitListener = this._handleExit.bind(this, undefined, timeout);

if (this.manager.mode === 'process') {
this.process = childProcess
.fork(path.resolve(this.manager.file), this.args, {
Expand All @@ -132,7 +134,7 @@ class Shard extends EventEmitter {
*/
this.emit('spawn', child);

if (timeout === -1 || timeout === Infinity) return child;
if (timeout === -1 || timeout === Infinity) return Promise.resolve(child);
return new Promise((resolve, reject) => {
const cleanup = () => {
clearTimeout(spawnTimeoutTimer);
Expand Down Expand Up @@ -379,9 +381,11 @@ class Shard extends EventEmitter {
/**
* Handles the shard's process/worker exiting.
* @param {boolean} [respawn=this.manager.respawn] Whether to spawn the shard again
* @param {number} [timeout] The amount in milliseconds to wait until the {@link Client}
* has become ready (`-1` or `Infinity` for no wait)
* @private
*/
_handleExit(respawn = this.manager.respawn) {
_handleExit(respawn = this.manager.respawn, timeout) {
/**
* Emitted upon the shard's child process/worker exiting.
* @event Shard#death
Expand All @@ -395,7 +399,7 @@ class Shard extends EventEmitter {
this._evals.clear();
this._fetches.clear();

if (respawn) this.spawn().catch(err => this.emit('error', err));
if (respawn) this.spawn(timeout).catch(err => this.emit('error', err));
}
}

Expand Down
2 changes: 1 addition & 1 deletion typings/index.d.ts
Expand Up @@ -1677,7 +1677,7 @@ export class Shard extends EventEmitter {
private _evals: Map<string, Promise<unknown>>;
private _exitListener: (...args: any[]) => void;
private _fetches: Map<string, Promise<unknown>>;
private _handleExit(respawn?: boolean): void;
private _handleExit(respawn?: boolean, timeout?: number): void;
private _handleMessage(message: unknown): void;

public args: string[];
Expand Down

0 comments on commit 905d100

Please sign in to comment.