Kill on each iteration of graceful exit loop

I believe it's possible for the `main_pid` to disappear between `kill` and `sigtimedwait` by means of an `exec` from a multithreaded process (`PTRACE_EVENT_EXIT` happens after the `exec`ing thread changes its tid to main_pid)

PiperOrigin-RevId: 555137959
Change-Id: Id22908fb31497c0906e4f4fda66400fbf9ac9efb
This commit is contained in:
Wiktor Garbacz 2023-08-09 05:59:49 -07:00 committed by Copybara-Service
parent 999336a27d
commit c14312c3a2

View File

@ -407,10 +407,6 @@ void PtraceMonitor::Run() {
const bool log_stack_traces = const bool log_stack_traces =
result_.final_status() != Result::OK && result_.final_status() != Result::OK &&
absl::GetFlag(FLAGS_sandbox2_log_all_stack_traces); absl::GetFlag(FLAGS_sandbox2_log_all_stack_traces);
if (!log_stack_traces) {
// Try to make sure main pid is killed and reaped
kill(process_.main_pid, SIGKILL);
}
constexpr auto kGracefullExitTimeout = absl::Milliseconds(200); constexpr auto kGracefullExitTimeout = absl::Milliseconds(200);
auto deadline = absl::Now() + kGracefullExitTimeout; auto deadline = absl::Now() + kGracefullExitTimeout;
if (log_stack_traces) { if (log_stack_traces) {
@ -432,9 +428,12 @@ void PtraceMonitor::Run() {
} }
break; break;
} }
if (!log_stack_traces && ret == process_.main_pid && if (!log_stack_traces) {
(WIFSIGNALED(status) || WIFEXITED(status))) { if (ret == process_.main_pid &&
break; (WIFSIGNALED(status) || WIFEXITED(status))) {
break;
}
kill(process_.main_pid, SIGKILL);
} }
if (ret == 0) { if (ret == 0) {
@ -454,10 +453,6 @@ void PtraceMonitor::Run() {
continue; continue;
} }
} }
if (!log_stack_traces) {
kill(process_.main_pid, SIGKILL);
}
} }
} }
} }