Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyenv init getting into an infinite loop when invoked from bashrc #264

Closed
konstantint opened this issue Oct 31, 2014 · 8 comments
Closed

pyenv init getting into an infinite loop when invoked from bashrc #264

konstantint opened this issue Oct 31, 2014 · 8 comments

Comments

@konstantint
Copy link
Contributor

@konstantint konstantint commented Oct 31, 2014

The current installation instructions suggest to place the line
eval "$(pyenv init -)"
at the end of .bash_profile or .bashrc depending on the Linux flavor.

This suggestion may lead to the following scenario:

  1. The user puts it into .bashrc because that's where he is used to placing bash environment configuration.
  2. The corresponding Linux flavor has bash configured to execute .bashrc on each bash invocation (not sure about Ubuntus, but this seems to be normal behaviour for many Linux flavors).
  3. On login, the .bashrc is executed
  4. The code in .bashrc reaches "pyenv init"
  5. pyenv is an executable script which has #!/usr/bin/env bash specified as the interpreter
  6. Consequently, bash is fired up to execute the script.
  7. The new bash executes .bashrc
  8. GOTO 4

Obviously, the immediate fix for the problem is to put the initialization line into .bash_profile rather than .bashrc, however at the point in time, when the user encounters this error, this is far from obvious (in particular, this has been the reason for me to not start using pyenv the first time I tried it and had no time to debug the problem, and now it took quite some time to figure out what's happening).

It would be great to find some nice way to prevent this possibility for an infinite loop somehow. The options I'd see, in order of niceness:

  • Get rid of the need to run a script on initialization completely. Once I'm adding the $PYENV_ROOT/bin to the path manually on installation, I could as well add the $PYENV_ROOT/shims there in the same command line.
  • Have #!/usr/bin/env sh in the script header (this way bash won't load bashrc) and make sure nothing breaks.
  • If nothing else, at least fix the current documentation to mention the possibility of this caveat in red blinking letters somewhere.
@yyuu

This comment has been minimized.

Copy link
Collaborator

@yyuu yyuu commented Nov 2, 2014

It is pretty weird. I couldn't reproduce such strange behaviour at least on Debian/Ubuntu/OS X. The ~/.bashrc must not be read from non-interactive shell and it should not causes infinite loops.

http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

The followings are my answers:

  1. You can use pyenv without invoking pyenv init -. Only few of pyenv commands (e.g. pyenv shell) require pyenv init -.
  2. pyenv is written in bash and cannot run with standard POSIX sh
  3. It would be better to be documented if there is possibility of infinite loops. But first, we need to clarify the problem. Please describe your platform and shell information (version, configurations, etc.)
@yyuu yyuu added the type: question label Nov 2, 2014
@konstantint

This comment has been minimized.

Copy link
Contributor Author

@konstantint konstantint commented Nov 2, 2014

  1. The more reasons there are to consider a refactoring where there is no need to use pyenv init at all.
  2. Yes, I was suspecting that, there was a vague hope the required changes would not be too drastic.
  3. This is not an out-of-the-box behaviour, but it is enabled by setting BASH_ENV=~/.bashrc, which is a fairly common configuration practice (I've seen many systems where this has been enabled by default, perhaps because this is a kind of a textbook configuration example). My example stems from a CentOS system and I suspect I was not the one to set the BASH_ENV in my .bash_profile.
@yyuu

This comment has been minimized.

Copy link
Collaborator

@yyuu yyuu commented Nov 4, 2014

I confirmed that this doesn't reproduce with out-of-box Debian/Ubuntu. I created a Docker container for reproduction, but the .bashrc bundled with those distributions will just return if $PS1 is not defined (=~ non-interactive).

% docker run -it yyuu/pyenv:issue264 bash -l
this is .bashrc
this is .profile
root@ddbcabcb80ef:/# 

I am wondering if I should fix this in pyenv script. This is caused by a kind miss configuration of the system, I think. Though, this is pretty easy to reproduce. Mentioning about potentially infinite loop in Wiki or README might be sufficient.

This is the Dockerfile. (you can also pull the image from DockerHub)

FROM ubuntu:14.04
MAINTAINER Yamashita, Yuu <peek824545201@gmail.com>

ENV PATH /root/.pyenv/shims:/root/.pyenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get --quiet --yes update
RUN apt-get --quiet --yes upgrade
RUN apt-get --quiet --yes install build-essential curl git libbz2-dev libreadline-dev libsqlite3-dev libssl-dev patch zlib1g-dev

RUN git clone --quiet https://github.com/yyuu/pyenv.git /root/.pyenv
RUN cd /root/.pyenv && git reset --hard 35aed21
RUN echo 'export BASH_ENV="/root/.bashrc"' >> /etc/profile
RUN echo 'echo "this is .profile"' >> /root/.profile
RUN echo 'echo "this is .bashrc"' >> /root/.bashrc
RUN echo 'eval "$(pyenv init -)"' >> /root/.bashrc

## Enable infinite loop
#RUN sed -i.orig -e '/^\[ -z "\$PS1" \]/s/^/#/' /root/.bashrc 
@konstantint

This comment has been minimized.

Copy link
Contributor Author

@konstantint konstantint commented Nov 4, 2014

As I mentioned, I encountered this in CentOS rather than Debian. Apparently Debian/Ubuntu has the concept of only having .bashrc which has a hard-coded check for non-interactive shell in the beginning, whereas CentOS/RedHat has both a .bash_profile (which is run for login shells) and a .bashrc (which is meant to be run for non-interactive shells). In particular bash_profile sets BASH_ENV to point to .bashrc.

The most easy way to reproduce the infinite loop is to change the line

 RUN echo 'eval "$(pyenv init -)"' >> /root/.bashrc

to

 RUN echo 'eval "$(pyenv init -)"' > /root/.bashrc

in your Dockerfile, and then, for example, invoke pyenv.

Yes, I think that this should be mentioned in the docs somewhere next to the installation instructions that require adding $(pyenv init -) to the startup script.

@yyuu

This comment has been minimized.

Copy link
Collaborator

@yyuu yyuu commented Nov 4, 2014

Now I'm on the business trip and cannot try CentOS due to the
limited bandwidth.

Please give me back PRs for document improvements, or you can edit the Wiki
by yourself.

山下 優 (やました ゆう)
peek824545201@gmail.com

konstantint added a commit to konstantint/pyenv that referenced this issue Nov 4, 2014
yyuu added a commit that referenced this issue Nov 4, 2014
Warn about #264 in the README
@yyuu

This comment has been minimized.

Copy link
Collaborator

@yyuu yyuu commented Nov 29, 2014

Adding warning in README has already been merged.

@yyuu yyuu closed this Nov 29, 2014
@StevenACoffman

This comment has been minimized.

Copy link

@StevenACoffman StevenACoffman commented Mar 2, 2017

Might be of value to others, but I conditionally initialized pyenv to avoid this problem.

    if [ -n "$(type -t pyenv)" ] && [ "$(type -t pyenv)" = function ]; then
    #    echo "pyenv is already initialized"
        true
    else
        if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
        if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
    fi
@jfly

This comment has been minimized.

Copy link

@jfly jfly commented Jan 17, 2018

@StevenACoffman's solution didn't quite work for me on zsh, so I implemented a workaround using environment variables here:

# Don't initialize pyenv if it is already initialized.
# See: https://github.com/pyenv/pyenv/issues/264#issuecomment-358490657
if [ -n "$PYENV_LOADING" ]; then
    true
else
    if which pyenv > /dev/null 2>&1; then
        export PYENV_LOADING="true"
        eval "$(pyenv init -)"
        eval "$(pyenv virtualenv-init -)"
        unset PYENV_LOADING
    fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.