rehash: detect and remove a stale lockfile (#3450)

fixes stalling for 60s and failing if some past fault
has resulting in a stale lockfile being present

---------

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This commit is contained in:
native-api 2026-05-17 09:38:23 +03:00 committed by GitHub
parent 06fd1ce788
commit 70f096a95c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 5 deletions

View File

@ -2,7 +2,7 @@
# Summary: Rehash pyenv shims (run this after installing executables) # Summary: Rehash pyenv shims (run this after installing executables)
set -e set -e
[ -n "$PYENV_DEBUG" ] && set -x [[ -n "$PYENV_DEBUG" ]] && set -x
SHIM_PATH="${PYENV_ROOT}/shims" SHIM_PATH="${PYENV_ROOT}/shims"
PROTOTYPE_SHIM_PATH="${SHIM_PATH}/.pyenv-shim" PROTOTYPE_SHIM_PATH="${SHIM_PATH}/.pyenv-shim"
@ -15,13 +15,15 @@ declare last_acquire_error
acquire_lock() { acquire_lock() {
# Ensure only one instance of pyenv-rehash is running at a time by # Ensure only one instance of pyenv-rehash is running at a time by
# setting the shell's `noclobber` option and attempting to write to # setting the shell's `noclobber` option and attempting to write to
# the prototype shim file. If the file already exists, print a warning # the prototype shim file.
# to stderr and exit with a non-zero status.
local ret local ret
set -o noclobber set -o noclobber
last_acquire_error="$( { ( echo -n > "$PROTOTYPE_SHIM_PATH"; ) 2>&1 1>&3 3>&1-; } 3>&1)" || ret=1 # Assuming an old lockfile is stale
# Unknown why this happens for some users but this at least unblocks them
last_acquire_error="$( { ( echo -n > "$PROTOTYPE_SHIM_PATH"; ) 2>&1 1>&3 3>&1-; } 3>&1)" \
|| { find "$PROTOTYPE_SHIM_PATH" -mmin +10 -exec rm -f {} \; ; ret=1; }
set +o noclobber set +o noclobber
[ -z "${ret}" ] [[ -z "${ret}" ]]
} }
remove_prototype_shim() { remove_prototype_shim() {

View File

@ -38,6 +38,18 @@ pyenv: cannot rehash: couldn't acquire lock ${PYENV_ROOT}/shims/.pyenv-shim for
assert_success assert_success
} }
@test "stale lockfile is removed" {
mkdir -p "${PYENV_ROOT}/shims"
#GNU and BSD `date` support generating relative dates via different syntax
# but BusyBox `date` used in Bash Docker images doesn't
touch -t 200001010000.00 "${PYENV_ROOT}/shims/.pyenv-shim"
run pyenv-rehash
assert_success ""
assert [ ! -e "${PYENV_ROOT}/shims/.pyenv-shim" ]
}
@test "creates shims" { @test "creates shims" {
create_alt_executable_in_version "2.7" "python" create_alt_executable_in_version "2.7" "python"
create_alt_executable_in_version "2.7" "fab" create_alt_executable_in_version "2.7" "fab"