Skip to content

Commit 60565cf

Browse files
committed
fix(run-store): short-circuit expireRunsBatch on an empty runIds array
1 parent 1a5ccdc commit 60565cf

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

internal-packages/run-store/src/PostgresRunStore.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,53 @@ describe("PostgresRunStore", () => {
670670
}
671671
);
672672

673+
postgresTest(
674+
"expireRunsBatch returns 0 and writes nothing when runIds is empty",
675+
async ({ prisma }) => {
676+
const { organization, project, environment } = await seedEnvironment(prisma);
677+
678+
const store = new PostgresRunStore({ prisma, readOnlyPrisma: prisma });
679+
680+
const runId = "run_expire_batch_empty";
681+
await prisma.taskRun.create({
682+
data: {
683+
id: runId,
684+
engine: "V2",
685+
status: "PENDING",
686+
friendlyId: "run_expire_batch_empty_friendly",
687+
runtimeEnvironmentId: environment.id,
688+
environmentType: "DEVELOPMENT",
689+
organizationId: organization.id,
690+
projectId: project.id,
691+
taskIdentifier: "my-task",
692+
payload: "{}",
693+
payloadType: "application/json",
694+
traceContext: {},
695+
traceId: "trace_empty",
696+
spanId: "span_empty",
697+
queue: "task/my-task",
698+
isTest: false,
699+
taskEventStore: "taskEvent",
700+
depth: 0,
701+
},
702+
});
703+
704+
const error = { type: "STRING_ERROR" as const, raw: "unused" };
705+
706+
// Must not throw (Prisma.join([]) would build an invalid `IN ()` clause).
707+
const count = await store.expireRunsBatch([], { error, now: new Date() });
708+
709+
expect(count).toBe(0);
710+
711+
const row = await prisma.taskRun.findUniqueOrThrow({
712+
where: { id: runId },
713+
select: { status: true, expiredAt: true },
714+
});
715+
expect(row.status).toBe("PENDING");
716+
expect(row.expiredAt).toBeNull();
717+
}
718+
);
719+
673720
postgresTest(
674721
"lockRunToWorker sets status to DEQUEUED with lock columns, includes runtimeEnvironment, and creates one PENDING_EXECUTING snapshot",
675722
async ({ prisma }) => {

internal-packages/run-store/src/PostgresRunStore.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ export class PostgresRunStore implements RunStore {
335335
): Promise<number> {
336336
const prisma = tx ?? this.prisma;
337337

338+
// Nothing to do for an empty set, and Prisma.join would build an invalid
339+
// `IN ()` clause, so short-circuit before touching the database.
340+
if (runIds.length === 0) {
341+
return 0;
342+
}
343+
338344
return prisma.$executeRaw`
339345
UPDATE "TaskRun"
340346
SET "status" = 'EXPIRED'::"TaskRunStatus",

0 commit comments

Comments
 (0)