Skip to content

fix: take a PR's project locks all or nothing - #2768

Open
dannysauer wants to merge 1 commit into
diggerhq:developfrom
neverendingsupport:fix/dannys/atomic-pr-locks-upstream
Open

dannysauer wants to merge 1 commit into
diggerhq:developfrom
neverendingsupport:fix/dannys/atomic-pr-locks-upstream

Conversation

@dannysauer

Copy link
Copy Markdown
Contributor

Summary

Closes #2759. Two PRs that both change projects a and b can no longer lock each other out. A plan, apply or digger lock now checks every project before taking any lock. If another open PR holds one, it takes none and reports the holder in the same wording as before. Otherwise it takes them all.

How it works

  • Atomic: the checks and inserts run in one database transaction that also holds a Postgres advisory lock for the repository. Two PRs can't interleave, and a failure, a panic, a lost lock or a crash commits nothing. That also closes the race where two PRs planning at once could both get the same project.
  • Idle timeout: Postgres disconnects a holder that sits idle for more than a minute, so a request stuck mid-way can't hold the repository.
  • Unlocks take the same lock, so a merged PR's unlock can't delete a lock another PR just took.
  • One helper: the five copies of the per-project lock loop are now one helper, used by the GitHub comment and PR handlers, GitLab, Bitbucket, and the EE drift hook.

Behavior changes

  • Retry message: a request that can't get the repository lock within a minute, or hits a database error, gets a "try again" comment. It used to get a lock-failure comment that read like another PR held the project.
  • EE drift hook: it now respects pr_locks: false, as the other lock paths already did.

Known gap

Checking whether a holder's PR is closed is a VCS call made while the repository is locked. If it hangs, Postgres frees the lock after a minute, but the stuck request isn't cleaned up.

Testing

  • Unit tests cover: taking nothing when another PR holds a project; keeping the PR's own locks; clearing a closed PR's lock; unlock; and each failure message.
  • Postgres tests skip unless DIGGER_TEST_POSTGRES_URL is set, because CI has no Postgres service. I ran them against Postgres 16:
    • Two PRs race for a and b in opposite order, 20 times. Each time, exactly one gets both. With the advisory lock turned off, the test fails on the first race.
    • A failed request, a stalled one and a panicking one each leave no locks behind.
    • Happy to add a Postgres service to backend_test.yml if you'd like these in CI.
  • Docs:
    • The PR-level locks page explains how locks are taken and released.
    • CommentOps says plan and apply lock all or none.
    • There's a link from the digger.yml reference, and two typo fixes.

#2760 proposes lock_scope: repository, which would lock every project through this same path. We run this on our self-hosted backend, and are happy to adjust it based on your feedback.

🤖 Generated with Claude Code

The backend locked a PR's projects one at a time and stopped at the first
project another PR held, keeping the locks it had already taken. Two PRs
that both changed projects a and b could each hold one and fail on the
other, until someone ran `digger unlock`. Each lock was also a separate
check and insert, so two PRs planning at once could both take the same
project.

Plan, apply and lock now check every project before taking any. If
another open PR holds one, the PR takes none and the comment names the
first locked project and its holder, in the wording it had before. The
checks and inserts run in one database transaction that also holds a
per-repository Postgres advisory lock, so two PRs can't interleave, and a
failure, panic, lost lock or crash commits nothing. Unlocks take the same
lock, so a merged PR's unlock can't delete a lock another PR just took.
Postgres disconnects a holder idle for more than a minute. A PR that
can't get the lock in a minute, or hits a database error, is told to try
again, in wording that isn't mistaken for a lock held by another PR.

Every backend path that takes PR locks goes through one helper: GitHub
comments and PR events, GitLab, Bitbucket, and the EE drift hook, which
now also respects pr_locks.

Closes diggerhq#2759.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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.

PR locks are taken one project at a time, so two PRs can lock each other out

1 participant