Skip to content

Commit 8c2ab44

Browse files
RaviTejaKNTSclaude
andcommitted
Use no-key-update locks so claims and dedupe can't deadlock
A status update doesn't change a key, so the claim, review, and dedupe locks don't need to block foreign-key checks on the row. Adds a two-connection test that fails with the old lock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c25c529 commit 8c2ab44

5 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎scripts/self-host-apps/apply-candidate-review.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export async function upsertInstallOptions(client, appId, options, catalogSource
286286
}
287287

288288
async function candidateById(client, id, dryRun) {
289-
const result = await client.query("select * from public.self_host_app_candidates where id = $1" + (dryRun ? "" : " for update"), [id]);
289+
const result = await client.query("select * from public.self_host_app_candidates where id = $1" + (dryRun ? "" : " for no key update"), [id]);
290290
if (!result.rows[0]) throw new Error(`Missing candidate: ${id}`);
291291
return result.rows[0];
292292
}

‎scripts/self-host-apps/dedupe-candidates.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export async function applyDecisions(client, {
7575
and (candidate.status = 'new' or
7676
(candidate.status = 'needs_review' and candidate.duplicate_reason = 'possible_name_match'))
7777
order by candidate.id
78-
for update of candidate`,
78+
for no key update of candidate`,
7979
[JSON.stringify(expectedCandidates.map(({ id, status, duplicate_reason }) => ({ id, status, duplicate_reason: duplicate_reason ?? null })))],
8080
);
8181
const eligible = new Set(result.rows.map((row) => row.id));

‎scripts/self-host-apps/select-candidate-batch.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export async function requestedIds(args) {
7676
// any of them; a missing or terminal row must leave the whole batch unchanged.
7777
export async function claimIds(client, ids, dryRun = false) {
7878
const result = await client.query(
79-
"select * from public.self_host_app_candidates where id = any($1::uuid[]) order by id" + (dryRun ? "" : " for update"), [ids],
79+
"select * from public.self_host_app_candidates where id = any($1::uuid[]) order by id" + (dryRun ? "" : " for no key update"), [ids],
8080
);
8181
const byId = new Map(result.rows.map((row) => [row.id, row]));
8282
const missing = ids.filter((id) => !byId.has(id));
@@ -138,7 +138,7 @@ async function main() {
138138
from public.self_host_app_candidates
139139
where status = any($1::text[])
140140
order by confidence_score desc, last_seen_at desc, name, id
141-
for update skip locked
141+
for no key update skip locked
142142
limit $2
143143
)
144144
update public.self_host_app_candidates candidate

‎tests/self-host-apps/chosen-candidates.test.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ test("review dry run checks all ten reviewed records using only reads", async ()
9898
queries.push(sql);
9999
if (["begin read only", "rollback"].includes(sql)) return { rows: [] };
100100
assert.match(sql, /^select/i);
101-
assert.doesNotMatch(sql, /for update/i);
101+
assert.doesNotMatch(sql, /for (no key )?update/i);
102102
if (sql.includes("self_host_app_candidates")) return { rows: [{ id: params[0], status: "enriching" }] };
103103
const review = reviews.find((row) => row.app?.slug === params[0]);
104104
return { rows: [{ id: review?.app_id ?? params[0] }] };

‎tests/self-host-apps/dedupe-claim-race.test.mjs‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ for (const status of ["enriching", "published"]) {
2626
// still match that snapshot when the guarded locking query runs.
2727
assert.match(sql, /candidate.status = expected.status/);
2828
assert.match(sql, /candidate.duplicate_reason is not distinct from expected.duplicate_reason/);
29-
assert.match(sql, /for update of candidate/);
29+
assert.match(sql, /for no key update of candidate/);
3030
const expected = JSON.parse(params[0]);
3131
const current = expected.map((row) => ({ ...row, status }));
3232
return { rows: current.filter((row) => expected.some((old) => old.id === row.id && old.status === row.status)) };
@@ -40,7 +40,7 @@ for (const status of ["enriching", "published"]) {
4040
test("unchanged dedupe targets still move forward while changed targets are skipped", async () => {
4141
const writes = [];
4242
const client = { async query(sql, params) {
43-
if (sql.includes("for update of candidate")) return { rows: [{ id: id(4) }] };
43+
if (sql.includes("for no key update of candidate")) return { rows: [{ id: id(4) }] };
4444
writes.push(params);
4545
assert.match(sql, /set status = 'queued'/);
4646
return { rows: [] };
@@ -81,3 +81,31 @@ test("database: a manual claim between dedupe read and write survives all stale
8181
await client.end();
8282
}
8383
});
84+
85+
// A claim must not block dedupe's foreign-key check on the row it holds. Otherwise a
86+
// claim waiting on dedupe's next row and dedupe waiting on the claimed row would deadlock.
87+
test("database: a claimed candidate can still be referenced by a concurrent dedupe write", {
88+
skip: process.env.SELF_HOST_APPS_TEST_ENVIRONMENT !== "development",
89+
}, async () => {
90+
const connect = () => new pg.Client({ connectionString: resolveDatabaseUrl({ environment: "development" }), connectionTimeoutMillis: 15000, query_timeout: 30000 });
91+
const claimer = connect();
92+
const deduper = connect();
93+
try {
94+
await claimer.connect();
95+
await deduper.connect();
96+
const { rows } = await claimer.query("select id from public.self_host_app_candidates where status = 'queued' order by id limit 2");
97+
assert.equal(rows.length, 2, "needs two queued development candidates");
98+
const [claimed, referencing] = rows.map((row) => row.id);
99+
await claimer.query("begin");
100+
await claimIds(claimer, [claimed], false);
101+
await deduper.query("begin");
102+
await deduper.query("set local lock_timeout = '3s'");
103+
// Rolled back below. A blocking lock would fail here with a lock timeout.
104+
await deduper.query("update public.self_host_app_candidates set duplicate_candidate_id = $1 where id = $2", [claimed, referencing]);
105+
} finally {
106+
await deduper.query("rollback").catch(() => {});
107+
await claimer.query("rollback").catch(() => {});
108+
await deduper.end().catch(() => {});
109+
await claimer.end().catch(() => {});
110+
}
111+
});

0 commit comments

Comments
 (0)