diff --git a/scripts/__tests__/pack-lock.test.ts b/scripts/__tests__/pack-lock.test.ts index af03a7c3..0e859d41 100644 --- a/scripts/__tests__/pack-lock.test.ts +++ b/scripts/__tests__/pack-lock.test.ts @@ -81,6 +81,32 @@ test ! -e "${lock}"`); expect(existsSync(lock)).toBe(false); }); + it('gives the lock back when the owner cannot be recorded, instead of leaking it', () => { + // A write that fails after mkdir (read-only temp dir, ENOSPC). `printf` is overridden so that the + // test does not depend on file permissions, which root ignores. The redirect still creates `owner`. + const result = run(`printf() { return 1; } +pack_lock_acquire "${lock}" 5 && echo "acquired" || echo "acquire returned $?" +test ! -e "${lock}" && echo "no lock left behind" +unset -f printf +pack_lock_acquire "${lock}" 1 && echo "free again"`); + expect(result.stdout).toContain('acquire returned 1'); + expect(result.stdout).not.toContain('\nacquired'); + expect(result.stdout).toContain('no lock left behind'); + expect(result.stdout).toContain('free again'); + expect(result.stderr).toContain(`could not record the owner in ${lock}`); + }); + + it('does not report a lock it gave back as held by another run on a later release', () => { + const result = run(`printf() { return 1; } +pack_lock_acquire "${lock}" 5 || true +unset -f printf +pack_lock_release +echo done`); + expect(result.stdout).toContain('done'); + expect(result.stderr).not.toContain('held by another run'); + expect(result.stderr).not.toContain('was removed while it was held'); + }); + it('does not fail the run when the lock has been removed while held', () => { // What the old `rmdir "$lock"` died on, under set -e. const result = run(`pack_lock_acquire "${lock}" 5 @@ -92,6 +118,49 @@ echo survived`); expect(result.stderr).toContain('was removed while it was held'); }); + describe('a lost lock is surfaced where a green job would show it', () => { + const lost = `pack_lock_acquire "${'${LOCK}'}" 5 +rm -rf "${'${LOCK}'}" +pack_lock_release`; + + it('prints a GitHub Actions warning annotation on stdout when running there', () => { + const result = run(lost.replace(/\$\{LOCK\}/g, lock), { GITHUB_ACTIONS: 'true' }); + expect(result.status).toBe(0); + expect(result.stdout).toContain('::warning title=pack-install lock::'); + expect(result.stdout).toContain('was removed while it was held'); + expect(result.stdout).toContain('please report this run'); + }); + + it('also annotates the case of a lock another run now holds', () => { + const result = run( + `pack_lock_acquire "${lock}" 5 +rm -rf "${lock}" +mkdir "${lock}" +echo other > "${lock}/owner" +pack_lock_release`, + { GITHUB_ACTIONS: 'true' }, + ); + expect(result.stdout).toContain('::warning title=pack-install lock::'); + expect(result.stdout).toContain('is held by another run now'); + }); + + it('prints no annotation outside GitHub Actions, only the stderr line', () => { + const env = { ...process.env } as Record; + delete env.GITHUB_ACTIONS; + const result = spawnSync('bash', ['-c', `set -euo pipefail\nsource "${lib}"\n${lost.replace(/\$\{LOCK\}/g, lock)}`], { + encoding: 'utf8', + env: { ...env, PACK_LOCK_POLL: '0.02' }, + }); + expect(result.stdout).not.toContain('::warning'); + expect(result.stderr).toContain('was removed while it was held'); + }); + + it('keeps the stderr line on Actions too, for a run read outside the annotations', () => { + const result = run(lost.replace(/\$\{LOCK\}/g, lock), { GITHUB_ACTIONS: 'true' }); + expect(result.stderr).toContain('warning: '); + }); + }); + it('never removes a lock another run took after ours was lost', () => { const result = run(`pack_lock_acquire "${lock}" 5 rm -rf "${lock}" @@ -166,6 +235,14 @@ describe('check-pack-install.sh takes the lock through the library', () => { expect(source.match(/^pack_lock_release$/gm)).toHaveLength(1); }); + it('installs the EXIT trap before it takes the lock, so a signal in between cannot leak it', () => { + const trapAt = source.search(/trap 'pack_lock_release;[^']*' EXIT/); + const acquireAt = source.indexOf('pack_lock_acquire "'); + expect(trapAt).toBeGreaterThan(-1); + expect(acquireAt).toBeGreaterThan(-1); + expect(trapAt).toBeLessThan(acquireAt); + }); + it('sources the library from the repo, not the working directory', () => { expect(source).toContain('source "$REPO/scripts/pack-lock.sh"'); }); diff --git a/scripts/check-pack-install.sh b/scripts/check-pack-install.sh index 2a7649cb..8a9cfdbc 100755 --- a/scripts/check-pack-install.sh +++ b/scripts/check-pack-install.sh @@ -53,10 +53,11 @@ mkdir -p "$WORK/tarballs" # step, and is released only by the run that took it. # shellcheck source=scripts/pack-lock.sh source "$REPO/scripts/pack-lock.sh" -pack_lock_acquire "${TMPDIR:-/tmp}/bugsee-pack-install.lock.d" 300 || exit 1 -# A kill between taking the lock and releasing it must not leave it behind. The -# release is idempotent, so this and the explicit one below can both run. +# Set BEFORE the lock is taken, so a signal between mkdir and the end of acquire cannot leave it +# behind. The release does nothing without a token and is idempotent, so this and the explicit one +# below can both run. trap 'pack_lock_release; [[ -n "${PACK_INSTALL_DIR:-}" ]] || rm -rf "$WORK"' EXIT +pack_lock_acquire "${TMPDIR:-/tmp}/bugsee-pack-install.lock.d" 300 || exit 1 PACK_RC=0 for pkg in react-native react-native-feedback; do (cd "$REPO/packages/$pkg" && npm pack --silent --pack-destination "$WORK/tarballs" >/dev/null) || { PACK_RC=1; break; } diff --git a/scripts/pack-lock.sh b/scripts/pack-lock.sh index cd3e6305..c2156f55 100755 --- a/scripts/pack-lock.sh +++ b/scripts/pack-lock.sh @@ -5,7 +5,8 @@ # around `npm pack`: prepack/postpack stage and unstage native-versions.json inside the package # directory, so one run's postpack would delete the file under another's pack. # -# pack_lock_acquire [seconds] waits (default 300 s); returns 1 on timeout +# pack_lock_acquire [seconds] waits (default 300 s); returns 1 on timeout, or when the owner +# cannot be recorded (the lock is then given back) # pack_lock_release idempotent; safe to call from an EXIT trap AND explicitly # # Why release checks ownership. The original release was a bare `rmdir "$lock"` under `set -e`. It @@ -30,7 +31,17 @@ pack_lock_acquire() { if mkdir "$lock" 2>/dev/null; then PACK_LOCK_DIR="$lock" PACK_LOCK_TOKEN="$$.$RANDOM$RANDOM" - printf '%s\n' "$PACK_LOCK_TOKEN" > "$lock/owner" + # Called as `pack_lock_acquire ... || exit 1`, where set -e does not apply inside, so a failed write + # must be handled here: a lock with no owner could never be released by its holder, and every later + # run would wait the full time and fail with a message blaming someone else. + if ! printf '%s\n' "$PACK_LOCK_TOKEN" > "$lock/owner"; then + rm -f "$lock/owner" 2>/dev/null || true + rmdir "$lock" 2>/dev/null || true + PACK_LOCK_DIR="" + PACK_LOCK_TOKEN="" + echo "FAIL: could not record the owner in $lock" >&2 + return 1 + fi return 0 fi waited=$((waited + 1)) @@ -40,6 +51,16 @@ pack_lock_acquire() { return 1 } +# A warning that cannot be missed. ci-each-version.sh prints each run's log inside a collapsed group only +# after every run finished, and a run that warned still exits 0, so a plain line on stderr would sit +# unseen in a green job. On GitHub Actions it is also an annotation, which shows on the run's summary. +pack_lock_warn() { + echo "warning: $1" >&2 + if [[ -n "${GITHUB_ACTIONS:-}" ]]; then + echo "::warning title=pack-install lock::$1" + fi +} + pack_lock_release() { [[ -n "$PACK_LOCK_TOKEN" ]] || return 0 local lock="$PACK_LOCK_DIR" token="$PACK_LOCK_TOKEN" owner="" @@ -53,9 +74,9 @@ pack_lock_release() { return 0 fi if [[ -e "$lock" ]]; then - echo "warning: $lock is held by another run now; left it alone" >&2 + pack_lock_warn "$lock is held by another run now; left it alone" else - echo "warning: $lock was removed while it was held, so the pack was not serialized" >&2 + pack_lock_warn "$lock was removed while it was held, so the pack was not serialized. Nothing in this repository removes it: please report this run" fi return 0 }