Skip to content

fix(oracle): repeat the lease predicates at the ExpireLeases lock level - #42

Merged
pdevito3 merged 5 commits into
mainfrom
fix/oracle-expire-outer-guard
Nov 26, 2026
Merged

pdevito3 merged 5 commits into
mainfrom
fix/oracle-expire-outer-guard

Conversation

@pdevito3

Copy link
Copy Markdown
Contributor

Why

The Oracle ExpireLeases sweep picks lapsed leases in a ROWNUM subquery and locks them in an outer SELECT ... FOR UPDATE SKIP LOCKED. Oracle skips a row that changed after the statement snapshot only when the change touches a column that the locking level reads. Today only the outer ORDER BY lease_expiry keeps the sweep off a row that an outcome or heartbeat wrote after the snapshot. Without it, the sweep writes a Succeeded job back to Scheduled and the job runs again. Nothing documented or tested that the ORDER BY carries this load.

Today's code is safe on Oracle 21 and 23 because of the ORDER BY (probe case noorder, measured on both). This PR is hardening, not a fix to a live bug. It was stacked on #39, which has merged, so it now targets main.

What changed

  • OracleJobStore.ExpireLeasesUntracedAsync repeats state = {(int)JobState.Leased} AND lease_expiry <= :now in the outer WHERE, the same way the Oracle claim repeats its predicates. The comment above the sweep says why.
  • LeaseFencing.tla: the sweep's LockLevelCols now holds attempt, state, and lease_expiry. A new ExpireNoOuterWhere mutant gates the outer WHERE.
  • Mutants: expire-no-outer-order-oracle now passes as a redundant guard. The new expire-no-outer-where-oracle (the old code) passes too. The new expire-no-outer-where-and-order-oracle drops both and breaks ExpireOnce.
  • The model skill's coverage record, the spec citations, and the probe comments follow the 1-line shift after OracleJobStore.cs:1471.

Scope

Product code: one predicate and two comment lines in the Oracle sweep. Postgres and SQL Server are unchanged.

Tradeoffs

  • The sync commit moves to 7716d9e, the code commit of this branch. A squash merge rewrites it, so the landing step must move the sync commit to the main SHA that carries the outer WHERE. Keeping ad5b0ab would be wrong, because the model no longer describes it.
  • No new probe case for the shipped shape. The SKIP LOCKED rule already predicts it, and case outerwhere measures that a column read only in the outer WHERE makes SKIP LOCKED skip the row.

Blast Radius

Every Oracle ExpireLeases call. The outer predicates only drop rows that the subquery picked from a stale snapshot. Statement count is unchanged, and the round-trip budget tests pass. The query plan was not inspected.

Verification

  • tests/formal/lease-fencing/check.sh: every config as expected (table in a comment below).
  • Oracle adapter suite on gvenzl/oracle-free:23-slim, private container: 355 passed, 0 failed, OracleConformanceTests and OracleRoundTripBudgetTests included.
  • dotnet build: 0 warnings, 0 errors.

@pdevito3

Copy link
Copy Markdown
Contributor Author

review-gate: passed a921d31

Reviewer: independent thermo-nuclear-review pass on opus over formal/lease-fencing...a921d31. That head is the pre-rebase twin of 5b2b712. After #39 merged, the branch was rebased onto main, and the sync commit SHA text moved from 58a4963 to 7716d9e. The tested content did not change. No correctness bug found.

Open findings, with the reason each stays:

  • Medium, tooling only: the model's sync commit (7716d9e) is a branch SHA, and a squash merge rewrites it, so the drift command in .agents/skills/lease-fencing-model/SKILL.md will name an unknown revision. No in-branch value survives a squash, and ad5b0ab no longer matches the model. After merge, move the sync commit to the main SHA that carries the outer WHERE.
  • Low: the outer predicate's effect on the Oracle query plan was not inspected. The statement count is unchanged, and the round-trip budget tests pass.
  • Low, fixed in 87b46f2: the probe comment called the sweep shape "the ExpireLeases shape" though most cases run without the outer WHERE.

@pdevito3

Copy link
Copy Markdown
Contributor Author

tests/formal/lease-fencing/check.sh on the model at this PR. The adapter configs come from a --all run. That run aborted mid-mutants on a full disk (TLC No space left on device), so the mutants come from a clean --mutants rerun on the same spec. Times are high because the machine load averaged 340-500.

config                                                                   expect               got                    distinct  secs
oracle.cfg                                                               pass                 pass                   14110656  1059
postgres.cfg                                                             pass                 pass                    1947808   115
sqlserver.cfg                                                            pass                 pass                    2698212   122
mutants/claim-no-due-recheck-oracle.cfg                                  pass                 pass                   14110656  1176
mutants/claim-no-lock-oracle.cfg                                         SingleLease          SingleLease                9697     2
mutants/claim-no-lock-postgres.cfg                                       SingleLease          SingleLease                6310     2
mutants/claim-no-lock-sqlserver.cfg                                      SingleLease          SingleLease                4356     2
mutants/claim-no-outer-due-oracle.cfg                                    pass                 pass                   14110656   705
mutants/expire-no-outer-order-oracle.cfg                                 pass                 pass                   14110656  1301
mutants/expire-no-outer-where-and-order-oracle.cfg                       ExpireOnce           ExpireOnce                46418     6
mutants/expire-no-outer-where-oracle.cfg                                 pass                 pass                   14110656   494
mutants/outcome-keeps-owner-postgres.cfg                                 pass                 pass                    2044516    81
mutants/owner-only-fence-oracle.cfg                                      OutcomeFenced        OutcomeFenced            137342     6
mutants/owner-only-fence-postgres.cfg                                    OutcomeFenced        OutcomeFenced             63631     5
mutants/owner-only-fence-sqlserver.cfg                                   OutcomeFenced        OutcomeFenced             83987     4
mutants/relinquish-no-leased-check-and-outcome-keeps-owner-postgres.cfg  RelinquishOnlyHeld   RelinquishOnlyHeld         8996     1
mutants/relinquish-no-leased-check-postgres.cfg                          pass                 pass                    1947808    45

@pdevito3

Copy link
Copy Markdown
Contributor Author

Closes the open medium from the gate: the skill and the spec header no longer pin the branch-only SHA 7716d9e. The sync commit is now the last commit that changed LeaseFencing.tla, found with git log -1 --format=%h -- tests/formal/lease-fencing/LeaseFencing.tla, so the drift command keeps working after a squash merge. Comment-only change to the spec; SANY parses it.

The sweep picks lapsed leases in a ROWNUM subquery and locks them in an
outer SELECT ... FOR UPDATE SKIP LOCKED. Oracle skips a row that changed
after the statement snapshot only when the change touches a column that
the locking level reads. Until now only the outer ORDER BY lease_expiry
kept the sweep off a row that an outcome or heartbeat wrote after the
snapshot. Repeat state = Leased and lease_expiry <= :now in the outer
WHERE, as the claim does, so the guard no longer rests on the ORDER BY.
The Oracle sweep now repeats state = 2 AND lease_expiry <= :now in the
outer WHERE of its SKIP LOCKED select, so LockLevelCols for the sweep
holds attempt, state, and lease_expiry. Add the ExpireNoOuterWhere
mutant. Removing the outer WHERE or the outer ORDER BY alone now passes,
because each reads lease_expiry. Removing both breaks ExpireOnce. Move
the sync commit to 7716d9e and update the shifted Oracle citations.
@pdevito3
pdevito3 force-pushed the fix/oracle-expire-outer-guard branch from 3579cb3 to 8d8ef3d Compare November 26, 2026 10:54
@pdevito3

Copy link
Copy Markdown
Contributor Author

verify: PASS+NOTES 8d8ef3d

Base: main f958b77 (merge base). Patch-id: 95df42ea8b3d7084d09e3649969862a6552e98d9 (matches).

  1. Diff: the only SQL change is the outer WHERE state = {(int)JobState.Leased} AND lease_expiry <= :now AND job_id IN (...) in ExpireLeasesUntracedAsync (OracleJobStore.cs:1476). JobState.Leased is 2. The command uses BindByName = true (line 61), so both :now references bind the one Tstz("now", now) parameter that the subquery uses. The style matches the claim's outer predicate at line 583.
  2. dotnet build -warnaserror tests/BackWave.Oracle.Tests (builds src/BackWave.Oracle): 0 warnings, 0 errors.
  3. Private gvenzl/oracle-free:23-slim on 127.0.0.1:15342: BackWave.Oracle.Tests 355 passed, 0 failed, 0 skipped. That includes OracleConformanceTests 233/233 and OracleRoundTripBudgetTests 10/10. Container removed with docker rm -f, then confirmed gone. Compose backwave-oracle-1 not touched.
  4. TLC via tests/formal/tlc.sh, one at a time:
    • oracle.cfg: pass, 14,110,656 distinct states
    • expire-no-outer-where-oracle: pass (expect pass), 14,110,656 distinct
    • expire-no-outer-order-oracle: pass (expect pass), 14,110,656 distinct
    • expire-no-outer-where-and-order-oracle: ExpireOnce (expect ExpireOnce), 41,454 distinct
      All four match the skill's table. Other configs were skipped because they do not touch the Oracle sweep.
  5. Scratch break: I removed the outer WHERE and the outer ORDER BY from the C# SQL (it still builds clean) and set ExpireLockCols == {"attempt"} in the spec. The real oracle.cfg then fails on ExpireOnce in 12 states: ClaimCommit, ReportUpdate, ExpireBegin, ReportUpdate, ExpireLock, ExpireCommit. In that trace the sweep locks and resets a row that an outcome committed after the sweep's snapshot. Reverted, and git status is clean at 8d8ef3d.
  6. Every OracleJobStore.cs citation in the skill table and the spec points at the code it names, including JobColumns at 4181-4184 and the sweep columns at 1475/1476/1483. The drift command runs, exits 0, and prints nothing (sync commit = HEAD).
  7. The added lines contain no em dash and no ADR or issue number.

Notes (not blocking):

  • The new comment in oracle-skip-locked-probe.sh cites OracleJobStore.cs:1475-1486. The statement is 1475-1484, so the range runs 2 lines too far, into the closing """ and connection, transaction)). The skill cites 1473-1484. Either range is defensible, but the two should agree.
  • I did not rerun the SKIP LOCKED probe. The outerwhere evidence in the skill is the author's measurement.

@pdevito3
pdevito3 merged commit 280ecbc into main Nov 26, 2026
2 checks passed
@pdevito3
pdevito3 deleted the fix/oracle-expire-outer-guard branch November 26, 2026 11:18
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