Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: kill %jobspec tries to kill dead processes



2018-01-05 12:46:02 +0000, Stephane Chazelas:
[...]
> sleep 1 | sleep 3 & sleep 2
> kill %"sleep 1"
> 
> kill still tries to kill the process that was running sleep 1
> even though the shell knows it has died:
[...]

I beleive the patch below should do it. I added an unrelated
comment about the "sig != 0". I guess we could skip the killing
altogether for sig == 0, but I left it in on the basis of "who
are we do deny the user's request even if it's pointless".

diff --git a/Src/signals.c b/Src/signals.c
index 94f379e..e06fc05 100644
--- a/Src/signals.c
+++ b/Src/signals.c
@@ -779,8 +779,17 @@ killjb(Job jn, int sig)
 	    return killpg(jn->gleader, sig);
     }
     for (pn = jn->procs; pn; pn = pn->next)
-        if ((err = kill(pn->pid, sig)) == -1 && errno != ESRCH && sig != 0)
-            return -1;
+	/*
+	 * Do not kill this job's process if it's already dead as its
+	 * pid could have been reused by the system.
+	 */
+	if (pn->status == SP_RUNNING || WIFSTOPPED(pn->status))
+	    /*
+	     * kill -0 on a job is pointless. We still call kill() for each process
+	     * in case the user cares about it but we ignore its outcome.
+	     */
+	    if ((err = kill(pn->pid, sig)) == -1 && errno != ESRCH && sig != 0)
+		return -1;
     return err;
 }
 



Messages sorted by: Reverse Date, Date, Thread, Author