Prevent race condition when grim_reaper thread is already trying to lock while the thread is being asked to shut down

This commit is contained in:
Thulinma 2024-01-19 15:53:10 +01:00
parent e20d8fae8c
commit d0374812e4

View file

@ -199,29 +199,31 @@ void Util::Procs::grim_reaper(void *n){
VERYHIGH_MSG("Grim reaper start"); VERYHIGH_MSG("Grim reaper start");
while (thread_handler){ while (thread_handler){
{ {
tthread::lock_guard<tthread::mutex> guard(plistMutex); if (plistMutex.try_lock()){
int status; int status;
pid_t ret = -1; pid_t ret = -1;
while (ret != 0){ while (ret != 0){
ret = waitpid(-1, &status, WNOHANG); ret = waitpid(-1, &status, WNOHANG);
if (ret <= 0){// ignore, would block otherwise if (ret <= 0){// ignore, would block otherwise
if (ret == 0 || errno != EINTR){break;} if (ret == 0 || errno != EINTR){break;}
continue; continue;
} }
int exitcode; int exitcode;
if (WIFEXITED(status)){ if (WIFEXITED(status)){
exitcode = WEXITSTATUS(status); exitcode = WEXITSTATUS(status);
}else if (WIFSIGNALED(status)){ }else if (WIFSIGNALED(status)){
exitcode = -WTERMSIG(status); exitcode = -WTERMSIG(status);
}else{// not possible }else{// not possible
break; break;
} }
if (plist.count(ret)){ if (plist.count(ret)){
HIGH_MSG("Process %d fully terminated with code %d", ret, exitcode); HIGH_MSG("Process %d fully terminated with code %d", ret, exitcode);
plist.erase(ret); plist.erase(ret);
}else{ }else{
HIGH_MSG("Child process %d exited with code %d", ret, exitcode); HIGH_MSG("Child process %d exited with code %d", ret, exitcode);
}
} }
plistMutex.unlock();
} }
} }
Util::sleep(500); Util::sleep(500);