]> git.proxmox.com Git - pve-installer.git/commitdiff
sys: wait a second after sending TERM signal before going for KILL
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 23 Feb 2024 14:47:38 +0000 (15:47 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 23 Feb 2024 14:49:42 +0000 (15:49 +0100)
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 <t.lamprecht@proxmox.com>
Proxmox/Sys/Command.pm

index d145483d7301385d661d684c1b73b9bb7714c1a5..cb5fe76e84c6e078ae94d7b95885efb7fbca62fc 100644 (file)
@@ -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");