Repository navigation
Leases: issue no member grants while the usage ledger is frozen or reconciling - #541
Merged
Merged
Conversation
Agent: codex/gpt-6.1-terra
Agent: codex/gpt-6.1-terra
Agent: codex/gpt-6.1-terra
Agent: codex/gpt-6.1-terra
Agent: codex/gpt-6.1-terra
teddashh
added a commit
that referenced
this pull request
Nov 14, 2026
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.
After a database restore, the operator freezes the usage ledger and begins recovery before control starts. Until recovery completes, member authorization that has not been confirmed must stay closed. Before this change,
RenewTxissued serving leases with member grants from the restored data whatever the ledger's mode, and a renewal replay returned a stored envelope with its grants.What changes.
internal/leases/leases.go,internal/leases/replay.go).RenewTxreadsusage_ledger_state.modewithFOR SHAREafter thefleet_configread and before the node row, and holds that lock until commit. Freeze and recovery lock the ledger before nodes, so both keep one order. A missing row, a failed read or an unknown mode fails closed withDependencyUnavailable(usage ledger state unavailable).frozenorreconciling, a renewal carries no member grants and does not read entitlements. A published, registered node then gets the provisional grant-less expiry (ProvisionalGrantlessLeaseSeconds, 60 s), even when it is ready, bounded by the configured maximum. Unpublished nodes keep today's expiry, and no other lease field changes. Once the mode isnormalagain, the next renewal is exactly today's lease.renewal_request_id) carries member grants is refused withDependencyUnavailable(usage ledger recovery in progress), and nothing is written. A stored envelope without member grants is returned as stored.ReadRenewal, the read-only replay path, now runs in its own transaction with the same share lock and applies the same rule.internal/nodes/httpwire/state.go).RenewServingLeaseruns in a transaction that locksfleet_config, then locks its own node row while it records the agent's endpoint, and only then callsRenewTx. It now takes the ledger share lock (leases.LockUsageLedgerMode) before that node lock, so the order is fleet, ledger, node. Without it, an HTTP renewal and a freeze fromnormalcould each wait for the other.Tests (integration, against PostgreSQL):
TestAT29_RecoveryLeaseFence: a normal lease carries the member grant; infrozenand inreconcilingthe same node's lease carries none, lastsmin(max offline, 60 s), keeps its node, incarnation, schema version and generations, and the entitlement reader is not called; an unpublished node keeps the full expiry; afterCompleteRecoverythe grant and the full expiry return.TestAT29_RecoveryLeaseRespectsSmallerOfflineCap: with a 30 s maximum, the recovery lease lasts 30 s.TestAT29_RecoveryStoredLeaseReplay: in both modes, replaying a stored envelope with grants is refused throughRenewTxandReadRenewal; a grant-less stored envelope replays unchanged, and the lease, member and audit row counts do not change.TestAT29_RecoveryLedgerReadFailureFailsClosed: without the ledger row, the error isDependencyUnavailableafter the transaction helper's mapping, and no lease is written.TestAT29_RecoveryFreezeRenewalLockOrderandTestAT29_HTTPRecoveryLeaseFreezeLockOrder: a freeze and a renewal, throughRenewTxand through the real HTTP handler, in both orders. Lock waits are read frompg_stat_activityandpg_blocking_pids, and the HTTP test also checks the node lock withFOR UPDATE NOWAIT. When the freeze goes first, the renewal waits on the ledger and holds no node lock; when the renewal goes first, the freeze waits for it. Both orders finish without error.TestAT29_HTTPRecoveryLeaseReplays: over HTTP, after a freeze, a renewal whose stored envelope has grants answers 503DEPENDENCY_UNAVAILABLEwith the same or a new idempotency key; a grant-less renewal replays byte for byte.Deliberate breakages, each reverted afterwards:
TestAT29_RecoveryLeaseFencefailed;TestAT29_RecoveryFreezeRenewalLockOrder/freeze_firstfailed;TestAT29_RecoveryStoredLeaseReplayfailed;TestAT29_HTTPRecoveryLeaseFreezeLockOrderfailed, one withSQLSTATE 55P03from theNOWAITcheck and one because the freeze did not wait;TestAT29_RecoveryLedgerReadFailureFailsClosedfailed.Validation: on the exact source
e655124f, whose basea554f2c5is still the head of main:go vetofinternal/leasesandinternal/nodes/httpwire, plain and with the integration tag, for linux/amd64 and linux/arm64;TestAT29_RecoveryLeaseFence,TestAT29_RecoveryLeaseRespectsSmallerOfflineCap,TestAT29_RecoveryStoredLeaseReplay,TestAT29_RecoveryLedgerReadFailureFailsClosed,TestAT29_RecoveryFreezeRenewalLockOrder,TestAT29_HTTPRecoveryLeaseFreezeLockOrderandTestAT29_HTTPRecoveryLeaseReplays;internal/nodes/httpwire;apps/worker, which depends oninternal/leases;go-lint,go test ./...(95 packages ok, none cached), the build of every command for amd64 and arm64, and the tidy check;TestD096_RealNetBird), build;tests/e2e/at/membersdepends on the changed packages) and e2e's pull-request lint job (itse2e-configstep not run);fvpn-controlandfvpn-worker(12 tests passed, none failed, flaky or skipped);These are local runs; no CI result is claimed here.
Review: While writing the first commit, its author found that the HTTP renewal handler locks its node before
RenewTx, which could deadlock with a freeze; the second commit takes the ledger lock first. An independent review of those two commits found one major problem: a missing ledger row returned an error that keptpgx.ErrNoRowsas its cause, the transaction helper mapped that todb.ErrNotFound, and the HTTP response was 500 instead of 503. The last three commits return an unwrappedDependencyUnavailablefor that case and test the mapped error. The review also noted thatTestAT29_RecoveryFreezeRenewalLockOrder/renewal_firstcannot show which lockRenewTxtook first; that was accepted, because thefreeze_firstsubtests catch the wrong order. A second review, of the last three commits, approved them with no findings.Agent: codex/gpt-6.1-terra
🤖 Generated with Claude Code