Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(workflow): cancel PNG export when editor closes
JPPhotob036ac6eb5bd
diff --git a/docs/src/content/docs/features/Workflows/editor-interface.mdx b/docs/src/content/docs/features/Workflows/editor-interface.mdxindex 2c88429a37..8d23fb07bb 100644--- a/docs/src/content/docs/features/Workflows/editor-interface.mdx+++ b/docs/src/content/docs/features/Workflows/editor-interface.mdx@@ -58,6 +58,8 @@ Earlier releases wrote every edit of a workflow opened from the library straight</Card><Card title="Workflow Image Export" icon="download">Click the camera button in the workflow editor controls to download a PNG of the complete workflow, rendered at up to 2x resolution. The image includes connectors, the background grid, and the selected Invoke color scheme, with nodes shown deselected and the MiniMap omitted.++ Closing the originating workflow editor while the PNG is being prepared cancels the export.</Card>### Managing Nodesdiff --git a/invokeai/frontend/webv2/src/features/workflow/ui/editor/InvocationFlowNode.browser.test.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/editor/InvocationFlowNode.browser.test.tsxindex 09333a12ae..080fff5a29 100644--- a/invokeai/frontend/webv2/src/features/workflow/ui/editor/InvocationFlowNode.browser.test.tsx+++ b/invokeai/frontend/webv2/src/features/workflow/ui/editor/InvocationFlowNode.browser.test.tsx@@ -25,6 +25,7 @@ import {createProjectGraph,projectGraphReducer,} from '@features/workflow/utility';+import { downloadBlob } from '@platform/browser/downloadBlob';import { QueryClient, QueryClientProvider } from '@tanstack/react-query';import { system } from '@theme/system';import { applyNodeChanges, ReactFlow, type NodeChange } from '@xyflow/react';@@ -784,13 +785,14 @@ describe('InvocationFlowNode output preview', () => {);it.each([- { cardinality: 'SINGLE', failed: false },- { cardinality: 'COLLECTION', failed: false },- { cardinality: 'SINGLE', failed: true },- { cardinality: 'COLLECTION', failed: true },+ { cardinality: 'SINGLE', failed: false, teardown: false },+ { cardinality: 'COLLECTION', failed: false, teardown: false },+ { cardinality: 'SINGLE', failed: true, teardown: false },+ { cardinality: 'COLLECTION', failed: true, teardown: false },+ { cardinality: 'SINGLE', failed: false, teardown: true },] as const)(- 'exports authored source images for $cardinality fields with failed=$failed',- async ({ cardinality, failed }) => {+ 'exports authored source images for $cardinality fields with failed=$failed and teardown=$teardown',+ async ({ cardinality, failed, teardown }) => {const source = outputImage(800, 400);const thumbnailSpy = vi.spyOn(galleryImageUrls, 'thumbnail').mockReturnValue(source);const value = { image_name: 'authored-source.png' };@@ -851,18 +853,40 @@ describe('InvocationFlowNode output preview', () => {expect(getComputedStyle(sourceImage!).objectFit).toBe(cardinality === 'SINGLE' ? 'contain' : 'cover');expect(sourceImage!.getBoundingClientRect().width).toBeLessThan(800);expect(sourceImage!.getBoundingClientRect().height).toBeLessThanOrEqual(128);+ const flowElement = host.querySelector<HTMLElement>('.react-flow')!;const exportPromise = exportWorkflowAsPng({bounds: { x: 20, y: 20, width: 300, height: 260 },fallbackWorkflowName: 'Untitled Workflow',- flowElement: host.querySelector<HTMLElement>('.react-flow')!,+ flowElement,workflowName: 'Authored source image',});+ const outcome = exportPromise.then(+ () => null,+ (error: Error) => error+ );await vi.waitFor(() => expect(decodeSpy).toHaveBeenCalledOnce());expect(exportMocks.toBlob).not.toHaveBeenCalled();+ if (teardown) {+ await act(() => root.unmount());+ root = createRoot(host);+ const nextGraph = { ...projectGraph, id: 'next-workflow' };+ const nextAdapter = createAdapter(createExecutionPort().port, projectSnapshotFor(nextGraph));+ await render(nextAdapter);+ expect(flowElement.isConnected).toBe(false);+ expect(host.querySelector('.react-flow')?.isConnected).toBe(true);+ }releaseImage();- await exportPromise;+ const error = await outcome;decodeSpy.mockRestore();naturalWidthSpy.mockRestore();+ if (teardown) {+ expect(error).toBeInstanceOf(Error);+ expect(error?.message).toContain('canceled');+ expect(exportMocks.toBlob).not.toHaveBeenCalled();+ expect(downloadBlob).not.toHaveBeenCalled();+ return;+ }+ expect(error).toBeNull();expect(exportedBlob?.type).toBe('image/png');const bitmap = await createImageBitmap(exportedBlob!);const canvas = document.createElement('canvas');diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/editor/workflowImageExport.edgeCases.test.ts b/invokeai/frontend/webv2/src/features/workflow/ui/editor/workflowImageExport.edgeCases.test.tsindex bb19bf0437..5faf803f21 100644--- a/invokeai/frontend/webv2/src/features/workflow/ui/editor/workflowImageExport.edgeCases.test.ts+++ b/invokeai/frontend/webv2/src/features/workflow/ui/editor/workflowImageExport.edgeCases.test.ts@@ -25,6 +25,7 @@ type FakeElement = {children: FakeElement[];cloneNode: () => FakeElement;id?: string;+ isConnected: boolean;matches: () => boolean;parentElement: FakeElement | null;getBoundingClientRect: () => { left: number; top: number; width: number; height: number };@@ -43,6 +44,7 @@ const createFakeElement = (overrides: Partial<FakeElement> = {}): FakeElement =>attributes: [],children: [],cloneNode: () => element,+ isConnected: true,getBoundingClientRect: () => ({ left: 0, top: 0, width: 1000, height: 1000 }),matches: () => false,parentElement: null,@@ -198,6 +200,29 @@ describe('workflow image export edge cases', () => {});+ it('cancels download if the editor unmounts during rasterization and removes staging', async () => {+ const { flowElement, stagingWrapper } = createExportDom();+ let finish!: (blob: Blob) => void;+ vi.mocked(toBlob).mockReturnValue(+ new Promise<Blob>((resolve) => {+ finish = resolve;+ })+ );+ const exportPromise = exportWorkflowAsPng({+ flowElement: flowElement as unknown as HTMLElement,+ bounds: { x: 0, y: 0, width: 100, height: 100 },+ workflowName: 'Workflow',+ fallbackWorkflowName: 'Unnamed Workflow',+ });+ const rejection = expect(exportPromise).rejects.toThrow('canceled');+ await vi.waitFor(() => expect(toBlob).toHaveBeenCalledOnce());+ flowElement.isConnected = false;+ finish(new Blob(['png'], { type: 'image/png' }));+ await rejection;+ expect(downloadBlob).not.toHaveBeenCalled();+ expect(stagingWrapper.remove).toHaveBeenCalledOnce();+ });+it('allows one bounded retry when a previous rasterization never settles', async () => {vi.useFakeTimers();let finishRasterization: (blob: Blob | null) => void = () => undefined;diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/editor/workflowImageExport.ts b/invokeai/frontend/webv2/src/features/workflow/ui/editor/workflowImageExport.tsindex ba5fccbeb2..777e49de29 100644--- a/invokeai/frontend/webv2/src/features/workflow/ui/editor/workflowImageExport.ts+++ b/invokeai/frontend/webv2/src/features/workflow/ui/editor/workflowImageExport.ts@@ -585,6 +585,9 @@ export const exportWorkflowAsPng = async ({}const decoded = await decodeSourceImages(flowElement);+ if (!flowElement.isConnected) {+ throw new Error('Workflow image export canceled because the editor was unmounted');+ }const contentBounds = getWorkflowContentBounds(flowElement, bounds, { includeInputFieldLabels: false });const dimensions = getWorkflowImageDimensions(contentBounds);const clone = flowElement.cloneNode(true) as HTMLElement;@@ -618,6 +621,9 @@ export const exportWorkflowAsPng = async ({if (!blob) {throw new Error('Workflow image export returned an empty Blob');}+ if (!flowElement.isConnected) {+ throw new Error('Workflow image export canceled because the editor was unmounted');+ }downloadPng(blob, workflowName, fallbackWorkflowName);} finally {stagingWrapper.remove();