Skip to content

Commit fefa869

Browse files
authored
fix(kill): should not try to open a connection to a not running ReplSet (#811)
1 parent c1f2333 commit fefa869

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/mongodb-memory-server-core/src/util/MongoInstance.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
killProcess,
1010
ManagerBase,
1111
checkBinaryPermissions,
12+
isAlive,
1213
} from './utils';
1314
import { lt } from 'semver';
1415
import { EventEmitter } from 'events';
@@ -423,7 +424,7 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
423424
// wrap the actual stop in a promise, so it can be awaited in multiple calls
424425
// for example a instanceError while stop is already running would cause another stop
425426
this.stopPromise = (async () => {
426-
if (!isNullOrUndefined(this.mongodProcess)) {
427+
if (!isNullOrUndefined(this.mongodProcess) && isAlive(this.mongodProcess.pid)) {
427428
// try to run "shutdown" before running "killProcess" (gracefull "SIGINT")
428429
// using this, otherwise on windows nodejs will handle "SIGINT" & "SIGTERM" & "SIGKILL" the same (instant exit)
429430
if (this.isReplSet) {

packages/mongodb-memory-server-core/src/util/__tests__/MongoInstance.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
UnexpectedCloseError,
1111
} from '../errors';
1212
import { assertIsError } from '../../__tests__/testUtils/test_utils';
13+
import { MongoClient } from 'mongodb';
1314

1415
jest.setTimeout(100000); // 10s
1516

@@ -278,6 +279,25 @@ describe('MongodbInstance', () => {
278279
expect(dbUtil.killProcess).not.toBeCalled();
279280
});
280281

282+
it('"kill" should not try to open a connection to a not running ReplSet', async () => {
283+
const gotPort = await getFreePort();
284+
const mongod = new MongodbInstance({
285+
instance: {
286+
replSet: 'testset',
287+
ip: '127.0.0.1',
288+
port: gotPort,
289+
dbPath: tmpDir,
290+
},
291+
});
292+
await mongod.start();
293+
jest.spyOn(MongoClient, 'connect');
294+
process.kill(mongod.mongodProcess!.pid, 'SIGKILL');
295+
await new Promise<void>((resolve) => setTimeout(resolve, 100));
296+
await mongod.stop();
297+
298+
expect(MongoClient.connect).not.toBeCalled();
299+
});
300+
281301
it('"_launchMongod" should throw an error if "mongodProcess.pid" is undefined', () => {
282302
const mongod = new MongodbInstance({ instance: { port: 0, dbPath: '' } }); // dummy values - they shouldnt matter
283303
const mockBinary = '/tmp/thisShouldNotExist';

0 commit comments

Comments
 (0)