fix(studio-server): the cancel route replies once the adapter's stop has settled (#7221)

* fix(studio-server): the cancel route replies once the adapter's stop has settled

* fix(studio-server): shorten the cancel route comments

* fix(studio-server): drop the added cancel comments so the comment share stays down

* fix(studio-server): the cancel route clears its timer when the stop throws
This commit is contained in:
Miguel Ángel
2026-11-05 08:00:38 +00:00
committed by GitHub
parent b98718fe93
commit 18513d46d2
3 changed files with 38 additions and 3 deletions
@@ -739,6 +739,34 @@ describe("POST /render/:jobId/cancel", () => {
}
});
it("replies only once the adapter's stop has settled", async () => {
const { adapter, rendersDir } = createAdapter(vi.fn());
const baseStartRender = adapter.startRender.bind(adapter);
let settled = false;
adapter.startRender = (opts) => {
const state = baseStartRender(opts);
state.cancel = () =>
new Promise<void>((done) =>
setTimeout(() => {
settled = true;
state.status = "failed";
done();
}, 30),
);
return state;
};
const app = new Hono();
registerRenderRoutes(app, adapter);
try {
const jobId = await startJob(app);
const res = await app.request(`http://localhost/render/${jobId}/cancel`, { method: "POST" });
expect(settled).toBe(true);
expect(((await res.json()) as { status: string }).status).toBe("failed");
} finally {
rmSync(rendersDir, { recursive: true, force: true });
}
});
it("does not cancel a job that already completed", async () => {
const spy = vi.fn();
const states: Array<{ status: string }> = [];
+9 -2
View File
@@ -58,6 +58,7 @@ function contentDispositionHeader(disposition: "inline" | "attachment", filename
export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void {
// Scoped job store — not shared across createStudioApi() calls
const renderJobs = new Map<string, RenderJobState>();
const CANCEL_SETTLE_MS = 45_000;
const nextFreeRenderJobId = (baseId: string, ext: string, rendersDir: string): string => {
let jobId = baseId;
@@ -246,13 +247,19 @@ export function registerRenderRoutes(api: Hono, adapter: StudioApiAdapter): void
// Cancel an in-flight render. Marks the job cancelled immediately (so the
// SSE stream terminates) and invokes the adapter's abort hook when present.
api.post("/render/:jobId/cancel", (c) => {
api.post("/render/:jobId/cancel", async (c) => {
const { jobId } = c.req.param();
const job = renderJobs.get(jobId);
if (!job) return c.json({ error: "not found" }, 404);
if (job.status === "rendering") {
job.status = "cancelled";
job.cancel?.();
let giveUp: ReturnType<typeof setTimeout> | undefined;
const late = new Promise((done) => (giveUp = setTimeout(done, CANCEL_SETTLE_MS)));
try {
await Promise.race([job.cancel?.(), late]);
} finally {
clearTimeout(giveUp);
}
}
return c.json({ status: job.status });
});
+1 -1
View File
@@ -25,7 +25,7 @@ export interface RenderJobState {
* stop an in-flight render; adapters that can't abort may omit it (the
* route still marks the job cancelled so the SSE stream terminates).
*/
cancel?: () => void;
cancel?: () => void | Promise<void>;
}
export interface MediaProcessingJobState {