Repository navigation
fix(relay): read reconcile's counter units after the cell lock - #25638
Conversation
reconcileReservationAccounting computed each cell's absolute reserved_requests from leases read before lockCellRows, so a host placed in that gap lost its unit: the counter went to 0 with one lease on the cell.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Read cell lease units after the cell lock:
reconcileReservationAccountingnow derivesrelay_cells.reserved_requestsfrom a freshSELECT cell_id, SUM(request_units) … GROUP BY cell_id WHERE cell_id IN (?, ?)issued afterlockCellRows, instead of summing theleasessnapshot read before the lock. This closes the window where a placement that committed a new lease while reconciliation waited on the cell rows had its counter bump overwritten away. - Regression test: a new
relay-postgrestest pauses reconciliation right after its lease read, places a brand-new host on one of the two cells, resumes, and assertsreserved_requests == lease_units == 1with an exact equality — it fails onmainand passes with the fix.
The fix is sound: every path that mutates reserved_requests holds the target cell row lock in the same transaction (the adjustCellReservation* helpers and reserveRegionalRehomeTargetRow; the only raw updates in non-test source are the locked rehome statement and this reconcile write), and Postgres transactions run at READ COMMITTED (plain BEGIN), so the post-lock read sees a lease committed during the wait. Lock order and the per-host assignment counts are unchanged.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (2)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review. 📝 WalkthroughWalkthrough
Priority: ⬇️ Low Merge Risk: ⚪ Minimal · up to The reconciliation change has no confirmed merge-blocking issue; merge after normal checks. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The change addresses a reservation-undercount race without expanding access or authority. Existing capacity checks and transactional rollback remain intact. Residual uncertainty concerns whether deployed database isolation settings match the assumptions behind the fix. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
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. Comment |

ELI5
Every relay cell row keeps a counter of how many requests it has promised. A repair job rewrites that counter from the list of live promises. It read the list, then waited for the cell row, then wrote the old list's total. A host placed while it waited lost its promise from the counter.
What Changed
reconcileReservationAccountingnow reads each cell's lease units with a plainSELECT … GROUP BY cell_idissued afterlockCellRows, and writes that total. The per-host counts and the lock order (assignments → leases → cell rows) are unchanged.postgres-transaction-recovery.test.ts: pauses reconciliation right after its lease read, places a brand-new host on one of the two cells, then lets reconciliation finish.Before: the cell ends with
reserved_requests = 0and one lease worth 1 unit (the test fails onmainwith exactly this). After:reserved_requests = 1, matching the lease.Why
The counter is written as an absolute value, so its inputs have to be read once the row is held. Any lease committed before the lock already bumped the counter and is now counted. Any lease still uncommitted belongs to a transaction that needs this row to bump the counter, so it cannot commit before reconciliation does.
Reconciliation runs only on migration-backed paths: evacuation completion and status, drain-send accounting repair, and supersede. Same-cap rolls use drain-return and create no migrations. In production it has run 0 times since 2026-09-28 (no
pg_stat_statementsentry for its statements, a window that includes the 10-02 US same-cap wave). So the bug bites only during evacuations, but when it does, a low counter lets a cell be promised pastcapacity_requestsuntil the next repair.Alternatives considered:
FOR UPDATEafter the cell lock would invert the lock order every live writer uses, which risks deadlocks.FOR UPDATEadds locks with no benefit; the cell row already fences the writers that matter.Found by the step-1 correctness review (F1), which reproduced it on local Postgres with the production lock shapes.
Linked Issue
N/A (internal relay fix)
Visual Proof
N/A: server-side accounting fix with no UI.
Testing
pnpm typecheck(cloud/apps/relay)New test fails on
main(reserved_requests0 vslease_units1) and passes with the fixFull relay suite including the
relay-postgresproject on a fresh local Postgres 16: 32 Postgres files / 173 tests pass; whole suite 113 files passI manually tested these changes locally
Automated tests added/updated, or explained why not below
Review
Ships as a normal director deploy (pause rehome → publish → deploy → monitor → re-enable). Cells are unaffected until their next image; the bug only lives in director-run reconciliation paths.
Agent skill upstream boundary
docs/reference/agent-skill-sharing-upstream-boundary.mdand copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.Notes
No wire, schema or lock-order change.
Cost, measured read-only in production by the reviewer:
relay_assignment_activity_leases, which has nocell_idindex (deliberately, seedatabase.ts).EXPLAIN ANALYZEof the exact statement: 14–16 ms. The same scan shape ran 7 days at a mean of 17.7 ms, with a max of 3.1 s under load.Alternatives the reviewer tested:
cell_idindex: 6.5 → 0.5 ms locally, but it costs out-of-band DDL on the shared instance and ~104/s more index writes. Not worth it for a path that ran 0×/week.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)