Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
chunks: deleteChunks locks the page row before its chunks
garrytan63e0b832debe
diff --git a/src/core/engine-sql/chunks.ts b/src/core/engine-sql/chunks.tsindex 746ded31b9..4a48d5cbae 100644--- a/src/core/engine-sql/chunks.ts+++ b/src/core/engine-sql/chunks.ts@@ -1037,11 +1037,14 @@ export async function deleteChunks(exec: SqlExecutor, slug: string, opts?: { souconst sourceId = opts?.sourceId ?? 'default';// Source-qualify the page-id subquery; slugs are only unique per source.// Removing chunks voids the text projection seal that certified them; a- // writer that reinstalls the projection seals it again (#8502).+ // writer that reinstalls the projection seals it again (#8502). The page+ // row is locked before any chunk row (the DELETE needs its id), the order+ // every writer takes them in, so this never deadlocks against one.await exec.run(sqlFragment`- WITH gone AS (+ WITH page AS (SELECT id FROM pages WHERE slug = ${slug} AND source_id = ${sourceId} FOR UPDATE),+ gone AS (DELETE FROM content_chunks- WHERE page_id = (SELECT id FROM pages WHERE slug = ${slug} AND source_id = ${sourceId})+ WHERE page_id = (SELECT id FROM page)RETURNING page_id)UPDATE pages SET text_projection_revision = NULLdiff --git a/test/e2e/delete-chunks-lock-order-8502.test.ts b/test/e2e/delete-chunks-lock-order-8502.test.tsnew file mode 100644index 0000000000..f1ea3a2e6d--- /dev/null+++ b/test/e2e/delete-chunks-lock-order-8502.test.ts@@ -0,0 +1,61 @@+/**+ * #8502 lock order: deleteChunks clears the page's text projection seal when+ * it deletes chunks. It must lock the owning page row before it touches a+ * chunk row, the order every writer uses (lock the page, then replace its+ * chunks). Deleting the chunks first and updating the page after deadlocked+ * (40P01) against a writer that held the page and then deleted its chunks.+ * An empty fragment upsertChunks clears the seal the same way and already+ * locked the page first; both are pinned here.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { hasDatabase, setupDB, teardownDB } from './helpers.ts';+import type { PostgresEngine } from '../../src/core/postgres-engine.ts';+import { importFromContent } from '../../src/core/import-file.ts';++const describeDb = hasDatabase() ? describe : describe.skip;+describeDb('deleteChunks lock order (Postgres)', () => {+ let engine: PostgresEngine;+ const slug = 'notes/lock-order';+ beforeAll(async () => { engine = await setupDB(); }, 60_000);+ afterAll(async () => { await teardownDB(); });++ const removals = [+ ['deleteChunks', () => engine.deleteChunks(slug)],+ ['an empty fragment upsertChunks', () => engine.upsertChunks(slug, [])],+ ] as const;++ test.each(removals)('%s waits on a writer holding the page instead of deadlocking against it', async (_name, remove) => {+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ await engine.executeRaw('TRUNCATE pages CASCADE');+ const written = await importFromContent(engine, slug, '---\ntitle: Lock order\ntype: note\n---\nA paragraph about acme-example.\n', { noEmbed: true });+ expect(written.chunks).toBeGreaterThan(0);++ let held!: () => void, release!: () => void;+ const pageHeld = new Promise<void>(resolve => { held = resolve; });+ const proceed = new Promise<void>(resolve => { release = resolve; });+ const writer = engine.transaction(async tx => {+ await tx.executeRaw("SELECT id FROM pages WHERE slug = $1 AND source_id = 'default' FOR UPDATE", [slug]);+ held();+ await proceed;+ await tx.executeRaw("DELETE FROM content_chunks WHERE page_id = (SELECT id FROM pages WHERE slug = $1 AND source_id = 'default')", [slug]);+ }).then(() => null, (error: { code?: string }) => error);+ await pageHeld;+ const deleting = remove().then(() => null, (error: { code?: string }) => error);+ let waiting = false;+ try {+ for (let attempt = 0; attempt < 200 && !waiting; attempt++) {+ const [row] = await engine.executeRaw<{ waiting: boolean }>(`SELECT EXISTS (+ SELECT 1 FROM pg_stat_activity WHERE datname = current_database() AND wait_event_type = 'Lock') AS waiting`);+ waiting = row!.waiting;+ if (!waiting) await Bun.sleep(10);+ }+ } finally { release(); }+ const [writerError, deleteError] = await Promise.all([writer, deleting]);+ expect(waiting).toBe(true);+ expect(writerError?.code).toBeUndefined();+ expect(deleteError?.code).toBeUndefined();+ expect(writerError).toBeNull();+ expect(deleteError).toBeNull();+ expect(await engine.executeRaw("SELECT 1 FROM content_chunks c JOIN pages p ON p.id = c.page_id WHERE p.slug = $1", [slug])).toHaveLength(0);+ }, 60_000);+});diff --git a/test/fixtures/goldens/sql-text/chunks.json b/test/fixtures/goldens/sql-text/chunks.jsonindex a6686a7835..1012bec291 100644--- a/test/fixtures/goldens/sql-text/chunks.json+++ b/test/fixtures/goldens/sql-text/chunks.json@@ -79,8 +79,8 @@"string","string"],- "sql": "WITH gone AS ( DELETE FROM content_chunks WHERE page_id = (SELECT id FROM pages WHERE slug = $1 AND source_id = $2) RETURNING page_id ) UPDATE pages SET text_projection_revision = NULL WHERE id = (SELECT page_id FROM gone LIMIT 1) AND text_projection_revision IS NOT NULL",- "textSha256": "7a650092284476e1dbedca3f5bec457882002e4444e5dadf9a37eddbee8d1c65"+ "sql": "WITH page AS (SELECT id FROM pages WHERE slug = $1 AND source_id = $2 FOR UPDATE), gone AS ( DELETE FROM content_chunks WHERE page_id = (SELECT id FROM page) RETURNING page_id ) UPDATE pages SET text_projection_revision = NULL WHERE id = (SELECT page_id FROM gone LIMIT 1) AND text_projection_revision IS NOT NULL",+ "textSha256": "a3c4aafe1c57c1d8c273b7d8b5223e856ecf6f1523684268b1e9050959a51f15"}]},