Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(persistence): release a lost claim only after its cancelled renewal settles (#5373)
time-attack9ce6b8acb11e
diff --git a/src/core/persistence/claim-lease.ts b/src/core/persistence/claim-lease.tsindex 329cdbc805..011179fe8d 100644--- a/src/core/persistence/claim-lease.ts+++ b/src/core/persistence/claim-lease.ts@@ -82,3 +82,17 @@ export function startClaimLease(renew: (signal: AbortSignal) => Promise<boolean>},};}++/**+ * After a lost claim: stop renewing and give the cancelled in-flight renewal up+ * to `boundMs` to settle, so its server-side statement is gone before the+ * caller queues the release behind the same row lock. Never rejects.+ */+export async function endLostLease(lease: ClaimLease, boundMs = 1_000): Promise<void> {+ const renewal = lease.end();+ if (!renewal) return;+ let timer: ReturnType<typeof setTimeout> | undefined;+ await Promise.race([renewal, new Promise<void>(resolve => { timer = setTimeout(resolve, boundMs); })]);+ if (timer) clearTimeout(timer);+}+diff --git a/src/core/persistence/consumer.ts b/src/core/persistence/consumer.tsindex 2f36885b64..193b88df4e 100644--- a/src/core/persistence/consumer.ts+++ b/src/core/persistence/consumer.ts@@ -4,7 +4,7 @@ import { CLAIMABLE_WRITE_SQL, claimGroupFollowers, claimNextLaneHead, claimNextWimport { finishUnpublishedFailure, publishMutation, recoverPublication, type PreparedMutation } from './coordinator.ts';import { localHostId } from './identity.ts';import { executeClaimedGroup, PAGE_BATCH_GROUP_MAX } from './group-publish.ts';-import { CLAIM_LOST, DEFAULT_CLAIM_LEASE_TIMING, startClaimLease, type ClaimLeaseTiming } from './claim-lease.ts';+import { CLAIM_LOST, DEFAULT_CLAIM_LEASE_TIMING, endLostLease, startClaimLease, type ClaimLeaseTiming } from './claim-lease.ts';import { isTerminal, type WriteRequest } from './model.ts';import { refreshManagedFilesystemRoots } from './filesystem-guard.ts';import { PROJECTION_RETRY_READY_SQL, rebuildPendingPageProjections } from '../page-state/projections.ts';@@ -541,6 +541,7 @@ export class PersistenceConsumer {const prepared = await lease.whileHeld(preparation);if (prepared === CLAIM_LOST) {root.until = this.keepUntilSettled(preparation);+ await endLostLease(lease);await releaseUnpublishedClaim(this.engine, row, 'claim_lost');return false;}diff --git a/src/core/persistence/group-publish.ts b/src/core/persistence/group-publish.tsindex 2dc8f6bdd3..c11338f559 100644--- a/src/core/persistence/group-publish.ts+++ b/src/core/persistence/group-publish.ts@@ -36,7 +36,7 @@ import { cancelRows, windowPredecessor } from './sync-window.ts';import { clearResolvedRecoveries, completeWrite, ENSURE_COUNTERS_SQL, getWriteRequestById, LOCK_COUNTERS_SQL, markRecovering, prepareRecoveries,publicationGroupKey, reclaimReleasedWrite, releaseUnpublishedClaim, renewGroupClaims } from './journal.ts';import { principalKey, requestPrincipal, type FileRecoveryRecord, type WriteRequest } from './model.ts';-import { CLAIM_LOST, DEFAULT_CLAIM_LEASE_TIMING, startClaimLease, type ClaimLeaseTiming } from './claim-lease.ts';+import { CLAIM_LOST, DEFAULT_CLAIM_LEASE_TIMING, endLostLease, startClaimLease, type ClaimLeaseTiming } from './claim-lease.ts';import { setMemberAttribution, withCoordinatedWrite } from './context.ts';import { requestAttribution } from './attribution.ts';import { tryAcquirePublicationCapacity } from './pool-capacity.ts';@@ -352,6 +352,7 @@ export async function executeClaimedGroup(engine: BrainEngine, rows: WriteReques})();if (await lease.whileHeld(preparing) === CLAIM_LOST) {run.leftRunning?.(preparing, true);+ await endLostLease(lease);for (const row of rows) await releaseUnpublishedClaim(engine, row, 'claim_lost');return false;}diff --git a/test/persistence-claim-lease.test.ts b/test/persistence-claim-lease.test.tsindex 0b0c49b7d2..4048fe13dd 100644--- a/test/persistence-claim-lease.test.ts+++ b/test/persistence-claim-lease.test.ts@@ -13,7 +13,7 @@* in milliseconds, with no datastore.*/import { describe, expect, test } from 'bun:test';-import { CLAIM_LOST, startClaimLease } from '../src/core/persistence/claim-lease.ts';+import { CLAIM_LOST, endLostLease, startClaimLease } from '../src/core/persistence/claim-lease.ts';const FAST = { everyMs: 5, deadlineMs: 40 };const sleep = (ms: number) => new Promise<void>(resolve => setTimeout(resolve, ms));@@ -118,4 +118,27 @@ describe('startClaimLease', () => {expect(renewals).toBe(0);expect(lease.held).toBe(true);});++ test('endLostLease waits for the cancelled renewal to settle before the caller releases', async () => {+ const cancelled = Promise.withResolvers<void>();+ let renewing = false;+ const lease = startClaimLease(signal => {+ renewing = true;+ return new Promise<boolean>((_, reject) => signal.addEventListener('abort', () => setTimeout(() => { cancelled.resolve(); reject(new Error('canceled')); }, 40)));+ }, { everyMs: 5, deadlineMs: 15 });+ await lease.lost;+ expect(renewing).toBe(true);+ let cancelSettled = false;+ void cancelled.promise.then(() => { cancelSettled = true; });+ await endLostLease(lease);+ expect(cancelSettled).toBe(true);+ });++ test('endLostLease gives up on a renewal that never settles after its bound', async () => {+ const lease = startClaimLease(() => new Promise<boolean>(() => {}), { everyMs: 5, deadlineMs: 10 });+ await lease.lost;+ const started = performance.now();+ await endLostLease(lease, 50);+ expect(performance.now() - started).toBeLessThan(1_000);+ });});