Skip to content

Commit 3af597c

Browse files
committed
fix(webapp): a retirement losing its guard restores the winner's engine state
When the guarded null misses because a concurrent override or deploy took the row between the read and the write, the earlier key removal may have erased engine state the winner just synced, including a pause-by-zero; the retire now re-syncs the engine from the fresh row so the winner's bounds stay enforced.
1 parent cbdbde2 commit 3af597c

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

apps/webapp/app/v3/services/createBackgroundWorker.server.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ async function retireStaleAnonymousConcurrencyLimitRows(
451451
);
452452
continue;
453453
}
454-
await prisma.taskQueue.updateMany({
454+
const retired = await prisma.taskQueue.updateMany({
455455
where: { id: row.id, updatedAt: row.updatedAt },
456456
data: {
457457
concurrencyLimit: null,
@@ -465,6 +465,31 @@ async function retireStaleAnonymousConcurrencyLimitRows(
465465
totalConcurrencyLimitOverriddenBy: null,
466466
},
467467
});
468+
if (retired.count === 0) {
469+
/** A concurrent writer (an operator override, or another deploy) took the
470+
* row between the read and the null, and the key removal above may have
471+
* erased the engine state that writer just synced — including a
472+
* pause-by-zero. Restore the engine from the fresh row so the winner's
473+
* bounds stay enforced; both writers write the same fresh values, so the
474+
* race converges. */
475+
const fresh = await prisma.taskQueue.findFirst({ where: { id: row.id } });
476+
if (fresh) {
477+
await Promise.allSettled([
478+
fresh.paused
479+
? updateQueueConcurrencyLimits(environment, fresh.name, 0)
480+
: typeof fresh.concurrencyLimit === "number"
481+
? updateQueueConcurrencyLimits(environment, fresh.name, fresh.concurrencyLimit)
482+
: removeQueueConcurrencyLimits(environment, fresh.name),
483+
typeof fresh.totalConcurrencyLimit === "number"
484+
? updateQueueTotalConcurrencyLimits(
485+
environment,
486+
fresh.name,
487+
fresh.totalConcurrencyLimit
488+
)
489+
: removeQueueTotalConcurrencyLimits(environment, fresh.name),
490+
]);
491+
}
492+
}
468493
}
469494
}
470495

0 commit comments

Comments
 (0)