mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-18 03:30:39 +09:00
Merge pull request #2643 from chrisschwer/fix/stale-execution-lock-lifecycle
fix: stale execution lock lifecycle (PIP-002)
This commit is contained in:
commit
943b851a5e
2 changed files with 69 additions and 10 deletions
|
|
@ -1632,12 +1632,20 @@ export function issueService(db: Db) {
|
|||
}
|
||||
if (issueData.status && issueData.status !== "in_progress") {
|
||||
patch.checkoutRunId = null;
|
||||
// Fix B: also clear the execution lock when leaving in_progress
|
||||
patch.executionRunId = null;
|
||||
patch.executionAgentNameKey = null;
|
||||
patch.executionLockedAt = null;
|
||||
}
|
||||
if (
|
||||
(issueData.assigneeAgentId !== undefined && issueData.assigneeAgentId !== existing.assigneeAgentId) ||
|
||||
(issueData.assigneeUserId !== undefined && issueData.assigneeUserId !== existing.assigneeUserId)
|
||||
) {
|
||||
patch.checkoutRunId = null;
|
||||
// Fix B: clear execution lock on reassignment, matching checkoutRunId clear
|
||||
patch.executionRunId = null;
|
||||
patch.executionAgentNameKey = null;
|
||||
patch.executionLockedAt = null;
|
||||
}
|
||||
|
||||
const runUpdate = async (tx: any) => {
|
||||
|
|
@ -1732,6 +1740,40 @@ export function issueService(db: Db) {
|
|||
await assertAssignableAgent(issueCompany.companyId, agentId);
|
||||
|
||||
const now = new Date();
|
||||
|
||||
// Fix C: staleness detection — if executionRunId references a run that is no
|
||||
// longer queued or running, clear it before applying the execution lock condition
|
||||
// so a dead lock can't produce a spurious 409.
|
||||
// Wrapped in a transaction with SELECT FOR UPDATE to make the read + clear atomic,
|
||||
// matching the existing pattern in enqueueWakeup().
|
||||
await db.transaction(async (tx) => {
|
||||
await tx.execute(
|
||||
sql`select id from issues where id = ${id} for update`,
|
||||
);
|
||||
const preCheckRow = await tx
|
||||
.select({ executionRunId: issues.executionRunId })
|
||||
.from(issues)
|
||||
.where(eq(issues.id, id))
|
||||
.then((rows) => rows[0] ?? null);
|
||||
if (!preCheckRow?.executionRunId) return;
|
||||
const lockRun = await tx
|
||||
.select({ id: heartbeatRuns.id, status: heartbeatRuns.status })
|
||||
.from(heartbeatRuns)
|
||||
.where(eq(heartbeatRuns.id, preCheckRow.executionRunId))
|
||||
.then((rows) => rows[0] ?? null);
|
||||
if (!lockRun || (lockRun.status !== "queued" && lockRun.status !== "running")) {
|
||||
await tx
|
||||
.update(issues)
|
||||
.set({ executionRunId: null, executionAgentNameKey: null, executionLockedAt: null, updatedAt: now })
|
||||
.where(
|
||||
and(
|
||||
eq(issues.id, id),
|
||||
eq(issues.executionRunId, preCheckRow.executionRunId),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
const sameRunAssigneeCondition = checkoutRunId
|
||||
? and(
|
||||
eq(issues.assigneeAgentId, agentId),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue