Skip to content

Commit e2c5511

Browse files
committed
fix(MongoMemoryReplSet): add extra fail-save to the timout
1 parent 95554af commit e2c5511

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

packages/mongodb-memory-server-core/src/MongoMemoryReplSet.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,9 @@ export class MongoMemoryReplSet extends EventEmitter {
589589
*/
590590
protected async _waitForPrimary(timeout: number = 30000): Promise<void> {
591591
let timeoutId: NodeJS.Timeout | undefined;
592+
// This is needed, because somehow, the timeout dosnt get always cleared (in time?), so this is an extra fail-save
593+
// https://github.com/nodkz/mongodb-memory-server/issues/431
594+
let foundPrimary = false;
592595

593596
await Promise.race([
594597
...this.servers.map(
@@ -608,13 +611,21 @@ export class MongoMemoryReplSet extends EventEmitter {
608611
}
609612
})
610613
),
611-
new Promise((_res, rej) => {
614+
new Promise<void>((res, rej) => {
612615
timeoutId = setTimeout(() => {
613-
rej(new Error(`Timed out after ${timeout}ms while waiting for an Primary`));
616+
if (foundPrimary) {
617+
log('_waitForPrimary: timeout didnt get cleared, but primary found');
618+
619+
return res();
620+
}
621+
622+
return rej(new Error(`Timed out after ${timeout}ms while waiting for an Primary`));
614623
}, timeout);
615624
}),
616625
]);
617626

627+
foundPrimary = true;
628+
618629
if (!isNullOrUndefined(timeoutId)) {
619630
clearTimeout(timeoutId);
620631
}

0 commit comments

Comments
 (0)