Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions scripts/__tests__/pack-lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string, string>;
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}"
Expand Down Expand Up @@ -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"');
});
Expand Down
7 changes: 4 additions & 3 deletions scripts/check-pack-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Severity: P3 Low
Problem: Moving the trap before acquire does not close the window the comment describes. The leak window is between mkdir "$lock" succeeding and printf ... > "$lock/owner" completing, and the release in this trap cannot clean up in that window:

  • If the signal lands before PACK_LOCK_TOKEN is assigned, pack_lock_release returns at its first line ([[ -n "$PACK_LOCK_TOKEN" ]] || return 0) and the lock stays.
  • If it lands after the token is set but before owner is written, release reads an empty owner. That is not equal to the token, so it takes the "held by another run" branch and leaves the directory.

I checked the second case with mkdir lk; PACK_LOCK_DIR=lk; PACK_LOCK_TOKEN=1.2; pack_lock_release; ls -d lk. It prints "held by another run now; left it alone" (and the new ::warning annotation under Actions), and lk is still there.
Impact: A SIGTERM or SIGINT (job cancel, timeout) in that sub-millisecond window still leaves an ownerless lock. Every later run then waits 300 s and fails with "stale lock?", which is the failure the first fix in this PR addresses. The new annotation would blame "another run" for it.
Scenario: The window is tiny, so this is rare. The point is that the PR text and this comment claim it is closed, and the new test only checks the textual order of trap and pack_lock_acquire in the source. It never exercises the window.
Fix: Either:

  • in pack_lock_acquire, set PACK_LOCK_DIR and PACK_LOCK_TOKEN before mkdir, and clear them if mkdir fails; and in pack_lock_release, treat "owner empty and token set" as ours to remove only when we have just created the directory (for example, a PACK_LOCK_CREATED=1 flag set right after a successful mkdir); or
  • soften the comment and PR description to say this narrows the window rather than closing it.

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; }
Expand Down
29 changes: 25 additions & 4 deletions scripts/pack-lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <lock dir> [seconds] waits (default 300 s); returns 1 on timeout
# pack_lock_acquire <lock dir> [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
Expand All @@ -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))
Expand All @@ -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=""
Expand All @@ -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
}
Loading