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 <noreply@anthropic.com>
This commit is contained in:
parent
d44caa8014
commit
764d833d42
2 changed files with 10 additions and 6 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue