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

Re: problem with 'ls | less' shell function



On Sat, Oct 22, 2022 at 4:43 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> So as far as zsh knows, the attachtty() was successful, but if the
> debugger is telling the truth, something is resetting the tty pgrp
> again without attachtty() being called.

A little further ... I instrumented jobs.c:makerunning() --

% dir() { ls -lrRt | less }
% dir
^Z
% fg
makerunning: PID 79731 job 1 jn->stat: 9427
makerunning: PID 79731 job 2 jn->stat: 370
[1]  + continued  ls -lrRt |
makerunning: PID 79731 job 1 jn->stat: 9425
makerunning: PID 79731 job 2 jn->stat: 368
zsh: suspended (tty output)  ls -lrRt |
zsh: suspended (tty output)  ls -lrRt |

With an anonymous function instead of a named one, this doesn't happen
and "fg" works:

% () { ls -lrRt | less }
^Z
% fg
makerunning: PID 79731 job 3 jn->stat: 82
[3]  - continued  ls -lrRt |
makerunning: PID 79731 job 3 jn->stat: 80

9427 is CHANGED|STOPPED|LOCKED|INUSE|SUPERJOB|CURSH|SUBLEADER
370 is STOPPED|LOCKED|NOPRINT|INUSE|SUBJOB
82 is STOPPED|LOCKED|INUSE

So we can remove at least one makerunning(), I think from bin_fg().
But I then instrumented update_job() where "continued..." is printed,
and:

% fg
[1]  + suspended  ls -lrRt |
makerunning: PID 687 job 1 jn->stat: 9427
makerunning: PID 687 job 2 jn->stat: 370
zsh: wait_for_processes after SIGCHLD
zsh: update_job signals.c L559
zsh: update_job signals.c L559
update_job: stopping gleader
zsh: suspended (tty output)  ls -lrRt |
zsh: suspended (tty output)  ls -lrRt |

Aha.  So there's a race condition of sorts, just one that we always
lose.  We send a SIGCONT to the process group for the forked job,
which wakes up both the subjob and the superjob, but when we get the
SIGCHLD for the subjob we decide that its superjob is running not
because we just restarted it, but because it is still waiting to be
stopped from the previous SIGTSTP.  I haven't worked out if this means
there's an order of signal delivery issue or just that we botched some
internal state.




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