fix canopy cancel: support pending runs, use docker kill for reliable container cleanup, name containers

Anton Kaminsky21d ago57c315f261cbparent 6b61a0a
2 files changed+12-3
api/src/routes/canopy.ts
@@ -206,14 +206,18 @@
206206 const run = db
207207 .prepare(`${RUN_SELECT} WHERE pr.id = ?`)
208208 .get(parseInt(id)) as any;
209 if (!run || run.status !== "running") {
210 return reply.code(400).send({ error: "Run is not running" });
209 if (!run || (run.status !== "running" && run.status !== "pending")) {
210 return reply.code(400).send({ error: "Run is not running or pending" });
211211 }
212212
213213 runner.cancelRun(parseInt(id));
214214 db.prepare(
215215 `UPDATE pipeline_runs SET status = 'cancelled', finished_at = datetime('now') WHERE id = ?`
216216 ).run(parseInt(id));
217 // Also cancel any pending/running steps
218 db.prepare(
219 `UPDATE pipeline_steps SET status = 'cancelled' WHERE run_id = ? AND status IN ('pending', 'running')`
220 ).run(parseInt(id));
217221
218222 const updated = db
219223 .prepare(`${RUN_SELECT} WHERE pr.id = ?`)
220224
api/src/services/canopy-runner.ts
@@ -580,9 +580,12 @@
580580 ...(step.volumes ?? []).flatMap((v) => ["-v", v]),
581581 ];
582582
583 const containerName = `canopy-${runId}-${stepIndex}`;
583584 const proc = spawn("docker", [
584585 "run",
585586 "--rm",
587 "--name",
588 containerName,
586589 ...volumeArgs,
587590 "-w",
588591 "/workspace",
@@ -637,7 +640,9 @@
637640 }, timeout);
638641
639642 const abortHandler = () => {
640 proc.kill("SIGTERM");
643 spawn("docker", ["kill", containerName]).on("close", () => {
644 proc.kill("SIGTERM");
645 });
641646 insertLog.run(stepId, "stderr", "Step cancelled");
642647 };
643648 signal.addEventListener("abort", abortHandler, { once: true });
644649