From 764d833d424e093a1337c7c63d84c0a4fa8db152 Mon Sep 17 00:00:00 2001 From: le king fu Date: Sun, 8 Nov 2026 12:28:48 -0500 Subject: [PATCH] fix(sync): lock the task row FOR NO KEY UPDATE in link writes The link transaction took FOR SHARE on the task row, then updated that row (updatedAt, #180): two link writes on the same task each held the share lock and waited for the other to upgrade, and Postgres aborted one (40P01). FOR NO KEY UPDATE is the lock the update takes anyway: link writes on one task now run one after the other, and a concurrent task delete is still waited for. Tags keep FOR SHARE. Replayed on a real Postgres (embedded-postgres, outside the repo): FOR SHARE, two concurrent links -> one 40P01; FOR NO KEY UPDATE -> both applied; link vs delete in either order -> no link left. Refs #196 Co-Authored-By: Claude Opus 5.5 --- web/src/app/api/sync/route.ts | 8 +++++--- web/src/app/api/tasks/[id]/tags/route.ts | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/web/src/app/api/sync/route.ts b/web/src/app/api/sync/route.ts index 7dab997..dbe0399 100644 --- a/web/src/app/api/sync/route.ts +++ b/web/src/app/api/sync/route.ts @@ -371,10 +371,12 @@ async function processOperation(op: SyncOperation, userId: string, ctx: RequestC if (action === 'create') { // A link to a deleted task or tag (a device offline while it was // deleted elsewhere) is dropped without a refusal: `applied`, nothing - // written (#196). FOR SHARE waits for a concurrent delete, which - // updates these rows, so the check cannot miss it. + // written (#196). The row locks wait for a concurrent delete, which + // updates these rows, so the check cannot miss it. The task row takes + // FOR NO KEY UPDATE, the lock of the updatedAt write below: FOR SHARE + // would deadlock two link writes on the same task. const [task] = await tx.select({ deletedAt: slTasks.deletedAt }).from(slTasks) - .where(eq(slTasks.id, entityId)).for('share'); + .where(eq(slTasks.id, entityId)).for('no key update'); const [tag] = await tx.select({ deletedAt: slTags.deletedAt }).from(slTags) .where(eq(slTags.id, tagId)).for('share'); if (task?.deletedAt || tag?.deletedAt) return; diff --git a/web/src/app/api/tasks/[id]/tags/route.ts b/web/src/app/api/tasks/[id]/tags/route.ts index 66fe833..3d89e71 100644 --- a/web/src/app/api/tasks/[id]/tags/route.ts +++ b/web/src/app/api/tasks/[id]/tags/route.ts @@ -44,11 +44,13 @@ export async function POST( // Insert (ignore conflicts), and move the task's updatedAt: GET /api/sync // then sends the task with the whole set of its links (#180). Deleted tags // are dropped from the insert without an error, and with none left nothing - // is written (#196). FOR SHARE waits for a concurrent delete of the task or - // a tag, which updates these rows, so a row deleted meanwhile is seen. + // is written (#196). The row locks wait for a concurrent delete of the task + // or a tag, which updates these rows, so a row deleted meanwhile is seen. + // The task row takes FOR NO KEY UPDATE, the lock of the updatedAt write + // below: FOR SHARE would deadlock two link writes on the same task. const written = await db.transaction(async (tx) => { const [live] = await tx.select({ id: slTasks.id }).from(slTasks) - .where(and(eq(slTasks.id, taskId), isNull(slTasks.deletedAt))).for('share'); + .where(and(eq(slTasks.id, taskId), isNull(slTasks.deletedAt))).for('no key update'); if (!live) return false; const liveTags = await tx.select({ id: slTags.id }).from(slTags) .where(and(inArray(slTags.id, body.data.tagIds), isNull(slTags.deletedAt))).for('share');