Skip to content

pack lock: give back the lock on a failed write, trap before acquire, annotate a lost lock - #193

Open
krassx wants to merge 1 commit into
mainfrom
fix/pack-lock-review-followups
Open

krassx wants to merge 1 commit into
mainfrom
fix/pack-lock-review-followups

Conversation

@krassx

@krassx krassx commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #192, which merged before the review fixes on it were pushed. Main has the ownership-checked lock; these three review findings are not in it.

What

  • A failed token write leaked the lock (P2). pack_lock_acquire is called as ... || exit 1, where set -e does not apply, so a failing printf > "$lock/owner" only printed an error and the function returned 0. The lock directory then stayed with no owner and every later run waited the full 300 s and failed with 'stale lock?'. A failed write now removes the partial owner and the lock directory, clears the state, prints FAIL: could not record the owner in <lock> and returns 1.
  • The EXIT trap is installed before the lock is taken (P3), so a signal between mkdir and the end of acquire cannot leave the lock behind. Release is idempotent and does nothing without a token, so this is safe.
  • A lost or taken lock is surfaced where a green job shows it (P3). The fix turned a loud failure into a quiet pass, with the only clue to the real cause on a stderr line inside a collapsed group of a job that exits 0. On GitHub Actions it is now also a ::warning title=pack-install lock::... annotation (the runner parses it when ci-each-version.sh prints the log), and the text says nothing in this repository removes the directory and asks for the run to be reported.

Verified

Five new tests fail against the code on main and pass here: the failed write (no lock left, acquire returns 1, the lock can be taken again, and a later release does not claim 'held by another run'; printf is overridden to fail so it does not depend on file permissions, which root ignores), the annotation for both the lost and the taken case, no annotation off Actions, and the trap-before-acquire order. yarn lint, yarn typecheck and yarn jest pass (5,280 tests).

Not verified

As in #192, what removes the lock directory on the hosted runner is still unknown. The annotation is there so that, if it recurs, the run summary says so.

Human guided, AI implemented

… annotate a lost lock

Follow-up to #192, which merged before these review fixes were pushed:

- A failed token write was swallowed (acquire is called as `|| exit 1`, where set -e
  does not apply), returning success and leaking an ownerless lock that every later
  run then waited 300 s for. A failed write now undoes the lock and fails the acquire.
- The EXIT trap is installed before the lock is taken, so a signal between mkdir and
  the end of acquire cannot leave it behind (release is idempotent and does nothing
  without a token).
- A lost or taken lock was only a plain stderr line inside a collapsed group of a job
  that exits 0, so nobody would see that serialization failed, nor the one clue to
  the real cause. On GitHub Actions it is now also a `::warning` annotation, and the
  text asks for the run to be reported.

New tests fail against the code on main: failed write (no leak, and no spurious
"held by another run" later), the annotation for both cases, no annotation off
Actions, and the trap-before-acquire order.

Human guided, AI implemented
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Pack installation now handles interruptions during lock setup more reliably, reducing the chance of a lock being left behind or blocking a later run.
    • Lock warnings are clearer when a lock is lost or its owner changes. In GitHub Actions, these warnings also appear as workflow annotations; outside Actions, they remain available in the command output.

Pack lock acquisition now reports failure when it cannot write the owner token, attempts to remove the incomplete lock, and clears its lock state. Lock-release warnings go to stderr and, when GITHUB_ACTIONS is set, also produce GitHub Actions warning annotations. The pack-install script installs its EXIT trap before lock acquisition. Tests cover cleanup, reacquisition, warning output, and trap ordering.

Change: Bug fix

Merge Risk: Low · up to c1d6a

Pack-lock failure handling is improved, and the new tests cover the main paths. A narrow race remains. If something outside the script removes the lock directory at the wrong moment, a failed owner write could delete another run's lock. The impact is limited to concurrent pack runs in CI and is unlikely to occur, so it is a bounded follow-up rather than a merge blocker.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the three lock-handling changes in the PR: giving back the lock on a failed owner write, installing the EXIT trap before acquire, and annotating a lost lock. The changeset covers each …
Description check Passed The description explains the follow-up to #192 and gives the three review findings: the leaked lock on a failed token write, the trap ordering, and the GitHub Actions annotation. It also lists the tes…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the three lock-handling changes in the PR: giving back the lock on a failed owner write, installing the EXIT trap before acquire, and annotating a lost lock. The changeset covers each of these, so the title matches the main change. It is long (90 characters, 17 words) but it is specific and related.

Full details: Description check

Explanation

The description explains the follow-up to #192 and gives the three review findings: the leaked lock on a failed token write, the trap ordering, and the GitHub Actions annotation. It also lists the tests added and the verification run. It matches the changeset, so the check passes.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 14bb2441-f1f5-4e66-9b4c-5052e94f7504
Commits

Reviewing files that changed from the base of the PR and between bad582d and c1d6ad6.

Files selected for processing: 3

  • scripts/__tests__/pack-lock.test.ts
  • scripts/check-pack-install.sh
  • scripts/pack-lock.sh

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@claude

claude Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Claude finished @krassx's task in 41s —— View job


Review of PR #193

I read the diff alongside scripts/pack-lock.sh, scripts/check-pack-install.sh and scripts/ci-each-version.sh. I did not run the Jest suite. I ran small bash repros of the release logic.

Overall risk: Low

Merge recommendation: Merge. The one finding is a P3: a comment overclaims, and a rare race is only narrowed, not closed.

Findings

P3 — The trap-before-acquire change does not close the mkdir→owner window (scripts/check-pack-install.sh:60, inline comment posted)

  • Release does nothing if the token is not set yet. If the token is set but owner is not written, release sees an empty owner and takes the "held by another run" branch. It leaves the directory and, under Actions, now emits a ::warning that blames another run.
  • I confirmed the second case with a repro: lk stays, and the warning is printed.
  • The new test only checks that trap appears before pack_lock_acquire in the source text, so it cannot catch this.
  • Fix: either track "we created the dir" before mkdir, so release can remove an ownerless directory it created, or soften the comment and PR description to say the window is narrowed.

Checked, no issues

  • Failed write path. rm -f owner, rmdir, clearing the state and return 1 are correct. The caller's || exit 1 now fires, and the EXIT trap is a no-op without a token. A later release does nothing, which the second new test covers.
  • Trap ordering. The new trap behaves the same as the old one on the exit 1 paths. This holds for both the mktemp case and the PACK_INSTALL_DIR case. The trap -/re-trap lines after the explicit release are unchanged and still consistent.
  • Annotation delivery. ci-each-version.sh writes each run's log with 2>&1 and cats it inside a ::group:: after all runs finish. The runner parses workflow commands there, so stdout ::warning lines will surface on the run summary.
  • Minor, not worth blocking: $lock comes from TMPDIR and is interpolated unescaped into the ::warning message. A %, CR or LF in TMPDIR would garble the annotation. This is unrealistic on CI runners.

Positives

  • The tests are well designed. printf is overridden so the failed-write test does not depend on file permissions, which root ignores.
  • The annotation is gated on GITHUB_ACTIONS, and the stderr line is kept for runs read outside Actions.
  • The failure path cleans up thoroughly and states the cause in its message.

# 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant