Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(create): Cancel on a running render says it will finish and be charged, and keeps polling

purple5013339bec6a
diff --git a/src/hooks/__tests__/useCloudCreate-cancel-on-a-running-job.test.ts b/src/hooks/__tests__/useCloudCreate-cancel-on-a-running-job.test.ts
new file mode 100644
index 00000000..da6a4bc3
--- /dev/null
+++ b/src/hooks/__tests__/useCloudCreate-cancel-on-a-running-job.test.ts
@@ -0,0 +1,75 @@
+/**
+ * Bug hunt 01.10.2026 (K7). Cancel on a job a GPU already took got a 409 from
+ * the server, stopped the polling and said nothing. The job finished and was
+ * charged, and the desktop has no job list to bring it back later. Now the
+ * customer is told, and the poll keeps running so the result lands.
+ */
+import { it, expect, vi, beforeEach } from 'vitest'
+
+vi.mock('react', async (orig) => {
+ const actual = (await orig()) as Record<string, unknown>
+ return { ...actual, useCallback: (fn: unknown) => fn }
+})
+
+const hoisted = vi.hoisted(() => {
+ class FakeCloudJobError extends Error {
+ status: number
+ code?: string
+ constructor(message: string, status: number, meta?: { code?: string }) {
+ super(message)
+ this.status = status
+ this.code = meta?.code
+ }
+ }
+ let release: (v: unknown) => void = () => {}
+ return {
+ FakeCloudJobError,
+ release: (v: unknown) => release(v),
+ submitCloudJob: vi.fn(async () => ({ id: 'job-run', quota: { cost: 1000, used: 1000, limit: 100000 } })),
+ // Honours the abort signal like the real poll.
+ pollJob: vi.fn((_id: string, opts?: { signal?: AbortSignal }) => new Promise((resolve, reject) => {
+ release = resolve
+ opts?.signal?.addEventListener('abort', () => reject(new Error('aborted')))
+ })),
+ cancelJob: vi.fn(async () => { throw new FakeCloudJobError('job is no longer cancelable', 409) }),
+ }
+})
+
+vi.mock('../useContentPolicy', () => ({
+ contentPolicySnapshot: () => 'off' as const,
+ loadContentPolicy: async () => 'off' as const,
+}))
+vi.mock('../../api/cloud/jobs', () => ({
+ uploadInput: vi.fn(),
+ getJob: vi.fn(),
+ submitCloudJob: hoisted.submitCloudJob,
+ pollJob: hoisted.pollJob,
+ cancelJob: hoisted.cancelJob,
+ CloudJobError: hoisted.FakeCloudJobError,
+ QuoteChangedError: class extends hoisted.FakeCloudJobError {},
+}))
+vi.mock('../../api/cloud/studio', () => ({ studioQuote: vi.fn(), StudioQuoteChangedError: class extends Error {} }))
+
+import { useCloudCreate, RENDER_CANNOT_STOP } from '../useCloudCreate'
+import { useCreateStore } from '../../stores/createStore'
+
+beforeEach(() => {
+ vi.stubGlobal('fetch', vi.fn(async () => ({ ok: true, blob: async () => new Blob(['x']) })))
+ useCreateStore.setState({ isGenerating: false, error: null, backend: 'cloud', gallery: [], source: null })
+ useCreateStore.getState().setIntent('image')
+ useCreateStore.getState().setPrompt('a lighthouse at dusk')
+})
+
+it('Cancel on a running job says it cannot stop, and the result still lands', async () => {
+ const hook = useCloudCreate()
+ const run = hook.generate()
+ await vi.waitFor(() => expect(hoisted.pollJob).toHaveBeenCalled())
+ await hook.cancel()
+ expect(useCreateStore.getState().error).toBe(RENDER_CANNOT_STOP)
+ hoisted.release({
+ id: 'job-run', kind: 'image', model: 'flux-schnell', status: 'succeeded',
+ result_url: 'https://storage.example/out.png', created_at: new Date().toISOString(),
+ })
+ await run
+ expect(useCreateStore.getState().gallery.some((g) => g.jobId === 'job-run')).toBe(true)
+})
diff --git a/src/hooks/useCloudCreate.ts b/src/hooks/useCloudCreate.ts
index 33f2194e..7b2a64e4 100644
--- a/src/hooks/useCloudCreate.ts
+++ b/src/hooks/useCloudCreate.ts
@@ -191,6 +191,10 @@ export function dataUrlToBlob(dataUrl: string): Blob {
// died with the Create view's remount, leaving the Cancel button a no-op
// mid-render. At most one cloud job runs per app (the store-level isGenerating
// gate), so a singleton is correct.
+/** Shown when Cancel reaches a job a GPU already took. */
+export const RENDER_CANNOT_STOP =
+ "This render already started and can't be stopped. It will finish, is charged, and lands in your gallery."
+
let activeJobId: string | null = null
let activeAbort: AbortController | null = null
@@ -686,13 +690,24 @@ export function useCloudCreate(opts: { onQuotaChange?: () => void } = {}) {
const cancel = useCallback(async () => {
const id = activeJobId
- activeAbort?.abort() // stop polling immediately either way
- if (!id) return
+ if (!id) {
+ activeAbort?.abort()
+ return
+ }
try {
await cancelJob(id)
+ activeAbort?.abort()
onQuotaChange?.() // queued-cancel refunds
- } catch {
- // 409 (already running/finished) — poll stop is all the client can do
+ } catch (err) {
+ // 409: a GPU already took the job (bug hunt 01.10.2026, K7). The cancel
+ // used to stop the polling and say nothing, so it looked cancelled while
+ // it finished and kept its credits, and the desktop has no job list to
+ // bring it back later. Say so, and keep polling so the result lands.
+ if (err instanceof CloudJobError && err.status === 409) {
+ useCreateStore.getState().setError(RENDER_CANNOT_STOP)
+ return
+ }
+ activeAbort?.abort()
}
}, [onQuotaChange])