zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: TRAPCHLD and wait
Date: Sun, 3 Dec 2023 10:02:12 -0800	[thread overview]
Message-ID: <CAH+w=7aATxmTuC5RdbmgO7puzo9s5rMHYvs0nZeTU8iTgG-4tQ@mail.gmail.com> (raw)

The manual says:

     It is possible to wait for recent processes (specified by process
     ID, not by job) that were running in the background even if the
     process has exited.  Typically the process ID will be recorded by
     capturing the value of the variable $! immediately after the
     process has been started.

This does work in the most obvious cases:

% { sleep 10; exit 23 } & bang=$!
[1] 560524
% sleep 20
[1]  + exit 23    { sleep 10; exit 23; }
% wait $bang
% print $?
23
%

However, you can't do that wait in a CHLD trap, even though it still
works after the trap has run:

% TRAPCHLD() {
  wait $bang
  q=$?
  zle -I
  print $q
  zle -R
}
% { sleep 10; exit 21 } & bang=$!
[1] 560661
%
[1]  + exit 21    { sleep 10; exit 21; }
%
TRAPCHLD:wait:1: pid 560661 is not a child of this shell
127
% wait $bang
% print $?
21
%

I suspect this has to do with order of operations in the signal
handlers but thought I'd ask for clues before digging in myself.


             reply	other threads:[~2023-12-03 18:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-03 18:02 Bart Schaefer [this message]
2023-12-03 21:27 ` Bart Schaefer

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=7aATxmTuC5RdbmgO7puzo9s5rMHYvs0nZeTU8iTgG-4tQ@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --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).