zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Jun T <takimoto-j@kba.biglobe.ne.jp>
Cc: zsh-workers@zsh.org
Subject: Re: [PATCH] problem with 'ls | less' shell function
Date: Mon, 7 Nov 2022 11:33:14 -0800	[thread overview]
Message-ID: <CAH+w=7Z-6akgCf=eN72YPp038r3APpLOkTV=zwzhKqRJgP=fHA@mail.gmail.com> (raw)
In-Reply-To: <0A744179-81FA-4405-857D-6FFBE8F8FA1E@kba.biglobe.ne.jp>

On Mon, Nov 7, 2022 at 12:44 AM Jun T <takimoto-j@kba.biglobe.ne.jp> wrote:
>
> [1] ( zsh -fc 'print a few words; /bin/sleep 10'; )

Missing the | { head -n -1 } here, but I think still confirmed working?

> [2] { sleep 10; sleep 11; } | { sleep 20; sleep 21; }
> [3] dir () { ls | less; }; dir
>
> Now both ^C and ^Z/fg fork for [1], ^C works for [2],
> and ^Z/fg works for [3] ({3] can't be killed by ^C but it's OK).
>
> But ^Z/fg still doesn't work for [2]; the pipeline never finishes.
[...]
> In the case of [2], the main zsh (zsh0) always fork() a subshell (zsh1)
> for the left hand side of the pipe, and zsh1 fork/exec 'sleep 10'.
> By hitting ^Z, zsh0 fork() another subshell (zsh2) for the right hand
> side of the pipe, but it seems zsh2 is not causing the problem.

Hmm.  If I examine "pstree" I find:

zsh(43919)---zsh(43978)---sleep(43980)

43978 is your "zsh2" and is busy-waiting with 100% CPU at

#0  0x000055ca5bc4c042 in execpline (state=0x7ffc311ce770, slcode=4098, how=2,
    last1=0) at exec.c:1789
#1  0x000055ca5bc4abc3 in execlist (state=0x7ffc311ce770, dont_change_job=1,
    exiting=1) at exec.c:1444
#2  0x000055ca5bc477d7 in execcursh (state=0x7ffc311ce770, do_exec=1)
    at exec.c:450

43919 is "zsh1" and is here:

#0  0x00007fcadf04045c in __GI___sigsuspend (set=0x7ffc311cd9b0)
    at ../sysdeps/unix/sysv/linux/sigsuspend.c:26
#1  0x000055ca5bcbf050 in signal_suspend (sig=17, wait_cmd=0) at signals.c:393
#2  0x000055ca5bc7c31a in zwaitjob (job=1, wait_cmd=0) at jobs.c:1629

> After 'fg', zsh1 (not zsh2) is using 100% of a CPU core, and
> when 'sleep 10' exits it is left as "defunct" (not waited for).

pstree for that zombie sleep shows it's a child of 43919 (zsh1) and
hasn't been cleaned up yet because zsh1 is waiting on zsh2.

zsh2 believes it's going to get signaled for the sleep, but it won't.

> > 2022/10/22 6:22, Bart Schaefer <schaefer@brasslantern.com> wrote:
> >
> > On Fri, Oct 21, 2022 at 12:41 AM Jun T <takimoto-j@kba.biglobe.ne.jp> wrote:
> >>
> >> The subshell have missed the SIGCHLD?
> >
> > The subshell will never get the SIGCHLD, because it is a sibling of
> > the pipeline, not its parent.
>
> Do you mean the subshell for the right hand side of the pipe?

Yes, that is what I mean.  The zombie sleep and zsh2 are siblings.

> Strace output suggests that zsh1 does not receive SIGCHLD when
> 'sleep 10' finishes. Maybe SIGCHLD is blocked?? (I'm not sure).

#2  0x000055ca5bc7c31a in zwaitjob (job=1, wait_cmd=0) at jobs.c:1629

job=1 is zsh2:

(gdb) p jobtab[1].procs[0]
$2 = {next = 0x55ca5dca7690, pid = 43978,

waitjob() in zsh1 will never return because zsh2 is in an infinite
loop waiting for a child that doesn't exist.

> zsh1 is repeatedly calling hasprocs(list_pid_job) at line 1790 in
> exec.c:

This is actually zsh2.  As you noted, hasprocs() returns zero, so we
fall through to

1902            else if (subsh && jn->stat & STAT_STOPPED)
1903                thisjob = newjob;
1904            else
1905                break;

The condition at 1902 is true because jn->stat refers to the zombie
sleep, which WAS stopped when zsh2 was forked (but is not actually
even a job of this new subshell).  So we go around the loop again at

1778            for (; !nowait;) {

When zsh2 was forked during "sleep 20" , we should have hit this

1899                break;

so we must have re-entered at line 1778 after starting the "sleep 21"
and are now incorrectly waiting for the left side of the pipeline.


  reply	other threads:[~2022-11-07 19:34 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-17  9:17 Thomas Klausner
2022-10-17 14:50 ` Mikael Magnusson
2022-10-17 15:36   ` Thomas Klausner
2022-10-19  8:26     ` zsugabubus
2022-10-19 10:04       ` Jun T
2022-10-19 13:36         ` Jun. T
2022-10-20  0:10       ` Bart Schaefer
2022-10-20 16:26         ` Peter Stephenson
2022-10-21  5:45       ` Jun T
2022-10-21  7:40         ` Jun T
2022-10-21 21:22           ` Bart Schaefer
2022-10-21 21:24             ` Bart Schaefer
2022-10-22 23:22               ` Bart Schaefer
2022-10-22 23:43                 ` Bart Schaefer
2022-11-03 23:10                   ` Bart Schaefer
2022-11-04  6:09                     ` [PATCH] " Bart Schaefer
2022-11-04 15:10                       ` [PATCH] " Jun T
2022-11-05  0:09                         ` Bart Schaefer
2022-11-06 19:12                           ` Bart Schaefer
2022-11-07  8:43                             ` Jun T
2022-11-07 19:33                               ` Bart Schaefer [this message]
2022-11-07 19:44                                 ` Roman Perepelitsa
2022-11-08  0:46                                   ` Bart Schaefer
2022-11-08  0:44                                 ` Bart Schaefer
2022-11-08  5:03                                   ` Bart Schaefer
2022-11-09  5:03                                     ` Bart Schaefer
2022-10-19  9:33 ` Jun T
2022-10-19 10:01   ` Jun T

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7Z-6akgCf=eN72YPp038r3APpLOkTV=zwzhKqRJgP=fHA@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=takimoto-j@kba.biglobe.ne.jp \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).