From: Thomas Lamprecht Date: Fri, 23 Feb 2024 14:47:38 +0000 (+0100) Subject: sys: wait a second after sending TERM signal before going for KILL X-Git-Url: https://git.proxmox.com/?p=pve-installer.git;a=commitdiff_plain;h=c8b7a3d36e1d2edec68aa9007cc482eceb7a6f1c sys: wait a second after sending TERM signal before going for KILL Graceful process termination can need a bit of time, so wait one second between sending the (catchable) TERM signal and the (uncatchable) KILL one. Makes the code shorter as a side benefit. Signed-off-by: Thomas Lamprecht --- diff --git a/Proxmox/Sys/Command.pm b/Proxmox/Sys/Command.pm index d145483..cb5fe76 100644 --- a/Proxmox/Sys/Command.pm +++ b/Proxmox/Sys/Command.pm @@ -49,16 +49,13 @@ my sub wait_for_process { kill('TERM', $pid) if $params{kill}; - my $terminated = waitpid($pid, WNOHANG); - return $? if $terminated > 0; - - kill('KILL', $pid) if $params{kill}; - my $timeout = $params{timeout} // 5; - for (1 .. $timeout) { - $terminated = waitpid($pid, WNOHANG); + for (0 .. $timeout) { + my $terminated = waitpid($pid, WNOHANG); return $? if $terminated > 0; - sleep(1); + + sleep(1) if $_ != $timeout; # all but last round + kill('KILL', $pid) if $params{kill} && $_ == 1; # just first round } log_warn("failed to kill child pid $pid, probably stuck in D-state?\n");