Repository navigation
fix(lease): fix flaky primary/follower leasing test - #2
Merged
Merged
Conversation
…create The leasing integration test was flaky because two bugs combined: - `Replica.leaser = …` did not reset `leasePrimary`, unlike the constructor option, so `isPrimary()` stayed true before any lease was acquired. The test's wait for A to become primary returned immediately, letting B race A for the lock; when B won, the test timed out. - `FileLeaser.acquireLease` created the lock with `open(path, 'wx')` and wrote it afterwards, so a racing acquirer hitting EEXIST could read an empty file and fail with a SyntaxError instead of LeaseExistsError. The lock is now written to a temp file and hard-linked into place. Temp file names also get a per-process counter to avoid same-millisecond collisions between leasers in one process. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KdmZiQnN1N4wmoaX7dpbJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the intermittent failure of
Replica.run() with a leaser (primary/follower) › one replica is primary and the other follows; takeover on release(timed out at ~2081ms).Root cause
Two bugs combined:
Replica.leasersetter didn't demote to follower. The constructor option setsleasePrimary = false, but the setter (which the test uses) left it at the defaulttrue.waitFor(() => replicaA.isPrimary())therefore returned before A acquired anything, so B'srun()raced A for the lock. When B won,waitFor(() => !replicaB.isPrimary(), 2000)timed out.FileLeaser.acquireLeasewasn't atomic.open(path, 'wx')created an emptylock.jsonand wrote it afterwards. A racing acquirer hittingEEXISTcould read the empty file and throw aSyntaxErrorinstead ofLeaseExistsError.Changes
src/replica.ts: the setter now mirrors the constructor (leasePrimary = leaser === null), except whilerun()is active.src/backends/file/leaser.ts: write the lock to a temp file andlink()it into place, so it appears atomically with full contents. Temp names get a per-process counter to avoid same-millisecond collisions.test/integration/leasing.test.ts: added a setter regression test (fails deterministically without the fix) and a concurrent-acquire test (4 leasers × 50 rounds, exactly one winner, the rest getLeaseExistsError).Verification
pnpm test:all(119 + 18),pnpm typecheck,pnpm lintall pass.🤖 Generated with Claude Code
https://claude.ai/code/session_01KdmZiQnN1N4wmoaX7dpbJS