Skip to content

Race condition when starting multiple terminals? pyenv: cannot rehash: .pyenv-shim exists #2829

Closed

Description

@fsc-eriker

This morning when I logged in after an OS update (MacOS Sonoma 14.1) I got -bash: python: command not found in all my restarted terminals.

In one of them, I could also see

pyenv: cannot rehash: /Users/fsc-eriker/.pyenv/shims/.pyenv-shim exists

Of course, the file no longer existed by the time I checked.

Running pyenv rehash in one of the terminals resolved the problem in all of them.

My suspicion is that there is a race condition when many terminals start up in quick succession after a reboot, though I have been running pyenv for several years and this is the first time I see this problem.

Prerequisite

  • Make sure your problem is not listed in the common build problems.
  • Make sure no duplicated issue has already been reported in the pyenv issues. You should look for closed issues, too.
  • Make sure you are not asking us to help solving your specific issue.
    • GitHub issues is opened mainly for development purposes. If you want to ask someone to help solving your problem, go to some community site like Gitter, StackOverflow, etc.
  • Make sure your problem is not derived from packaging (e.g. Homebrew).
    • Please refer to the package documentation for the installation issues, etc.
  • Make sure your problem is not derived from plugins.
    • This repository is maintaining pyenv and the default python-build plugin only. Please refrain from reporting issues of other plugins here.

Description

  • Platform information: MacOS 14.1
  • OS architecture (e.g. amd64): Intel
  • pyenv version: 2.3.17
  • Python version: 3.11.3 (among others)
  • C Compiler information (e.g. gcc 7.3): clang I guess? I have 15.0.0
  • Please attach the debug trace of the failing command as a gist:
    • Run env PYENV_DEBUG=1 <faulty command> 2>&1 | tee trace.log and attach trace.log. E.g. if you have a problem with installing Python, run env PYENV_DEBUG=1 pyenv install -v <version> 2>&1 | tee trace.log (note the -v option to pyenv install).
  • If you have a problem with installing Python, please also attach config.log from the build directory
    • The build directory is reported after the "BUILD FAILED" message and is usually under /tmp.
  • If the build succeeds but the problem is still with the build process (e.g. the resulting Python is missing a feature), please attach
    • the debug trace from reinstalling the faulty version with env PYENV_DEBUG=1 pyenv install -f -k -v <version> 2>&1 | tee trace.log
    • config.log from the build directory. When using pyenv install with -k as per above, the build directory will be under $PYENV_ROOT/sources.

I have pyenv-virtualenv plugin but I imagine it is not involved in this issue. Unfortunately, because there is no way to repro reliably, I cannot rule out that this is caused by Homebrew or the plugin or etc.

Several old issues seem related but too old to be directly relevant as duplicates; #1092, #1145, maybe #1137 and #1747.

Activity

  1. native-api commented on Oct 28, 2023

    @native-api
    Member

    pyenv: cannot rehash: /Users/fsc-eriker/.pyenv/shims/.pyenv-shim exists

    My suspicion is that there is a race condition when many terminals start up in quick succession after a reboot

    That's exactly it.

    Does that cause a problem though? All terminal instances should be fine if at least one rehash has succeeded.

  2. native-api commented on Oct 28, 2023

    @native-api
    Member

    because there is no way to repro reliably

    touch "${PYENV_ROOT}/shims/.pyenv-shim"

    That's what one of our tests uses to model this situation.

  3. fsc-eriker commented on Oct 30, 2023

    @fsc-eriker
    ContributorAuthor

    Does that cause a problem though? All terminal instances should be fine if at least one rehash has succeeded.

    Indeed, the ticket is about a case where this did not happen, i.e. none of the terminal instances was fine (as far as I was able to check; I did not individually review more than 5-6 -- I'm guessing maybe one of them must have succeeded eventually, but which one?)

    This morning when I logged in after an OS update (MacOS Sonoma 14.1) I got -bash: python: command not found in all my restarted terminals.

  4. fsc-eriker commented on Oct 30, 2023

    @fsc-eriker
    ContributorAuthor

    As a tentative workaround I changed my .bashrc to run eval "$(pyenv init - --no-rehash)" and instead set up a separate login item to run eval "$(pyenv init -)" just once. I don't think I can know whether it works until much later.

  5. native-api commented on Nov 1, 2023

    @native-api
    Member

    I cannot reproduce the problem. pyenv-rehash is designed for this situation: it uses a lock file and waits with a 60 second timeout if it's already held before bailing out.
    Moreover, the stock startup code silences error messages from pyenv rehash. So you may have local customizations in place.

    Make sure you have the latest, stock version of Pyenv.
    If it's still a problem, please provide the debug trace demonstrating the problem as per the issue template.

  6. github-actions commented on Dec 1, 2023

    @github-actions
    Contributor

    This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

  7. rsantellan commented on Jan 31, 2024

    @rsantellan

    After installing on LMDE 6 (Debian 12) I'm having the same problem, on startup guake is started an when I try to open a terminal it shows on guake:

    pyenv: cannot rehash: ~/.pyenv/shims/.pyenv-shim exists

    Is pretty common issue for me (I turn off the laptop every day and work mainly with a terminal).

    How can I provide more information to this issue ?

  8. native-api commented on Jan 31, 2024

    @native-api
    Member

    @rsantellan Use the issue template to provide the information necessary to diagnose a problem. It appears when you create a new issue.

  9. sasa-buklijas commented on Oct 18, 2024

    @sasa-buklijas

    just FYI
    I had same problem because machine had 1% free ssd space.
    it was remote (ssh only) server for testing

  10. techwizrd commented on May 16, 2026

    @techwizrd

    I’m seeing what looks like the same failure mode on macOS with a newer pyenv, but with a more visible shell-startup impact.

    Environment:

    • macOS
    • fish
    • pyenv 2.6.26
    • installed via Homebrew

    What happens:

    • starting a new interactive fish shell was taking about 60 seconds
    • the slowdown came from pyenv init during shell startup
    • pyenv init emits command pyenv rehash
    • pyenv rehash then waits the full 60 second timeout trying to acquire the shim lock/prototype file

    Observed error:

    pyenv: cannot rehash: couldn't acquire lock ~/.pyenv/shims/.pyenv-shim for 60 seconds. Last error message:
    pyenv-rehash: line 22: ~/.pyenv/shims/.pyenv-shim: cannot overwrite existing file
    

    In my case, the file was still present after the failure:

    ~/.pyenv/shims/.pyenv-shim
    

    This looks like a stale/orphaned lock/prototype file case, not a sandbox writability issue.

    Measurements:

    • fish --no-config -i -c exit: about 0.01s
    • normal fish -i -c exit before fix: about 60.6s
    • after removing the stale .pyenv-shim and changing shell init to pyenv init - --no-rehash fish | source: about 0.28s to 0.34s

    So there seem to be two separate concerns:

    1. a stale .pyenv-shim can block pyenv rehash for the full timeout
    2. unconditional rehash in pyenv init makes that failure hit every new shell

    I realize --no-rehash is a workable user-side fix, but I wanted to add evidence that the .pyenv-shim exists problem can still translate into a consistent 60-second shell startup delay when the lock file is left behind.

    If useful, I can provide more details from local testing.

  11. native-api commented on May 17, 2026

    @native-api
    Member

    I guess we can assume that a lockfile older than, say, 10 minutes is stale.

    We still don't know how this happens but this will at least unblock affected users.

  12. deleted a comment from github-actions on May 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions