diff --git a/src/comm/comm_client.cpp b/src/comm/comm_client.cpp index d428671c..025946ce 100644 --- a/src/comm/comm_client.cpp +++ b/src/comm/comm_client.cpp @@ -52,6 +52,8 @@ namespace comm { close(read_fd); close(write_fd); + + util::kill_process(pid, false); return -1; } diff --git a/src/comm/comm_server.cpp b/src/comm/comm_server.cpp index 7b8a1717..52c5be21 100644 --- a/src/comm/comm_server.cpp +++ b/src/comm/comm_server.cpp @@ -312,8 +312,9 @@ namespace comm close(firewall_pipe[0]); // Wait for some time and check if websocketd is still running properly. + // Sending signal 0 to test whether process exist. util::sleep(20); - if (kill(pid, 0) == -1) + if (util::kill_process(pid, false, 0) == -1) return -1; websocketd_pid = pid; diff --git a/src/hpfs/hpfs.cpp b/src/hpfs/hpfs.cpp index b5b839f4..c78d8cad 100644 --- a/src/hpfs/hpfs.cpp +++ b/src/hpfs/hpfs.cpp @@ -40,9 +40,11 @@ namespace hpfs if (pid > 0) { // HotPocket process. - // Check if process is still running. - util::sleep(20); - if (kill(pid, 0) == -1) + util::sleep(INIT_CHECK_INTERVAL); + + // Check if hpfs process is still running. + // Sending signal 0 to test whether process exist. + if (util::kill_process(pid, false, 0) == -1) return -1; merge_pid = pid; @@ -102,8 +104,9 @@ namespace hpfs { util::sleep(INIT_CHECK_INTERVAL); - // Check if process is still running. - if (kill(pid, 0) == -1) + // Check if hpfs process is still running. + // Sending signal 0 to test whether process exist. + if (util::kill_process(pid, false, 0) == -1) { LOG_ERR << "hpfs process " << pid << " has stopped."; break; diff --git a/src/sc.cpp b/src/sc.cpp index 1963033b..66b9a819 100644 --- a/src/sc.cpp +++ b/src/sc.cpp @@ -40,7 +40,11 @@ namespace sc // Write the inputs into the contract process. if (feed_inputs(ctx) != 0) + { + util::kill_process(pid, true); + ctx.contract_pid = 0; goto failure; + } // Wait for child process (contract process) to complete execution. const int presult = await_process_execution(ctx.contract_pid); diff --git a/src/util.cpp b/src/util.cpp index 48f92d05..5638713d 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -215,7 +215,7 @@ namespace util pthread_sigmask(SIG_SETMASK, &mask, NULL); } - // Kill a process with a signal and wait until it stops running. + // Kill a process with a signal and if specified, wait until it stops running. int kill_process(const pid_t pid, const bool wait, const int signal) { if (kill(pid, signal) == -1) @@ -224,8 +224,8 @@ namespace util return -1; } - int pid_status; - if (wait && waitpid(pid, &pid_status, 0) == -1) + const int wait_options = wait ? 0 : WNOHANG; + if (waitpid(pid, NULL, wait_options) == -1) { LOG_ERR << errno << ": waitpid after kill failed."; return -1;