Skip to content

safe_sleep.sh: resolve reliability and maintainability issues#4465

Open
logiclrd wants to merge 1 commit into
actions:mainfrom
logiclrd:safe-sleep
Open

safe_sleep.sh: resolve reliability and maintainability issues#4465
logiclrd wants to merge 1 commit into
actions:mainfrom
logiclrd:safe-sleep

Conversation

@logiclrd
Copy link
Copy Markdown

As described in #185304, the safe_sleep.sh script is problematic because it isn't clear exactly what problems it is solving or whether it is doing so correctly.

This PR attempts to address this in the following ways:

  • The purpose of the script is documented.
  • Digging into the history revealed that the original root reason for the script's existence in the first place is environments where sleep cannot be trusted. Additional logic has been added to detect if sleep returns immediately and fall through to a fallback strategy.
  • This script that already explicitly specifies /bin/bash on the shebang and directly uses Bash-specific syntax no longer has a branch where it double-checks that it is running under Bash. That branch now also simply uses Bash features like the rest of the script.
  • The fallback to read verifies that read is a builtin before attempting to use it to read from the shell process' standard streams.
  • In the extremely unlikely event that execution reaches the busy wait ultimate fallback, the script now renices the shell it's running in to a lower priority.
  • The busy wait now uses date to could elapsed time instead of the Bash built-in SECONDS variable, and it will now leverage the %N format specifier to date if it is supported by the host-supplied date implementation. This allows subsecond precision with the busy-wait. If subsecond precision isn't available, then it continues to loop with second-level precision, which means that the delay will be up to 1 second shorter than requested, depending on how far into the first second the wait was initiated (average 0.5 seconds shorter than requested).

These paths have been tested on modern Linux systems, on Linux hosted on WSL 1, and with Bash 3.2.57 running on FreeBSD.

Closes: #185304

@logiclrd logiclrd requested a review from a team as a code owner May 30, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant