Skip to content
Merged
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
172 changes: 172 additions & 0 deletions scripts/__tests__/pack-lock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { spawn, spawnSync } from 'node:child_process';
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

/**
* The lock check-pack-install.sh takes around `npm pack`, run as real concurrent bash processes.
*
* It used to release with a bare `rmdir "$lock"` under `set -e`, which failed a run with "rmdir:
* failed to remove '/tmp/bugsee-pack-install.lock.d': No such file or directory" whenever the lock
* was already gone (the `pack install (0.82-0.86)` job failed that way on roughly one run in four),
* and removed another run's lock if one had taken it. Release now checks ownership and is
* idempotent.
*/
const lib = join(__dirname, '..', 'pack-lock.sh');
const script = join(__dirname, '..', 'check-pack-install.sh');

interface Result {
status: number | null;
stdout: string;
stderr: string;
}

/** Runs `body` in a bash that has sourced the library, with `set -euo pipefail` as the real script has. */
function run(body: string, env: Record<string, string> = {}): Result {
const result = spawnSync('bash', ['-c', `set -euo pipefail\nsource "${lib}"\n${body}`], {
encoding: 'utf8',
env: { ...process.env, PACK_LOCK_POLL: '0.02', ...env },
});
return { status: result.status, stdout: result.stdout, stderr: result.stderr };
}

function runAsync(body: string): Promise<Result> {
return new Promise((resolve) => {
const child = spawn('bash', ['-c', `set -euo pipefail\nsource "${lib}"\n${body}`], {
env: { ...process.env, PACK_LOCK_POLL: '0.01' },
});
let stdout = '';
let stderr = '';
child.stdout.on('data', (chunk) => (stdout += chunk));
child.stderr.on('data', (chunk) => (stderr += chunk));
child.on('close', (status) => resolve({ status, stdout, stderr }));
});
}

