summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2018-08-29 22:54:57 +0200
committertomsmeding <tom.smeding@gmail.com>2018-08-29 22:54:57 +0200
commitaa8c92d0cb854dcc93d7f9076285332417354889 (patch)
tree583347bae0091b902c18efd9aaaf21d319a97649
parent4d82d1ae95ee33dae80f71021ec15da03bcdde73 (diff)
Fix wait()'ing on a Process after terminate()'ing it
-rw-r--r--main.cpp3
-rw-r--r--process.cpp6
2 files changed, 6 insertions, 3 deletions
diff --git a/main.cpp b/main.cpp
index 173346c..aa92b64 100644
--- a/main.cpp
+++ b/main.cpp
@@ -311,9 +311,6 @@ match_done:
if (success) usleep(10000);
procs[i].terminate();
}
- for (int i = 0; i < 2; i++) {
- procs[i].wait();
- }
gMultiLog.append(logId, mres.describe(p1, p2));
gMultiLog.complete(logId);
diff --git a/process.cpp b/process.cpp
index d3ceb28..cab5484 100644
--- a/process.cpp
+++ b/process.cpp
@@ -64,6 +64,7 @@ void Process::run() {
}
void Process::wait() {
+ if (pid == -1) return;
while (true) {
int status;
if (waitpid(pid, &status, 0) < 0) {
@@ -88,6 +89,8 @@ void Process::unStop() {
}
bool Process::writeLine(const string_view line) {
+ if (pid == -1) return false;
+
string str;
str.reserve(line.size() + 1);
str += line;
@@ -108,6 +111,8 @@ bool Process::writeLine(const string_view line) {
}
optional<string> Process::readLine() {
+ if (pid == -1) return nullopt;
+
size_t idx = readBuf.find('\n');
if (idx != string::npos) {
string res = readBuf.substr(0, idx);
@@ -144,6 +149,7 @@ void Process::terminate() {
// SIGKILL if necessary
if (pid != -1) {
kill(pid, SIGKILL); // force kill
+ wait();
pid = -1;
}
}