Repository navigation
Make pyenv-rehash safer for multiple processes - #1092
clintharrison wants to merge 3 commits into
Conversation
|
There's some discussion on this on Gitter |
| # 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 |
There was a problem hiding this comment.
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.
Lines 13 to 27 in a8e207f
Please modify the code around the noclobber to,
- Use
flockorshlockif it is available on the platform (otherwise, fallback tonoclobber) - 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.
There was a problem hiding this comment.
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.
| remove_prototype_shim() { | ||
| rm -f "$PROTOTYPE_SHIM_PATH" | ||
| if [ -n "$PYENV_REHASH_PARALLEL_SAFE" ]; then | ||
| release_lock |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
This function is already registered as an exit trap, but I've renamed it so hopefully its purpose is clearer.
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.
b6a3cec to
680a480
Compare
|
@yyuu any updates here? 🙂 |
|
Ping :) |
yyuu
left a comment
There was a problem hiding this comment.
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.
| # 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 |
There was a problem hiding this comment.
POSIX's sleep(1) doesn't support subsecond. It'd be better to regard 1 second as the minimum sleep duration.
| SHIM_PATH="${PYENV_ROOT}/shims" | ||
| PROTOTYPE_SHIM_PATH="${SHIM_PATH}/.pyenv-shim" | ||
| LOCK_FILE_PATH="${SHIM_PATH}/.pyenv.lock" | ||
| LOCK_FILE_FD=90 |
There was a problem hiding this comment.
At least I don't think it's good idea to hardcode fd of lock file in the script....
| 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 |
There was a problem hiding this comment.
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.
| @test "rehash in progress" { | ||
| # Skip this when flock/shlock are available | ||
| if [ -n $(command -v flock) ] || [ -n $(command -v shlock) ]; then | ||
| skip |
There was a problem hiding this comment.
This effectively skips rehash test on Travis CI, etc. It's not good at least.
We encountered the same error (in the same parallelizing-build-system setting) as #174, as the hook around
pip installoccasionally ran at the same time. Sincerehashis a relatively quick operation, andpip installoperations 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 thePYENV_REHASH_PARALLEL_SAFEenvironment 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 includedshlockand achieve reasonable success.