Skip to content

Commit

Permalink
SERVER-25777 When stopping a spawned process, MongoDB shell will now …
Browse files Browse the repository at this point in the history
…abort after implicitly falling back to SIGKILL after timeout.
  • Loading branch information
milkie committed Sep 29, 2016
1 parent 7f6eb7f commit 0b879b3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
4 changes: 4 additions & 0 deletions jstests/replsets/apply_ops_wc.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,8 @@

majorityWriteConcerns.forEach(testMajorityWriteConcerns);

// Allow clean shutdown
secondaries[0].getDB('admin').runCommand({configureFailPoint: 'rsSyncApplyStop', mode: 'off'});
secondaries[1].getDB('admin').runCommand({configureFailPoint: 'rsSyncApplyStop', mode: 'off'});

})();
1 change: 1 addition & 0 deletions jstests/replsets/stepdown_killop.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@
assert.writeOK(primary.getDB(name).foo.remove({}));
exitCode = writer();
assert.eq(0, exitCode);
secondary.getDB('admin').runCommand({configureFailPoint: 'rsSyncApplyStop', mode: 'off'});
})();
3 changes: 3 additions & 0 deletions src/mongo/shell/dbshell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,9 @@ int _main(int argc, char* argv[], char** envp) {
cout << "failed to load: " << shellGlobalParams.files[i] << endl;
return -3;
}
if (mongo::shell_utils::KillMongoProgramInstances() != EXIT_SUCCESS) {
return -3;
}
}

if (shellGlobalParams.files.size() == 0 && shellGlobalParams.script.empty())
Expand Down
39 changes: 15 additions & 24 deletions src/mongo/shell/shell_utils_launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,6 @@ inline void kill_wrapper(ProcessId pid, int sig, int port, const BSONObj& opt) {

int killDb(int port, ProcessId _pid, int signal, const BSONObj& opt) {
ProcessId pid;
int exitCode = 0;
if (port > 0) {
if (!registry.isPortRegistered(port)) {
log() << "No db started on port: " << port;
Expand All @@ -898,32 +897,18 @@ int killDb(int port, ProcessId _pid, int signal, const BSONObj& opt) {

kill_wrapper(pid, signal, port, opt);

bool processTerminated = false;
bool killSignalSent = (signal == SIGKILL);
for (int i = 0; i < 6000; ++i) {
if (i == 600) {
log() << "process on port " << port << ", with pid " << pid
<< " not terminated, sending sigkill";
kill_wrapper(pid, SIGKILL, port, opt);
killSignalSent = true;
}
processTerminated = wait_for_pid(pid, false, &exitCode);
if (processTerminated) {
break;
}
sleepmillis(100);
}
if (!processTerminated) {
severe() << "failed to terminate process on port " << port << ", with pid " << pid;
invariant(false);
}

int exitCode = EXIT_FAILURE;
bool processTerminated = wait_for_pid(pid, true, &exitCode);
registry.deleteProgram(pid);

if (killSignalSent) {
if (signal == SIGKILL) {
sleepmillis(4000); // allow operating system to reclaim resources
}

if (!processTerminated) {
warning() << "process " << pid << " failed to terminate.";
return EXIT_FAILURE;
}
return exitCode;
}

Expand Down Expand Up @@ -979,12 +964,18 @@ BSONObj StopMongoProgramByPid(const BSONObj& a, void* data) {
return BSON("" << (double)code);
}

void KillMongoProgramInstances() {
int KillMongoProgramInstances() {
vector<ProcessId> pids;
registry.getRegisteredPids(pids);
int returnCode = EXIT_SUCCESS;
for (auto&& pid : pids) {
killDb(0, pid, SIGTERM);
int port = registry.portForPid(pid);
int code = killDb(port != -1 ? port : 0, pid, SIGTERM);
if (code != EXIT_SUCCESS) {
returnCode = code;
}
}
return returnCode;
}

MongoProgramScope::~MongoProgramScope() {
Expand Down
2 changes: 1 addition & 1 deletion src/mongo/shell/shell_utils_launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct MongoProgramScope {
MongoProgramScope() {} // Avoid 'unused variable' warning.
~MongoProgramScope();
};
void KillMongoProgramInstances();
int KillMongoProgramInstances();

void installShellUtilsLauncher(Scope& scope);

Expand Down

0 comments on commit 0b879b3

Please sign in to comment.