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

Re: zsh mysteriously suspending job with sudo



2020-02-22 18:09:13 -0700, Ronan Pigott:
[...]
> $ sudo true
> [sudo] password for ronan:
> $ pacman -Qttdq | sudo pacman -Rns -
> [...]
> :: Do you want to remove these packages? [Y/n] zsh: done
>  pacman -Qttdq |
> zsh: suspended (tty output)  sudo pacman -Rns -
[...]

Can be reproduced with:

$ sleep 1 | sudo sh -c 'sleep 2; ps -jfHt "$(tty<&2)"; awk "{print \$8}" /proc/self/stat /dev/tty'
UID        PID  PPID  PGID   SID  C STIME TTY          TIME CMD
chazelas 25430  8308 25430 25430  0 13:44 pts/1    00:00:00 /bin/zsh
root     26867 25430 26866 25430  0 13:46 pts/1    00:00:00   sudo sh -c sleep 2; ps -jfHt "$(tty<&2)"; awk "{print \$8}" /proc/self/stat /dev/tty
root     26868 26867 26866 25430  0 13:46 pts/1    00:00:00     sh -c sleep 2; ps -jfHt "$(tty<&2)"; awk "{print \$8}" /proc/self/stat /dev/tty
root     26871 26868 26866 25430  0 13:46 pts/1    00:00:00       ps -jfHt /dev/pts/1
25430
zsh: done                   sleep 1 |
zsh: suspended (tty input)  sudo sh -c

As seen above, at the time "ps" is run (and awk later), the
foreground process group of the terminal is 25430 which is the
pgid of the main shell, not the pgid of the foreground job
(26866), which is why that job gets a SIGTTIN when awk tries to
read from the terminal (or SIGTTOU when pacman does an ioctl to
the terminal).

From "strace", it seems it's because when "sleep 1" (the process
group leader) finishes, zsh does a kill(-26866,0), presumably to
check that the process group is still alive, but that fails with
EPERM as there are processes running as root in that group, and
then zsh changes the foreground process group back to the main
shell's.

So it seems indeed to be a bug in zsh.

I suppose an easy fix would be to check for an errno of ESRCH
when kill(-pgid,0) fails to make sure it's because the process
group is gone. But, here the shell should be able to know that
the job is not gone as sudo, a direct children of the shell has
not returned yet, so there's probably something wrong with the
logic in the first place.

-- 
Stephane



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