Skip to content

Make pyenv-rehash safer for multiple processes - #1092

Closed
clintharrison wants to merge 3 commits into
pyenv:masterfrom
clintharrison:feature/pyenv-rehash-parallel-safe
Closed

clintharrison wants to merge 3 commits into
pyenv:masterfrom
clintharrison:feature/pyenv-rehash-parallel-safe

Conversation

@clintharrison

Copy link
Copy Markdown

We encountered the same error (in the same parallelizing-build-system setting) as #174, as the hook around pip install occasionally ran at the same time. Since rehash is a relatively quick operation, and pip install operations can take a while, it isn't feasible to force our build system to serialize those steps.

Rather than simply emitting an error message and failing, it'd be nicer if users can opt in to using a file lock when running pyenv-rehash. This commit adds the PYENV_REHASH_PARALLEL_SAFE environment variable that can be used to accomplish this goal.

On a Linux system, this works well with flock. This unfortunately does not exist on macOS by default, but we can fall back to the included shlock and achieve reasonable success.

@yyuu

yyuu commented Feb 26, 2018

Copy link
Copy Markdown
Contributor

Comment thread libexec/pyenv-rehash Outdated
# is available, this script can acquire an exclusive file lock to ensure only
# one process is running pyenv-rehash.
if [ -n "$PYENV_REHASH_PARALLEL_SAFE" ]; then
wait_for_lock

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some code for (pseudo) multi-process locking in pyenv-rehash. In general, it'd be better to avoid having code for similar purpose in multiple places.

# 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.
set -o noclobber
{ echo > "$PROTOTYPE_SHIM_PATH"
} 2>/dev/null ||
{ if [ -w "$SHIM_PATH" ]; then
echo "pyenv: cannot rehash: $PROTOTYPE_SHIM_PATH exists"
else
echo "pyenv: cannot rehash: $SHIM_PATH isn't writable"
fi
exit 1
} >&2
set +o noclobber

Please modify the code around the noclobber to,

  1. Use flock or shlock if it is available on the platform (otherwise, fallback to noclobber)
  2. Wait until the release of lock instead of immediate exit as error

For now it's immediately existing as error if there's another pyenv-rehash process is running. However, it must be some understandable behavior to wait until the release of lock since there're some of internal calls of pyenv-rehash within pyenv and they're actually NOT expecting pyenv-rehash to fail.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I removed the environment variable to opt in to this behavior, and use flock or shlock when available. Unfortunately, without flock or shlock, there's no way to wait for the lock becoming available when we fall back to the noclobber variant. This limitation is fundamentally the reason for adding flock and shlock support.

Comment thread libexec/pyenv-rehash Outdated
remove_prototype_shim() {
rm -f "$PROTOTYPE_SHIM_PATH"
if [ -n "$PYENV_REHASH_PARALLEL_SAFE" ]; then
release_lock

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The release of lock need to be done as a EXIT trap (e.g. trap release_lock EXIT) to make sure the lock is released at the end of the script.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is already registered as an exit trap, but I've renamed it so hopefully its purpose is clearer.

Clint Harrison added 2 commits March 5, 2018 12:51
This commit removes the opt-in nature of flock/shlock and uses them
when available. The old behavior is still used when neither is
available, since it's behavior in bash we can always rely on.
@clintharrison
clintharrison force-pushed the feature/pyenv-rehash-parallel-safe branch from b6a3cec to 680a480 Compare March 5, 2018 20:50
@clintharrison

Copy link
Copy Markdown
Author

@yyuu any updates here? 🙂

@clintharrison

Copy link
Copy Markdown
Author

Ping :)

@yyuu yyuu mentioned this pull request Apr 19, 2018
5 tasks done

@yyuu yyuu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for delay. I've left few comments.

I've proposed my workaround for the issue as #1145. It is actually using the original code for the lock, but I believe it'll work sufficiently for most cases. Please have a look when you get some chance.

Comment thread libexec/pyenv-rehash
# On macOS/BSD, use shlock when flock is not installed
# Try forever to acquire the lock
while ! shlock -f "$LOCK_FILE_PATH" -p $$; do
sleep 0.5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

POSIX's sleep(1) doesn't support subsecond. It'd be better to regard 1 second as the minimum sleep duration.

https://www.unix.com/man-page/posix/1P/sleep/

Comment thread libexec/pyenv-rehash
SHIM_PATH="${PYENV_ROOT}/shims"
PROTOTYPE_SHIM_PATH="${SHIM_PATH}/.pyenv-shim"
LOCK_FILE_PATH="${SHIM_PATH}/.pyenv.lock"
LOCK_FILE_FD=90

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least I don't think it's good idea to hardcode fd of lock file in the script....

Comment thread libexec/pyenv-rehash
elif command -v shlock &>/dev/null; then
# On macOS/BSD, use shlock when flock is not installed
# Try forever to acquire the lock
while ! shlock -f "$LOCK_FILE_PATH" -p $$; do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my understanding, this lock relies on the file at $LOCK_FILE_PATH and there might be possibility of unwanted unlock by deletion of the file at the path. I think it's effectively similar as current pseudo lock mechanism based on noclobber.

Comment thread test/rehash.bats
@test "rehash in progress" {
# Skip this when flock/shlock are available
if [ -n $(command -v flock) ] || [ -n $(command -v shlock) ]; then
skip

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This effectively skips rehash test on Travis CI, etc. It's not good at least.

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.

2 participants