Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: fd used for saving redirected fds leaked to child processes
- X-seq: zsh-workers 41537
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: fd used for saving redirected fds leaked to child processes
- Date: Sun, 13 Aug 2017 11:49:34 -0700
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=/zyLrJKcvxPioXme2VOj8Jaz74mfsl7stmSIhD1i5+E=; b=Ulsr2Xse24uCLdkoE+renuZa/T0RS3JIpeWkVI3ht3VKer1tokWwKDkXwQf30CiRvA pfE+48CLLEdqjA6ysohsaYS6/8zbwj+sns5B1wez8nBQP9OLP4PzQ1xin5Mrld1dEHp8 mhuyNK2P2qFpRGqaQTZ9MPty2SrkGbo161rFOYzi/1buOJnbHkX9AAG/61ta7yc0OMjW aEarWc0KNZ8aCazrM0bL/jgNgluo2do98ew6bazxh9UvFl+fh5Mj78UOZvlqGwEKWv3b CB5Cpsd9107E4sc4OfkzhHi6LLL+lh/UGIBe98XATFgkN9d3Nf+reomM5h+Kcbmw0IDC wtRQ==
- In-reply-to: <20170813161207.GA6530@chaz.gmail.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <20170813161207.GA6530@chaz.gmail.com>
On Sun, Aug 13, 2017 at 9:12 AM, Stephane Chazelas
<stephane.chazelas@xxxxxxxxx> wrote:
> In:
>
> mkfifo fifo
> zsh -c '{ echo GO > fifo & echo $!; } > pid; echo done' | cat
>
> "cat" hangs until some process open the fifo in read mode, even
> though that "echo GO > fifo" command was run in background and
> "done" is output straight away.
Hmm. Here in a bit more detail is what I believe is going on:
There is a parent shell (probably also zsh in this example) managing
the two processes "zsh -c ..." and "cat". Following zsh's usual
practice, the "zsh -c" process has been forked (in case "cat" is a
builtin) and then "cat" has also been forked (because it's not a
builtin). Thus the stdin of "cat" is connected to the stdout of "zsh
-c" as you would expect.
Looking inside that "zsh -c", the "{ ... }" construct is supposed to
be executed in the current shell. So *that* zsh saves its stdout as
fd 12, redirects the "{ ... }" output to the "pid" file, and begins
handling what's inside the braces. At this point fd 12 cannot be
closed, because the current shell still needs it in order to restore
stdout.
Now zsh does "echo GO > file &". It is after zsh forks to background
this process that fd 12 might become irrelevant. However, (the subtle
bit), zsh attempts to process all the redirections first, before doing
the cleanup of the file descriptors that the child process doesn't
need. So it blocks on opening the fifo with fd 12 still open, even
though it would eventually close fd 12 before "echo" is executed.
You can demonstrate this because even using /bin/echo in the example
above, which would invoke the close-on-exec flag, still blocks in
exactly the same way.
The question is how it's possible to address this without
"accidentally" closing descriptors that might legitimately be managed
with further redirections e.g. X>&Y.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author