Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): a drain keeps retrying admission that gave up on counter contention; document group lock order (#5984)

garrytan08ddd72b717f
diff --git a/src/core/persistence/admission-retry.ts b/src/core/persistence/admission-retry.ts
index 7bf526f4d0..199bfd6595 100644
--- a/src/core/persistence/admission-retry.ts
+++ b/src/core/persistence/admission-retry.ts
@@ -18,6 +18,7 @@ export async function retryWriteAdmission<T>(requestId: string, attempt: (remain
const unavailable = new OperationError('storage_error', 'Write admission is temporarily blocked by database contention.',
`Retry the same operation, arguments, and request_id ${requestId}. No queued receipt has been confirmed.`);
unavailable.writeError = 'storage_error';
+ unavailable.detail = 'database_contention';
throw unavailable;
}
// The transaction has rolled back and released its connection before any
diff --git a/src/core/persistence/group-publish.ts b/src/core/persistence/group-publish.ts
index 51ddd21600..29e32a57d1 100644
--- a/src/core/persistence/group-publish.ts
+++ b/src/core/persistence/group-publish.ts
@@ -7,7 +7,11 @@
* work is shared: the worktree lock, the recovery and capacity checks, the
* ownership guard and the counter and request locks. Counters are locked after
* the members are applied and before their request rows, so the brain-wide
- * counter row is held only for the completion statements (ENG-A5).
+ * counter row is held only for the completion statements (ENG-A5). A group
+ * therefore takes page guards before counters, the reverse of single
+ * publication and of forget/mirror recovery; a deadlock with one of those on
+ * the same page is detected by Postgres (40P01) and both sides retry: the
+ * group falls back to single publication, admission and withdrawal retry.
*
* The group is all-or-nothing. Any failure rolls the transaction back and
* returns null; the caller then publishes the members one at a time, so a
diff --git a/src/core/persistence/sync-drain.ts b/src/core/persistence/sync-drain.ts
index 0b05a16db3..d70ec8d5bc 100644
--- a/src/core/persistence/sync-drain.ts
+++ b/src/core/persistence/sync-drain.ts
@@ -102,6 +102,8 @@ function transientDelay(error: unknown, attempt: number, refreshWaitedMs: number
const hinted = Number(/retry_after_ms=(\d+)/.exec(String(error.detail ?? ''))?.[1]);
return Number.isFinite(hinted) && hinted > 0 ? hinted : 1000;
}
+ // Admission gave up on lock contention (a concurrent publication holds the shared counters); the frozen request ID is kept.
+ if (error instanceof OperationError && error.detail === 'database_contention') return refreshWaitedMs >= REFRESH_WAIT_MS ? null : 1000;
if (attempt > TRANSIENT_ATTEMPTS) return null;
const contention = error instanceof OperationError && error.code === 'database_contention';
if (!contention && !isRetryableConnError(error) && !isStatementTimeoutError(error) && getCode(error) !== '57014') return null;
@@ -197,7 +199,7 @@ export async function runDrain(input: DrainInput): Promise<SyncResult> {
} catch (error) {
const delay = transientDelay(error, ++attempt, refreshWaitedMs, input.backoffMs ?? 250);
if (delay === null || signal?.aborted) throw error;
- if (error instanceof OperationError && error.code === 'worktree_refreshing') refreshWaitedMs += delay;
+ if (error instanceof OperationError && (error.code === 'worktree_refreshing' || error.detail === 'database_contention')) refreshWaitedMs += delay;
await sleep(delay, signal);
continue;
}
diff --git a/test/sync-drain.test.ts b/test/sync-drain.test.ts
index ec635bbdb7..810dc5d863 100644
--- a/test/sync-drain.test.ts
+++ b/test/sync-drain.test.ts
@@ -74,6 +74,16 @@ describe('runDrain', () => {
expect(fatal.calls()).toBe(1);
});
+ test('admission contention keeps retrying the frozen request instead of ending the drain', async () => {
+ const contention = new OperationError('storage_error', 'Write admission is temporarily blocked by database contention.');
+ contention.detail = 'database_contention';
+ const s = scripted([contention, contention, contention, contention, contention, done()]);
+ const started = Date.now();
+ expect((await runDrain({ pass: s.pass })).drain?.outcome).toBe('synced');
+ expect(s.calls()).toBe(6);
+ expect(Date.now() - started).toBeGreaterThanOrEqual(4500);
+ }, 15_000);
+
test('worktree_refreshing waits its retry_after_ms hint', async () => {
const refreshing = new OperationError('worktree_refreshing', 'refreshing');
refreshing.detail = 'retry_after_ms=5';