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');