Skip to content
Myr

[New] `nvm install`: serialize concurrent installs of the same version

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Two `nvm install <same version>` runs could race on the version directory - one removing or replacing it while the other reads or writes it. Take a per-version advisory lock (an atomically-created directory under $NVM_DIR/.cache locks) around the binary/source install, so a second run of the same version waits for the first; installs of different versions never contend. NVM_INSTALL_LOCK_TIMEOUT (seconds, default 600) bounds the wait, after which nvm reports the lock path so a lock left by a killed install can be removed. NVM_INSTALL_LOCK_STALE (minutes, default 0 / off) opts into automatically stealing a lock older than that, for unattended or CI use.

authored by Jordan Harband · committed by Jordan Harband · parent 9570a22 · Browse files · 5 files+210 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+210 added−1 removed

  • tests:+123 added−0 removed
  • other:+87 added−1 removed

5 files+210 added−1 removed

added:test/fast/Unit tests/nvm_acquire_install_lock+56 added
1#!/bin/sh23cleanup() {4 [ -n "${NVM_DIR}" ] && [ -d "${NVM_DIR}" ] && rm -rf "${NVM_DIR}"5 unset -f die cleanup6 unset NVM_DIR NVM_INSTALL_LOCK NVM_INSTALL_LOCK_TIMEOUT NVM_INSTALL_LOCK_STALE \7 version lock OUTPUT EXIT_CODE8}910die() { echo "$@" ; cleanup ; exit 1; }1112: nvm.sh13\. ../../../nvm.sh1415type nvm_acquire_install_lock > /dev/null 2>&1 || die 'nvm_acquire_install_lock is not available'1617NVM_DIR="$(mktemp -d)"18[ -n "${NVM_DIR}" ] || die 'unable to create temp NVM_DIR'19version='v20.0.0'20lock="$(nvm_cache_dir)/locks/$(nvm_install_lock_name "${version}")"2122# An empty version is a no-op that succeeds and takes no lock.23unset NVM_INSTALL_LOCK24nvm_acquire_install_lock '' || die 'acquiring with an empty version should succeed as a no-op'25[ -z "${NVM_INSTALL_LOCK-}" ] || die 'an empty-version acquire should not record a lock'2627# Acquiring a free lock succeeds, creates the lock dir, and records its path.28nvm_acquire_install_lock "${version}" || die 'acquiring a free lock should succeed'29[ -d "${lock}" ] || die 'acquire should create the lock directory'30[ "${NVM_INSTALL_LOCK}" = "${lock}" ] || die 'acquire should record NVM_INSTALL_LOCK'3132# Release it so the following contention checks start from a held-by-someone-else state.33command rmdir "${lock}" 2>/dev/null34unset NVM_INSTALL_LOCK3536# A lock held by "another process", with no wait budget, fails and names the path.37command mkdir -p "${lock}"38OUTPUT="$(NVM_INSTALL_LOCK_TIMEOUT=0 nvm_acquire_install_lock "${version}" 2>&1)"; EXIT_CODE=$?39[ "${EXIT_CODE}" != "0" ] || die 'acquire should fail when the lock is held and the timeout is 0'40case "${OUTPUT}" in41 *"${lock}"*) ;;42 *) die "the timeout message should name the lock path; got >${OUTPUT}<" ;;43esac44[ -d "${lock}" ] || die 'a failed acquire must not remove the held lock'4546# By default, a held lock is never stolen no matter how old it is.47touch -t 202001010000 "${lock}" 2>/dev/null || die 'unable to age the lock dir'48NVM_INSTALL_LOCK_TIMEOUT=0 nvm_acquire_install_lock "${version}" 2>/dev/null && die 'an old lock must not be stolen unless NVM_INSTALL_LOCK_STALE is set'49[ -d "${lock}" ] || die 'the old lock should still be held'5051# With NVM_INSTALL_LOCK_STALE set, a sufficiently old lock is stolen and re-acquired.52unset NVM_INSTALL_LOCK53NVM_INSTALL_LOCK_STALE=1 NVM_INSTALL_LOCK_TIMEOUT=0 nvm_acquire_install_lock "${version}" || die 'a stale lock should be stolen when NVM_INSTALL_LOCK_STALE is set'54[ "${NVM_INSTALL_LOCK}" = "${lock}" ] || die 'stealing a stale lock should acquire it'5556cleanup
added:test/fast/Unit tests/nvm_install_lock_name+32 added
1#!/bin/sh23cleanup() {4 [ -n "${NVM_DIR}" ] && [ -d "${NVM_DIR}" ] && rm -rf "${NVM_DIR}"5 unset -f die cleanup6 unset NVM_DIR7}89die() { echo "$@" ; cleanup ; exit 1; }1011: nvm.sh12\. ../../../nvm.sh1314type nvm_install_lock_name > /dev/null 2>&1 || die 'nvm_install_lock_name is not available'1516NVM_DIR="$(mktemp -d)"17[ -n "${NVM_DIR}" ] || die 'unable to create temp NVM_DIR'1819# A normal version passes through unchanged.20[ "$(nvm_install_lock_name 'v20.0.0')" = 'v20.0.0' ] || die "v20.0.0 => >$(nvm_install_lock_name 'v20.0.0')<"2122# Dots, hyphens, plus, and underscores are all preserved.23[ "$(nvm_install_lock_name 'iojs-v1.0.0')" = 'iojs-v1.0.0' ] || die "iojs-v1.0.0 => >$(nvm_install_lock_name 'iojs-v1.0.0')<"2425# Path separators and other unsafe characters become underscores.26[ "$(nvm_install_lock_name 'lts/*')" = 'lts__' ] || die "lts/* => >$(nvm_install_lock_name 'lts/*')<"27[ "$(nvm_install_lock_name 'a b/c')" = 'a_b_c' ] || die "a b/c => >$(nvm_install_lock_name 'a b/c')<"2829# The empty string maps to the empty string (no trailing-newline artifact).30[ "$(nvm_install_lock_name '')" = '' ] || die "empty => >$(nvm_install_lock_name '')<"3132cleanup
added:test/fast/Unit tests/nvm_release_install_lock+35 added
1#!/bin/sh23cleanup() {4 [ -n "${NVM_DIR}" ] && [ -d "${NVM_DIR}" ] && rm -rf "${NVM_DIR}"5 unset -f die cleanup6 unset NVM_DIR NVM_INSTALL_LOCK version lock7}89die() { echo "$@" ; cleanup ; exit 1; }1011: nvm.sh12\. ../../../nvm.sh1314type nvm_release_install_lock > /dev/null 2>&1 || die 'nvm_release_install_lock is not available'1516NVM_DIR="$(mktemp -d)"17[ -n "${NVM_DIR}" ] || die 'unable to create temp NVM_DIR'18version='v20.0.0'19lock="$(nvm_cache_dir)/locks/$(nvm_install_lock_name "${version}")"2021# Releasing when nothing is held is a no-op that succeeds.22unset NVM_INSTALL_LOCK23nvm_release_install_lock || die 'releasing with no lock held should succeed'2425# After acquiring, releasing removes the lock directory and clears the marker.26nvm_acquire_install_lock "${version}" || die 'setup: acquire failed'27[ -d "${lock}" ] || die 'setup: lock dir should exist after acquire'28nvm_release_install_lock || die 'release should succeed'29[ ! -d "${lock}" ] || die 'release should remove the lock directory'30[ -z "${NVM_INSTALL_LOCK-}" ] || die 'release should clear NVM_INSTALL_LOCK'3132# Releasing again is a harmless no-op.33nvm_release_install_lock || die 'a second release should be a no-op'3435cleanup
modified:nvm.sh+83 added-1 removed
3228 unmodified lines
3229nvm_cache_dir() {3230 nvm_echo "${NVM_DIR}/.cache"3231}32323233# Turn a version into a filesystem-safe lock name. Versions that reach here are3234# already restricted to [0-9A-Za-z._+-], but be defensive about anything else.3235nvm_install_lock_name() {3236 command printf '%s' "${1-}" | command tr -c '0-9A-Za-z._+-' '_'3237}32383239# Acquire an advisory, per-version install lock so two concurrent3240# `nvm install <same version>` runs cannot race on the same version directory3241# (one removing/replacing it while the other reads or writes it). The lock is a3242# directory created with `mkdir`, which is atomic across POSIX filesystems.3243#3244# Tunables (env vars):3245# NVM_INSTALL_LOCK_TIMEOUT seconds to wait for a held lock (default 600)3246# NVM_INSTALL_LOCK_STALE minutes after which a lock is assumed abandoned3247# and stolen; 0 (default) never steals3248#3249# On success the lock path is recorded in NVM_INSTALL_LOCK for the matching3250# nvm_release_install_lock.3251nvm_acquire_install_lock() {3252 local VERSION3253 VERSION="${1-}"3254 if [ -z "${VERSION}" ]; then3255 return 03256 fi32573258 local LOCK_ROOT3259 LOCK_ROOT="$(nvm_cache_dir)/locks"3260 # If the lock directory can't be created, don't block the install over it.3261 command mkdir -p "${LOCK_ROOT}" 2>/dev/null || return 032623263 local LOCK3264 LOCK="${LOCK_ROOT}/$(nvm_install_lock_name "${VERSION}")"32653266 local TIMEOUT3267 TIMEOUT="${NVM_INSTALL_LOCK_TIMEOUT:-600}"3268 local STALE3269 STALE="${NVM_INSTALL_LOCK_STALE:-0}"32703271 local WAITED3272 WAITED=03273 local ANNOUNCED3274 ANNOUNCED=03275 while ! command mkdir "${LOCK}" 2>/dev/null; do3276 # Steal a lock left behind by a crashed install once it is old enough.3277 if [ "${STALE}" != '0' ] && [ -n "$(command find "${LOCK}" -maxdepth 0 -type d -mmin "+${STALE}" 2>/dev/null)" ]; then3278 nvm_err "Removing stale install lock for ${VERSION} (older than ${STALE} minute(s))"3279 command rm -rf "${LOCK}" 2>/dev/null3280 continue3281 fi3282 if [ "${WAITED}" -ge "${TIMEOUT}" ]; then3283 nvm_err "Timed out after ${TIMEOUT}s waiting for another install of ${VERSION} to finish."3284 nvm_err "If no other install is running, remove ${LOCK} and try again."3285 return 13286 fi3287 if [ "${ANNOUNCED}" -eq 0 ]; then3288 nvm_err "Waiting for another install of ${VERSION} to finish..."3289 ANNOUNCED=13290 fi3291 command sleep 13292 WAITED=$((WAITED + 1))3293 done32943295 NVM_INSTALL_LOCK="${LOCK}"3296 return 03297}32983299# Release the lock acquired by nvm_acquire_install_lock, if any.3300nvm_release_install_lock() {3301 if [ -n "${NVM_INSTALL_LOCK-}" ]; then3302 command rmdir "${NVM_INSTALL_LOCK}" 2>/dev/null || command rm -rf "${NVM_INSTALL_LOCK}" 2>/dev/null || true3303 unset NVM_INSTALL_LOCK3304 fi3305}33063307# args: pattern3308# Lists versions available in the local cache (not yet installed).3309# Returns version numbers like "v18.20.4", one per line, sorted.3310nvm_ls_cached() {
695 unmodified lines
4006 return 334007 fi4008 EXIT_CODE=04009 else4010 # Serialize concurrent installs of this version so two runs cannot race4011 # on its version directory (one replacing it while the other reads it).4012 if ! nvm_acquire_install_lock "${VERSION}"; then4013 return 14014 fi40154016 if [ "_${NVM_OS}" = "_freebsd" ]; then4017 # node.js and io.js do not have a FreeBSD binary4018 nobinary=1
34 unmodified lines
4053 NVM_NO_PROGRESS="${NVM_NO_PROGRESS:-${noprogress}}" NVM_OFFLINE="${NVM_OFFLINE}" nvm_install_source "${FLAVOR}" std "${VERSION}" "${NVM_MAKE_JOBS}" "${ADDITIONAL_PARAMETERS}"4054 EXIT_CODE=$?4055 fi4056 fi40574058 nvm_release_install_lock4059 fi40604061 if [ $EXIT_CODE -eq 0 ] && ! nvm_validate_install "${VERSION}"; then4062 nvm_err "The install of ${VERSION} reported success but failed verification; not activating it."
870 unmodified lines
4933 nvm_echo nvm_err nvm_grep nvm_cd \4934 nvm_die_on_prefix nvm_get_make_jobs nvm_get_minor_version \4935 nvm_has_solaris_binary nvm_is_merged_node_version \4936 nvm_is_natural_num nvm_is_version_installed nvm_validate_install \4937 nvm_install_lock_name nvm_acquire_install_lock nvm_release_install_lock \4938 nvm_list_aliases nvm_make_alias nvm_print_alias_path \4939 nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \4940 nvm_sanitize_path nvm_has_colors nvm_process_parameters \4941 nvm_node_version_has_solaris_binary nvm_iojs_version_has_solaris_binary \
5 unmodified lines
4947 nvm_process_nvmrc nvm_process_nvmrc_content nvm_nvmrc_invalid_msg \4948 nvm_write_nvmrc \4949 >/dev/null 2>&14950 unset NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \4869 NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS \4951 NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS NVM_INSTALL_LOCK \4952 NVM_COLORS INSTALLED_COLOR SYSTEM_COLOR \4953 CURRENT_COLOR NOT_INSTALLED_COLOR DEFAULT_COLOR LTS_COLOR \4954 >/dev/null 2>&14955 ;;
167 unmodified lines
modified:README.md+4 added
873 unmodified lines
874875The following environment variables can be set to configure `nvm install`:876877- `NVM_NO_SOURCE_FALLBACK` - when `1`, a failed binary download aborts instead of silently falling back to a (much slower) from-source compile; the persistent equivalent of the `-b` flag, and mutually exclusive with `-s`.878- `NVM_INSTALL_LOCK_TIMEOUT` - seconds to wait for a concurrent install of the same version to finish before giving up (default `600`). On timeout, nvm prints the lock path so a lock left behind by a killed install can be removed.879- `NVM_INSTALL_LOCK_STALE` - minutes after which an install lock is assumed abandoned and stolen automatically; `0` (the default) never steals.880881`nvm install <version>` takes a per-version advisory lock (a directory under `$NVM_DIR/.cache/locks`), so two shells installing the same version at once cannot corrupt its version directory; installs of *different* versions never block each other.882883884## Bash Completion885886To activate, you need to source `bash_completion`:887
329 unmodified lines