On the Hurd, kill (pid, SIGKILL) on a process that is already on its
way out can fail with EINVAL, where Linux just returns 0.  proc still
hands out the task port at that point, but GNU Mach has started
tearing the task down, so task_terminate says KERN_INVALID_ARGUMENT
(or KERN_FAILURE a bit earlier).

When that happens, keep our send right to the task and ask proc again.
If we get the same task back, it's the one that is dying and there's
nothing left to do.  If we get a different one, the process has execed,
so we terminate the new task.  No port at all means it's a zombie by
now.  Just retrying isn't enough, because on a single CPU the loop can
keep spinning on the same dying task for a long time.

This is where guix-daemon's "killing process N: Invalid argument" comes
from, see https://codeberg.org/guix/guix/issues/8638.

Signed-off-by: ajrojas <[email protected]>
---
With guix-daemon it happens all the time.  It closes the build hook's
stdin and kills the hook's process group right after, and the hook is
usually already exiting on that EOF.  Yelninei reported the log line in
the Guix issue.

I tried it on x86_64-gnu, Guix in QEMU with one CPU, using Guix's
glibc 2.41 with this patch.  A child that exits when its stdin closes,
killed right after the close, got EINVAL most of the time before and
never after.  guix-daemon stopped printing the message on "guix build
--no-substitutes".  Zombies, reaped pids, live process groups and bad
signal numbers behave as before.

The KERN_FAILURE case and the exec case aren't tested.  I only ever got
KERN_INVALID_ARGUMENT, and I couldn't catch a process in the middle of
an exec.

 sysdeps/mach/hurd/kill.c | 57 +++++++++++++++++++++++++++-------------
 1 file changed, 39 insertions(+), 18 deletions(-)

diff --git a/sysdeps/mach/hurd/kill.c b/sysdeps/mach/hurd/kill.c
index 630a87243b3..039edd8aabd 100644
--- a/sysdeps/mach/hurd/kill.c
+++ b/sysdeps/mach/hurd/kill.c
@@ -49,24 +49,45 @@ __kill (pid_t pid, int sig)
       /* SIGKILL is not delivered as a normal signal.
         Sending SIGKILL to a process means to terminate its task.  */
       if (sig == SIGKILL)
-       /* Fetch the process's task port and terminate the task.  We
-          loop in case the process execs and changes its task port.
-          If the old task port dies after we fetch it but before we
-          send the RPC, we get MACH_SEND_INVALID_DEST; if it dies
-          after we send the RPC request but before it is serviced, we
-          get MIG_SERVER_DIED.  */
-       do
-         {
-           task_t refport;
-           err = __proc_pid2task (proc, pid, &refport);
-           /* Ignore zombies.  */
-           if (!err && refport != MACH_PORT_NULL)
-             {
-               err = __task_terminate (refport);
-               __mach_port_deallocate (__mach_task_self (), refport);
-             }
-         } while (err == MACH_SEND_INVALID_DEST
-                  || err == MIG_SERVER_DIED);
+       {
+         /* Fetch the process's task port and terminate the task.  We
+            loop in case the process execs and changes its task port.
+            If the old task port dies after we fetch it but before we
+            send the RPC, we get MACH_SEND_INVALID_DEST; if it dies
+            after we send the RPC request but before it is serviced, we
+            get MIG_SERVER_DIED.  If the task is already being
+            terminated, we get KERN_FAILURE or KERN_INVALID_ARGUMENT.
+            Then we keep our send right to it, so that proc gives us
+            the same name again if the process is still on that task,
+            in which case it is going away and we are done.  */
+         task_t dying = MACH_PORT_NULL;
+         do
+           {
+             task_t refport;
+             err = __proc_pid2task (proc, pid, &refport);
+             /* Ignore zombies.  */
+             if (!err && refport != MACH_PORT_NULL)
+               {
+                 if (refport == dying)
+                   err = 0;
+                 else
+                   err = __task_terminate (refport);
+                 if (err == KERN_FAILURE || err == KERN_INVALID_ARGUMENT)
+                   {
+                     if (dying != MACH_PORT_NULL)
+                       __mach_port_deallocate (__mach_task_self (), dying);
+                     dying = refport;
+                   }
+                 else
+                   __mach_port_deallocate (__mach_task_self (), refport);
+               }
+           } while (err == MACH_SEND_INVALID_DEST
+                    || err == MIG_SERVER_DIED
+                    || err == KERN_FAILURE
+                    || err == KERN_INVALID_ARGUMENT);
+         if (dying != MACH_PORT_NULL)
+           __mach_port_deallocate (__mach_task_self (), dying);
+       }
       else
        {
          error_t taskerr;
-- 
2.56.0


Reply via email to