[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Should launching background jobs inside a loop cause a race condition?
From: |
Pablo Repetto |
Subject: |
Should launching background jobs inside a loop cause a race condition? |
Date: |
Wed, 18 Dec 2024 18:53:05 -0300 |
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 cat &
: $(( i+=1 ))
done
# sleep 1
echo hello world >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.
Note that the form
echo hello world | {
i=0
while [ $i -lt 10 ]; do
<pipe cat &
: $(( i+=1 ))
done
}
works just fine in bash, but dash chokes on it. I've reported this
behavior as a bug. Trying to use a named pipe as a workaround is how I
encountered this race condition.
https://lore.kernel.org/dash/CAFKqKCrEXCkyFTx8SqOHx=LHYyKpfa6scjcrMCxA=Hoo0p9yMA@mail.gmail.com/
This question was also posted to unix stackexchange a few days ago:
https://unix.stackexchange.com/questions/788219/should-launching-background-jobs-cause-a-race-condition
- Should launching background jobs inside a loop cause a race condition?,
Pablo Repetto <=