Skip to content

Leases: issue no member grants while the usage ledger is frozen or reconciling - #541

Merged
teddashh merged 5 commits into
mainfrom
w5-recovery-lease-fence
Nov 14, 2026
Merged

teddashh merged 5 commits into
mainfrom
w5-recovery-lease-fence

Conversation

@teddashh

Copy link
Copy Markdown
Contributor

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, RenewTx issued 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.

  • The mode is read once, in lock order (internal/leases/leases.go, internal/leases/replay.go). RenewTx reads usage_ledger_state.mode with FOR SHARE after the fleet_config read 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 with DependencyUnavailable (usage ledger state unavailable).
  • No member grants in recovery. While the mode is frozen or reconciling, 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 is normal again, the next renewal is exactly today's lease.
  • Stored envelopes. In those modes, a renewal whose stored envelope (same node, incarnation and renewal_request_id) carries member grants is refused with DependencyUnavailable (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.
  • The HTTP renewal handler's lock order (internal/nodes/httpwire/state.go). RenewServingLease runs in a transaction that locks fleet_config, then locks its own node row while it records the agent's endpoint, and only then calls RenewTx. 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 from normal could each wait for the other.

Tests (integration, against PostgreSQL):

  • TestAT29_RecoveryLeaseFence: a normal lease carries the member grant; in frozen and in reconciling the same node's lease carries none, lasts min(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; after CompleteRecovery the 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 through RenewTx and ReadRenewal; 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 is DependencyUnavailable after the transaction helper's mapping, and no lease is written.
  • TestAT29_RecoveryFreezeRenewalLockOrder and TestAT29_HTTPRecoveryLeaseFreezeLockOrder: a freeze and a renewal, through RenewTx and through the real HTTP handler, in both orders. Lock waits are read from pg_stat_activity and pg_blocking_pids, and the HTTP test also checks the node lock with FOR 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 503 DEPENDENCY_UNAVAILABLE with the same or a new idempotency key; a grant-less renewal replays byte for byte.

Deliberate breakages, each reverted afterwards:

  • skipping the mode in the grant decision: TestAT29_RecoveryLeaseFence failed;
  • reading the mode after the node row: TestAT29_RecoveryFreezeRenewalLockOrder/freeze_first failed;
  • allowing every stored envelope to replay: TestAT29_RecoveryStoredLeaseReplay failed;
  • taking the handler's ledger lock after the endpoint is recorded: both subtests of TestAT29_HTTPRecoveryLeaseFreezeLockOrder failed, one with SQLSTATE 55P03 from the NOWAIT check and one because the freeze did not wait;
  • wrapping the missing-row error as a cause again: both assertions of TestAT29_RecoveryLedgerReadFailureFailsClosed failed.

Validation: on the exact source e655124f, whose base a554f2c5 is still the head of main:

  • the change's shape: exactly the 6 owned paths in 5 commits, scanned, with clean whitespace;
  • go vet of internal/leases and internal/nodes/httpwire, plain and with the integration tag, for linux/amd64 and linux/arm64;
  • the seven recovery fence tests by name against PostgreSQL, each passed three times, none skipped: TestAT29_RecoveryLeaseFence, TestAT29_RecoveryLeaseRespectsSmallerOfflineCap, TestAT29_RecoveryStoredLeaseReplay, TestAT29_RecoveryLedgerReadFailureFailsClosed, TestAT29_RecoveryFreezeRenewalLockOrder, TestAT29_HTTPRecoveryLeaseFreezeLockOrder and TestAT29_HTTPRecoveryLeaseReplays;
  • CI's nodes job against PostgreSQL: lint, unit tests, integration, build (12 packages ok);
  • CI's pki job against PostgreSQL: verify (lint, unit tests, build) and integration (26 packages ok);
  • CI's control job against PostgreSQL: verify (drift, lint, unit tests, build) and integration (18 packages ok, none from the test cache);
  • none of the 539 test databases the runs named was left behind;
  • CI's billing job against PostgreSQL: verify (lint, unit tests, integration, build; 18 packages ok; the composition test passed once) and the arm64 build of every package;
  • CI's policies job against PostgreSQL: verify (lint with the integration tag, build, unit tests) and integration (4 packages ok);
  • CI's usage job against PostgreSQL: verify (lint, build, unit and integration tests; 7 packages ok);
  • the fleet update integration tests against PostgreSQL (1 packages ok); CI's path filters skip them for this change, but they depend on internal/nodes/httpwire;
  • the DNS steps that need no Docker: lint, build, unit tests and the publish integration (12 packages ok); CI's path filters skip them for this change, but the publish step tests apps/worker, which depends on internal/leases;
  • none of the 492 test databases the runs named was left behind;
  • CI's Go job: go-lint, go test ./... (95 packages ok, none cached), the build of every command for amd64 and arm64, and the tidy check;
  • CI's Node job: lint, unit tests (10 packages ok), integration against PostgreSQL (2 packages ok; skipped: TestD096_RealNetBird), build;
  • the at-members pull-request job (lint, test, build; CI's path filter skips it for this change, but tests/e2e/at/members depends on the changed packages) and e2e's pull-request lint job (its e2e-config step not run);
  • CI's Portal job: lockfile install, typecheck, lint, format check, unit tests with coverage (367 tests passed), and the live Playwright suite against fvpn-control and fvpn-worker (12 tests passed, none failed, flaky or skipped);
  • none of the 2 test databases the runs named was left behind.

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 kept pgx.ErrNoRows as its cause, the transaction helper mapped that to db.ErrNotFound, and the HTTP response was 500 instead of 503. The last three commits return an unwrapped DependencyUnavailable for that case and test the mapped error. The review also noted that TestAT29_RecoveryFreezeRenewalLockOrder/renewal_first cannot show which lock RenewTx took first; that was accepted, because the freeze_first subtests 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

@teddashh
teddashh merged commit 3ba3989 into main Nov 14, 2026
36 checks passed
@teddashh
teddashh deleted the w5-recovery-lease-fence branch November 14, 2026 23:58
teddashh added a commit that referenced this pull request Nov 14, 2026
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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