release proc.pid properly before calling waitpid()

This commit is contained in:
Jörg Thalheim 2023-12-10 21:20:45 +01:00 committed by mergify[bot]
parent e1ad62cef1
commit 521380076d

View file

@ -1,4 +1,5 @@
#include <map> #include <map>
#include <sys/signal.h>
#include <thread> #include <thread>
#include <condition_variable> #include <condition_variable>
#include <filesystem> #include <filesystem>
@ -85,23 +86,23 @@ struct State {
}; };
void handleBrokenWorkerPipe(Proc &proc) { void handleBrokenWorkerPipe(Proc &proc) {
// we already took the process status from Proc, no
// need to wait for it again to avoid error messages
pid_t pid = proc.pid.release();
while (1) { while (1) {
int rc = waitpid(proc.pid, nullptr, WNOHANG); int rc = waitpid(pid, nullptr, WNOHANG);
if (rc == 0) { if (rc == 0) {
proc.pid = -1; // we already took the process status from Proc, no kill(pid, SIGKILL);
// need to wait for it again to avoid error messages
throw Error("BUG: worker pipe closed but worker still running?"); throw Error("BUG: worker pipe closed but worker still running?");
} else if (rc == -1) { } else if (rc == -1) {
proc.pid = -1; kill(pid, SIGKILL);
throw Error("BUG: waitpid waiting for worker failed: %s", throw Error("BUG: waitpid waiting for worker failed: %s",
strerror(errno)); strerror(errno));
} else { } else {
if (WIFEXITED(rc)) { if (WIFEXITED(rc)) {
proc.pid = -1;
throw Error("evaluation worker exited with %d", throw Error("evaluation worker exited with %d",
WEXITSTATUS(rc)); WEXITSTATUS(rc));
} else if (WIFSIGNALED(rc)) { } else if (WIFSIGNALED(rc)) {
proc.pid = -1;
if (WTERMSIG(rc) == SIGKILL) { if (WTERMSIG(rc) == SIGKILL) {
throw Error("evaluation worker killed by SIGKILL, maybe " throw Error("evaluation worker killed by SIGKILL, maybe "
"memory limit reached?"); "memory limit reached?");