describe('pack-lock.sh', () => {
let dir: string;
let lock: string;

beforeEach(() => {
dir = mkdtempSync(join(tmpdir(), 'pack-lock-'));
lock = join(dir, 'lock.d');
});
afterEach(() => rmSync(dir, { recursive: true, force: true }));

it('takes the lock, records its owner and removes it on release', () => {
const result = run(`pack_lock_acquire "${lock}" 5
test -d "${lock}"
test -s "${lock}/owner"
pack_lock_release
test ! -e "${lock}"`);
expect(result).toMatchObject({ status: 0, stderr: '' });
});

it('serializes real concurrent processes: no two are in the critical section together', async () => {
const guard = join(dir, 'inside.d');
const log = join(dir, 'log');
writeFileSync(log, '');
const worker = (id: number) => `
pack_lock_acquire "${lock}" 60
if ! mkdir "${guard}" 2>/dev/null; then echo "OVERLAP ${id}" >> "${log}"; fi
sleep 0.03
rmdir "${guard}" 2>/dev/null || true
echo "done ${id}" >> "${log}"
pack_lock_release`;
const results = await Promise.all(Array.from({ length: 12 }, (_, id) => runAsync(worker(id))));
expect(results.map((r) => r.status)).toEqual(results.map(() => 0));
const lines = readFileSync(log, 'utf8').trim().split('\n');
expect(lines.filter((line) => line.startsWith('OVERLAP'))).toEqual([]);
expect(lines.filter((line) => line.startsWith('done'))).toHaveLength(12);
expect(existsSync(lock)).toBe(false);
});

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
rm -rf "${lock}"
pack_lock_release
echo survived`);
expect(result.status).toBe(0);
expect(result.stdout).toContain('survived');
expect(result.stderr).toContain('was removed while it was held');
});

it('never removes a lock another run took after ours was lost', () => {
const result = run(`pack_lock_acquire "${lock}" 5
rm -rf "${lock}"
mkdir "${lock}"
echo "someone-else" > "${lock}/owner"
pack_lock_release
test -d "${lock}"
test "$(cat "${lock}/owner")" = someone-else
echo intact`);
expect(result.status).toBe(0);
expect(result.stdout).toContain('intact');
expect(result.stderr).toContain('held by another run now');
});

it('is idempotent: a trap and an explicit release can both run', () => {
const result = run(`trap 'pack_lock_release' EXIT
pack_lock_acquire "${lock}" 5
pack_lock_release
pack_lock_release
echo ok`);
expect(result).toMatchObject({ status: 0, stderr: '' });
expect(existsSync(lock)).toBe(false);
});

it('does not release a lock it never took', () => {
// Another run holds it; this one only released, never acquired.
run(`mkdir "${lock}"; echo other > "${lock}/owner"`);
const result = run('pack_lock_release; echo ok');
expect(result).toMatchObject({ status: 0, stderr: '' });
expect(existsSync(lock)).toBe(true);
});

it('releases from the EXIT trap when the run dies while holding it', () => {
const result = run(`trap 'pack_lock_release' EXIT
pack_lock_acquire "${lock}" 5
exit 3`);
expect(result.status).toBe(3);
expect(existsSync(lock)).toBe(false);
});

it('gives up after the wait, with the stale-lock message, and leaves the holder alone', () => {
run(`mkdir "${lock}"; echo holder > "${lock}/owner"`);
const result = run(`pack_lock_acquire "${lock}" 0.1 || echo "returned $?"`);
expect(result.stdout).toContain('returned 1');
expect(result.stderr).toContain(`FAIL: could not take ${lock} (stale lock? remove it)`);
expect(readFileSync(join(lock, 'owner'), 'utf8').trim()).toBe('holder');
});

it('still tries once when the wait is shorter than one poll', () => {
expect(run(`pack_lock_acquire "${lock}" 0 && echo got`).stdout).toContain('got');
});

it('does not write its token anywhere but the lock', () => {
const result = run(`pack_lock_acquire "${lock}" 5
echo "$PACK_LOCK_TOKEN"
pack_lock_release`);
expect(result.stdout.trim()).toMatch(/^\d+\.\d+$/);
});
});

describe('check-pack-install.sh takes the lock through the library', () => {
const source = readFileSync(script, 'utf8');

it('releases only through pack_lock_release', () => {
// The bare rmdir is what failed the run when the lock was already gone.
expect(source).not.toMatch(/rmdir\s+"\$PACK_LOCK"/);
expect(source).toContain('pack_lock_release');
});

it('releases from the EXIT trap as well as explicitly', () => {
expect(source).toMatch(/trap 'pack_lock_release;[^']*' EXIT/);
expect(source.match(/^pack_lock_release$/gm)).toHaveLength(1);
});

it('sources the library from the repo, not the working directory', () => {
expect(source).toContain('source "$REPO/scripts/pack-lock.sh"');
});
});
20 changes: 9 additions & 11 deletions scripts/check-pack-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ mkdir -p "$WORK/tarballs"
# prepack/postpack stage and unstage native-versions.json inside the package
# directory, which parallel runs of this script (scripts/ci-each-version.sh)
# share: one run's postpack would delete the file under another's pack. A
# mkdir lock (flock is not on macOS) serializes just this step.
PACK_LOCK="${TMPDIR:-/tmp}/bugsee-pack-install.lock.d"
PACK_LOCK_HELD=0
for _ in $(seq 1 300); do
if mkdir "$PACK_LOCK" 2>/dev/null; then PACK_LOCK_HELD=1; break; fi
sleep 1
done
[[ "$PACK_LOCK_HELD" -eq 1 ]] || { echo "FAIL: could not take $PACK_LOCK (stale lock? remove it)" >&2; exit 1; }
# A kill between mkdir and rmdir must not leave the lock behind.
trap 'rmdir "$PACK_LOCK" 2>/dev/null; [[ -n "${PACK_INSTALL_DIR:-}" ]] || rm -rf "$WORK"' EXIT
# mkdir lock (scripts/pack-lock.sh; flock is not on macOS) serializes just this
# 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.
trap 'pack_lock_release; [[ -n "${PACK_INSTALL_DIR:-}" ]] || rm -rf "$WORK"' EXIT

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: The EXIT trap is installed after pack_lock_acquire. A SIGINT or SIGTERM between mkdir and this line leaves the lock behind, and the new token write widens that window slightly. The pre-existing code had the same gap.

Impact: The leaked lock makes every other run wait 300 s and then fail. This matters mainly on a developer machine or a cancelled job on a reused host. Hosted runners are ephemeral.

Fix: pack_lock_release is now idempotent and does nothing without a token. The trap can therefore be set before the acquire, with the same body:

trap 'pack_lock_release; [[ -n "${PACK_INSTALL_DIR:-}" ]] || rm -rf "$WORK"' EXIT
pack_lock_acquire ... || exit 1

This is cheap. The pin test trap 'pack_lock_release;[^']*' EXIT still matches.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed: the EXIT trap is now installed before pack_lock_acquire, with the same body (the release does nothing without a token, so it is safe before one exists). A test pins that the trap comes before the acquire, and fails against the previous order.

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 All @@ -67,7 +65,7 @@ done
# runs. Parallel first-time `npx --yes` installs into ~/.npm/_npx corrupt each
# other (TAR_ENTRY_ERROR ENOENT); once warm, later runs only read it.
[[ "$PACK_RC" -eq 0 ]] && { npx --yes "@react-native-community/cli@${RN_CLI_VERSION:-20.2.0}" --version >/dev/null || { echo "FAIL: npx warm-up" >&2; PACK_RC=1; }; }
rmdir "$PACK_LOCK"
pack_lock_release
if [[ -n "${PACK_INSTALL_DIR:-}" ]]; then trap - EXIT; else trap 'rm -rf "$WORK"' EXIT; fi
[[ "$PACK_RC" -eq 0 ]] || { echo "FAIL: npm pack or npx warm-up (see above)" >&2; exit 1; }
CORE_TGZ="$(ls "$WORK"/tarballs/bugsee-react-native-[0-9]*.tgz)"
Expand Down
61 changes: 61 additions & 0 deletions scripts/pack-lock.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bash
#
# A mkdir lock (flock is not on macOS) that is only ever released by the process that took it.
# Sourced by check-pack-install.sh, whose parallel runs (scripts/ci-each-version.sh) share one lock
# 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_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
# removed whatever sat at that path, and failed the whole run with "rmdir: failed to remove ...: No
# such file or directory" when the directory was already gone, although the pack it guarded had
# finished. A holder now writes a token into the lock, and release removes the lock only if the
# token is still its own. A lock that is gone or has been taken by someone else is reported, not
# removed and not fatal: serialization already failed by then, and failing the run as well, or
# deleting another run's lock, only spreads it.
#
# PACK_LOCK_POLL is the wait between attempts, in seconds (default 1; tests use less).

PACK_LOCK_DIR=""
PACK_LOCK_TOKEN=""

pack_lock_acquire() {
local lock="$1" seconds="${2:-300}" poll="${PACK_LOCK_POLL:-1}" waited=0
# Attempts, not seconds, so a fractional poll still gives the caller's total wait.
local attempts
attempts=$(awk -v s="$seconds" -v p="$poll" 'BEGIN { n = int(s / p); print (n < 1 ? 1 : n) }')
while [[ "$waited" -lt "$attempts" ]]; do
if mkdir "$lock" 2>/dev/null; then
PACK_LOCK_DIR="$lock"
PACK_LOCK_TOKEN="$$.$RANDOM$RANDOM"
printf '%s\n' "$PACK_LOCK_TOKEN" > "$lock/owner"

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: P2 Medium

Problem: A failed token write is swallowed, so the run reports success and the lock directory is leaked. check-pack-install.sh calls pack_lock_acquire ... || exit 1, which disables set -e inside the function. A failing printf ... > "$lock/owner" therefore only prints an error, and the function still returns 0 with PACK_LOCK_TOKEN set. At release owner is empty, which differs from the token. The function takes the "held by another run" branch and leaves the directory in place. rmdir ... || true hides any similar failure.

Impact: The old rmdir would have cleaned this up. Now every later run waits the full 300 s and fails with "stale lock? remove it", and the warning blames "another run".

Scenario: I reproduced it on this checkout with umask 0222, so the lock dir is created read-only. The output was:

line 33: /tmp/t1/l.d/owner: Permission denied
acquired rc=0
warning: /tmp/t1/l.d is held by another run now; left it alone

ls then showed l.d still present. The same happens with ENOSPC or EROFS on $TMPDIR. A SIGKILL between mkdir and this write leaves an ownerless lock the same way, as it did before. The write is only a fresh failure source.

Fix: Treat an unwritable token as a failed acquire and undo it:

if ! printf '%s\n' "$PACK_LOCK_TOKEN" > "$lock/owner"; then
  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

Add a test for it. None of the 13 covers a failed write.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in the latest push, as suggested: a failed token write removes the (empty or partial) owner, removes the lock directory, clears the state, prints FAIL: could not record the owner in <lock> and returns 1, so the acquire fails instead of leaking an ownerless lock. The test overrides printf to fail (the redirect still creates owner, so it also checks that it is cleaned up) rather than relying on file permissions, which root ignores; it asserts the lock is gone, the acquire returned 1 and the lock can be taken again, and a second test that a later release does not claim 'held by another run'. Both fail against the previous version.

return 0
fi
waited=$((waited + 1))
sleep "$poll"
done
echo "FAIL: could not take $lock (stale lock? remove it)" >&2
return 1
}

pack_lock_release() {
[[ -n "$PACK_LOCK_TOKEN" ]] || return 0
local lock="$PACK_LOCK_DIR" token="$PACK_LOCK_TOKEN" owner=""
# Cleared first: a trap that fires while this runs must not release twice.
PACK_LOCK_DIR=""
PACK_LOCK_TOKEN=""
owner="$(cat "$lock/owner" 2>/dev/null || true)"
if [[ "$owner" == "$token" ]]; then
rm -f "$lock/owner"
rmdir "$lock" 2>/dev/null || true
return 0
fi
if [[ -e "$lock" ]]; then
echo "warning: $lock is held by another run now; left it alone" >&2
else
echo "warning: $lock was removed while it was held, so the pack was not serialized" >&2

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: The only signal that serialization failed is a plain stderr line. ci-each-version.sh writes each run's output to a log and prints it in a collapsed ::group:: only after all runs finish. A run that hit this branch exits 0, so nobody sees the line.

Impact: The fix turns a loud failure into a silent pass. The overlapping npm pack the lock exists to prevent can still happen. If it does, the next failure is probably a confusing missing native-versions.json in a later step, with the cause buried in a green job's log. The unexplained directory removal is also the only clue to the real root cause, and a quiet line makes it likely to be missed.

Fix: Emit it as an annotation, for example echo "::warning::$lock was removed while it was held..." when GITHUB_ACTIONS is set. Alternatively ci-each-version.sh could grep the logs for the warning and surface it. Without this the root cause stays hard to catch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, this was the weak point: the fix turned a loud failure into a quiet pass and buried the only clue to the real cause. pack_lock_warn now also emits ::warning title=pack-install lock::... on stdout when GITHUB_ACTIONS is set, for both the lost-lock and the taken-by-another-run cases (the runner parses it when ci-each-version.sh cats the log, so it lands on the run summary), and the text says nothing in this repository removes the directory and asks for the run to be reported. The stderr line stays for reads outside Actions. Tests cover both cases on Actions and no annotation off Actions.

fi
return 0
}
Loading