Sample Header Ad - 728x90

Should launching background jobs cause a race condition?

2 votes
2 answers
102 views
I tested the following with both bash and dash, and it seems to reliably hang: [ -p pipe ] || mkfifo pipe i=0 while [ $i -lt 10 ]; do pipe wait If we lower the limit from 10 to 1, it doesn't hang (at least not reliably). Sleeping keeps it from hanging. I think that the echo is happening before all background jobs have opened the pipe, so EOF is written to the pipe, closing the pipe and terminating _some_ background jobs. The late arrivals then get stuck waiting for an EOF that never comes. Is this expected behavior? It sure looks like a bug to me, but I'm not sure whether it's my bug or the shell's. Also: I know I can just write echo x | { }, and that would be neater, but I'm working around [some dash behavior I've already reported as a bug](https://lore.kernel.org/dash/CAFKqKCrEXCkyFTx8SqOHx=LHYyKpfa6scjcrMCxA=Hoo0p9yMA@mail.gmail.com/) . EDIT: The version below terminates consistently in bash, so it seems likely that it is _intended_ to be possible to launch a group of background processes and feed them from a single pipeline without doing anything special. echo hello world | { i=0 while [ $i -lt 10 ]; do cat & : $(( i+=1 )) done } wait EDIT: opening the pipe in the parent (r/w mode to avoid [blocking](https://www.man7.org/linux/man-pages/man7/fifo.7.html)) and duplicating the resulting file descriptor for each background process does not seem to solve the issue and results in hangs even after sleeping. [ -p pipe ] || mkfifo pipe exec 3pipe i=0 while [ $i -lt 10 ]; do pipe wait
Asked by Pablo Repetto (31 rep)
Dec 16, 2024, 02:27 PM
Last activity: Dec 20, 2024, 12:22 PM