Skip to content

fix(forge): stop a default-branch change from landing on a branch a push is deleting - #331

Merged
tonibergholm-codento merged 4 commits into
mainfrom
fix/forge-default-branch-race
Nov 9, 2026
Merged

tonibergholm-codento merged 4 commits into
mainfrom
fix/forge-default-branch-race

Conversation

@tonibergholm-codento

@tonibergholm-codento tonibergholm-codento commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

A PATCH default_branch sent while a push was deleting the new default could leave the repository's default naming a branch that no longer exists. The push's pre-receive hook had read the old default when it started, so it let the delete through. This was the first item on #316.

The PATCH now takes a lease from the transport registry:

  • Before it takes the repository lock, it waits up to 10 s until no writer (a push or a maintenance pass) is registered on the repository. If one is still running after that, it answers 503 this repository is being written to, try again.
  • It commits the row only if no writer has registered since it checked the branch. If one has, it puts HEAD back and answers 503. A push that registers after the commit reads the new default in its hook.
  • Registry slots are no longer removed when they empty, so the writer count survives. forget still drops a slot along with its repository.

e2e_default_race.rs is the regression test. It holds a real git push :dev in its hook after the hook has read the row, using SC_FORGE_HOOK_HOLD, a test-only knob read from the server's own environment, like SC_FORGE_MERGE_PAUSE_MS. While the push is held, it sends the PATCH.

Proof: without the lifecycle.rs change, the PATCH answers 200 and the default then names the deleted dev; with it, the PATCH answers 503 and the default stays main. fmt, clippy (-D warnings) and cargo test -p scl-forge pass locally.

After an Opus review: a PATCH that resends the current default returns at once and doesn't wait for pushes (clients that send the whole object do this), HEAD is also put back when the row write fails, a unit test covers the restore, and the hold knob and its test exist in debug builds only.

Trade-off: a real default-branch change answers 503 while a push or maintenance pass runs on that repository for longer than 10 s. Gitea doesn't do this.

Not covered: branch-protection rule changes have the same kind of race with a push that is already running. That one stays documented and out of scope.

Reviewed with sol-loop: 2 rounds with gpt-6.1-sol on high. Sol verdict: approved

Created with Claude Opus 5.5 in T3 Code (Claude Code harness).

…ing (#316)

The pre-receive hook reads the default branch when a push starts, so a
PATCH default_branch that ran while a push deleting the new default was in
flight left HEAD and the row naming a missing branch.

The PATCH now takes a lease from the transport registry: it waits up to 10 s
until no writer (push, maintenance) is registered, and commits the row only if
no writer registered since, otherwise it restores HEAD and answers 503.

e2e_default_race holds a real push in its hook (test-only SC_FORGE_HOOK_HOLD)
between its read of the row and its ref update; without the fix the PATCH
answers 200 and the default ends up naming the deleted branch.
@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f4c7974b-4162-42cb-82c5-39fa54f4364d
  • 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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T10:51:54.609646Z 10b07f0 New commits
🔒 Security Review ✅ Completed 2026-11-09T10:55:32.481379Z 10b07f0 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

… on any failed commit

From review: a PATCH that resends the current default_branch no longer
waits for (or 503s behind) pushes; HEAD is put back on a failed row write as
well as on a lost lease, with a unit test for the restore; the hook hold knob
is compiled into debug builds only; the lock-order note names the registry
mutex as innermost.
@tonibergholm-codento
tonibergholm-codento merged commit 6d51382 into main Nov 9, 2026
11 checks passed
@tonibergholm-codento
tonibergholm-codento deleted the fix/forge-default-branch-race branch November 9, 2026 11:02
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.

2 participants