From 70f096a95c3bcc3fc178a9def668e18b847adffe Mon Sep 17 00:00:00 2001 From: native-api Date: Sun, 17 May 2026 09:38:23 +0300 Subject: [PATCH] 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> --- libexec/pyenv-rehash | 12 +++++++----- test/rehash.bats | 12 ++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/libexec/pyenv-rehash b/libexec/pyenv-rehash index 253ab68a..25413100 100755 --- a/libexec/pyenv-rehash +++ b/libexec/pyenv-rehash @@ -2,7 +2,7 @@ # Summary: Rehash pyenv shims (run this after installing executables) set -e -[ -n "$PYENV_DEBUG" ] && set -x +[[ -n "$PYENV_DEBUG" ]] && set -x SHIM_PATH="${PYENV_ROOT}/shims" PROTOTYPE_SHIM_PATH="${SHIM_PATH}/.pyenv-shim" @@ -15,13 +15,15 @@ declare last_acquire_error acquire_lock() { # Ensure only one instance of pyenv-rehash is running at a time by # setting the shell's `noclobber` option and attempting to write to - # the prototype shim file. If the file already exists, print a warning - # to stderr and exit with a non-zero status. + # the prototype shim file. local ret 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 - [ -z "${ret}" ] + [[ -z "${ret}" ]] } remove_prototype_shim() { diff --git a/test/rehash.bats b/test/rehash.bats index e8edf675..77916005 100755 --- a/test/rehash.bats +++ b/test/rehash.bats @@ -38,6 +38,18 @@ pyenv: cannot rehash: couldn't acquire lock ${PYENV_ROOT}/shims/.pyenv-shim for 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" { create_alt_executable_in_version "2.7" "python" create_alt_executable_in_version "2.7" "fab"