Bug 15619

Summary: Odd beavor of loops with async sleep in 1.136
Product: Busybox Reporter: foosinn <busybox>
Component: Standard ComplianceAssignee: unassigned
Status: RESOLVED FIXED    
Severity: major CC: busybox-cvs, manhongdai, tero.saarni
Priority: P5    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: Linux   
Host: Target:
Build:

Description foosinn 2023-06-05 12:52:15 UTC
If the first command of a for or while loop is forked the following sleep seems to be forked or maybe even skipped too.

Example broken:

for i in 1 2 3 4 5 6 7 8 9; do echo test & sleep 5; done; echo test2

The former command does just take a few milliseconds. Compared to

for i in 1 2 3 4 5 6 7 8 9; do echo test & /bin/sleep 5; done; echo test2

which takes quite some time.


This issue was not existing in 1.35. Tested in an Alpine Linux Docker container.

Thanks for your time!
Comment 1 Tero Saarni 2023-06-16 21:00:19 UTC
This is because of built-in sleep [1] introduced in BusyBox 1.36.0 which exits when receiving SIGCHLD.

Here is another way how to reproduce:

/bin/sleep 1 & sleep 10

The purpose of the first command it to trigger SIGCHLD after one second.
The second command should sleep for 10 seconds.

- If running it in busybox, the command sequence exits after 1 second
- If running it in bash, the command sequence exits after 10 second

Similar problem was previously reported for "read" [2] and fix [3] was applied for it.

--
Tero

[1] https://git.busybox.net/busybox/commit/?id=58598eb7093561d914a6254697e137b815f1fdfc
[2] https://www.mail-archive.com/busybox@busybox.net/msg24147.html
[3] https://git.busybox.net/busybox/commit/?id=f5470419404d643070db99d058405b714695b817
Comment 2 Tero Saarni 2023-06-16 22:17:53 UTC
Having a second look, I think it was this fix [1] that causes the problem as a side effect. It was released in 1.36.1.

[1] https://git.busybox.net/busybox/commit/?h=1_36_1&id=ce839dea92ce10627094096835e831bf5d267631
Comment 3 Manhong 2023-07-09 11:59:09 UTC
*** Bug 15631 has been marked as a duplicate of this bug. ***
Comment 4 Manhong 2023-07-09 12:07:04 UTC
I marked bug 15631 as a duplicate of this bug.

Before this bug is fixed, we can add a pair of parenthesis as a workaround. The "bug.sh" below demonstrates that "sleep 10" doesn't sleep, while the "fix.sh" shows the workaround

pi:~# ./bug.sh
Sun Jul  9 12:05:09 UTC 2023
Sun Jul  9 12:05:09 UTC 2023
pi:~# ./fix.sh
Sun Jul  9 12:05:12 UTC 2023
Sun Jul  9 12:05:22 UTC 2023

pi:~# cat bug.sh
date &
sleep 10
date
pi:~# cat fix.sh
( date & )
sleep 10
date
Comment 5 Denys Vlasenko 2023-07-10 08:54:04 UTC
I'm disabling sleep builtin in ash, with a comment describing problems.