Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kill): should not try to open a connection to a not running ReplSet #811

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
killProcess,
ManagerBase,
checkBinaryPermissions,
isAlive,
} from './utils';
import { lt } from 'semver';
import { EventEmitter } from 'events';
Expand Down Expand Up @@ -423,7 +424,7 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
// wrap the actual stop in a promise, so it can be awaited in multiple calls
// for example a instanceError while stop is already running would cause another stop
this.stopPromise = (async () => {
if (!isNullOrUndefined(this.mongodProcess)) {
if (!isNullOrUndefined(this.mongodProcess) && isAlive(this.mongodProcess.pid)) {
// try to run "shutdown" before running "killProcess" (gracefull "SIGINT")
// using this, otherwise on windows nodejs will handle "SIGINT" & "SIGTERM" & "SIGKILL" the same (instant exit)
if (this.isReplSet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
UnexpectedCloseError,
} from '../errors';
import { assertIsError } from '../../__tests__/testUtils/test_utils';
import { MongoClient } from 'mongodb';

jest.setTimeout(100000); // 10s

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

it('"kill" should not try to open a connection to a not running ReplSet', async () => {
const gotPort = await getFreePort();
const mongod = new MongodbInstance({
instance: {
replSet: 'testset',
ip: '127.0.0.1',
port: gotPort,
dbPath: tmpDir,
},
});
await mongod.start();
jest.spyOn(MongoClient, 'connect');
process.kill(mongod.mongodProcess!.pid, 'SIGKILL');
await new Promise<void>((resolve) => setTimeout(resolve, 100));
await mongod.stop();

expect(MongoClient.connect).not.toBeCalled();
});

it('"_launchMongod" should throw an error if "mongodProcess.pid" is undefined', () => {
const mongod = new MongodbInstance({ instance: { port: 0, dbPath: '' } }); // dummy values - they shouldnt matter
const mockBinary = '/tmp/thisShouldNotExist';
Expand Down