zsh-workers
 help / color / mirror / code / Atom feed
* fd used for saving redirected fds leaked to child processes
@ 2017-08-13 16:12 Stephane Chazelas
  2017-08-13 18:49 ` Bart Schaefer
  2017-08-13 18:49 ` Peter Stephenson
  0 siblings, 2 replies; 12+ messages in thread
From: Stephane Chazelas @ 2017-08-13 16:12 UTC (permalink / raw)
  To: Zsh hackers list

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.

What we see is the child zsh process opening the "fifo" having
a fd open on the pipe to cat:

zsh     28400 chazelas   12w  FIFO   0,10      0t0  125690 pipe 28399,cat,0r

I think that fd was the one that was dupped from stdout by its
parent to /save/ stdout before doing the > pid redirection (so
it can be used to restore stdout after the command group
returns).

That fd is not needed/used in the child so should be closed
there.

(here, the work around and more obvious way to do it is with
zsh -c 'echo GO > fifo & echo $! > pid; echo done' | cat
that was just an example to illustrate the problem).

bash and dash seem to also have the problem. pdksh, ksh93 and
yash are OK. Looking at strace outputs, they seem to be closing
those fds in the children.

The Bourne shell and rc are OK, but only because they don't use
a dupped fd to "save" stdout when redirecting compound commands.
They run the compound command in a subshell.

Note that those fds have the CLOEXEC flag, so the circumstances
in which it becomes a problem are few.

-- 
Stephane


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2017-09-29 15:18 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-13 16:12 fd used for saving redirected fds leaked to child processes Stephane Chazelas
2017-08-13 18:49 ` Bart Schaefer
2017-08-13 18:49 ` Peter Stephenson
2017-08-13 21:45   ` Bart Schaefer
2017-08-14  9:09     ` Peter Stephenson
2017-08-14 14:49       ` Bart Schaefer
2017-08-14 15:24         ` Peter Stephenson
2017-08-14 15:31           ` Bart Schaefer
2017-08-14 19:19         ` Peter Stephenson
2017-09-29 15:06           ` Daniel Shahaf
2017-09-29 15:18             ` Peter Stephenson
2017-08-15 18:42         ` Peter Stephenson

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).