Add a non-busy path to safe_sleep for modern versions of bash#3870
Closed
pdewilde wants to merge 2 commits intoactions:mainfrom
Closed
Add a non-busy path to safe_sleep for modern versions of bash#3870pdewilde wants to merge 2 commits intoactions:mainfrom
pdewilde wants to merge 2 commits intoactions:mainfrom
Conversation
Rudxain
reviewed
Dec 10, 2025
| read -rt "$1" <> <(:) || : | ||
| fi | ||
| # Fallback to busy wait. | ||
| while [[ $SECONDS -lt $1 ]]; do |
There was a problem hiding this comment.
-lt is vulnerable to code-injection (SC)
Previously mentioned here
latekvo
suggested changes
Dec 11, 2025
|
|
||
| # Bash 4.0 and above supports read. | ||
| if [[ -n "$BASH_VERSINFO" && "${BASH_VERSINFO[0]}" -ge 4 ]]; then | ||
| echo foo |
There was a problem hiding this comment.
Suggested change
| echo foo |
Remove debug logs
| fi | ||
| # Fallback to busy wait. | ||
| while [[ $SECONDS -lt $1 ]]; do | ||
| echo bar |
Author
|
Changes incorporated here: #4146 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The current implementation of
safe_sleep.shis a busy wait.This seems to exist because
sleepis not a shell builtin, and it can't be assumed all environments have the sleep binary.Bash 4 and above support the builtin
readcommand, which can be used as a sleep with the timeout feature. This pattern is described here:https://github.com/dylanaraps/pure-bash-bible?tab=readme-ov-file#use-read-as-an-alternative-to-the-sleep-command
Despite Bash 4 shipping 16 years ago, bash 3.x is still the default shipped on MacOS due to 4+ being licensed as GPL3, so the busy wait was retained as a fallback.
Also the condition in the busy wait was switched from
!=to-lt, which is a safer comparison and avoids possibility of an infinite loop.Fixes #2380
#3143 has another similar approach, which relies on conditionally checking for sleep or ping binaries to provide a sleep functionality.