From 66cbcb641b606086088ac753bf626648dd97b045 Mon Sep 17 00:00:00 2001 From: diodiogod Date: Sat, 17 Jan 2026 20:30:36 -0300 Subject: [PATCH] Add retry logic for image loading validation Increase robustness of image loading check before sending canvas data. Now retries up to 5 times with 200ms delays (1 second total) instead of a single 100ms wait. This fixes the 'Failed to get confirmation from server' error that appeared when executing workflows immediately after ComfyUI restart, before images finished loading from IndexedDB. Prevents workflow execution failures due to timing issues during canvas initialization. --- js/CanvasIO.js | 27 +++++++++++++++++---------- src/CanvasIO.ts | 29 +++++++++++++++++++---------- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/js/CanvasIO.js b/js/CanvasIO.js index 833e480..75f58f7 100644 --- a/js/CanvasIO.js +++ b/js/CanvasIO.js @@ -197,16 +197,23 @@ export class CanvasIO { } async _renderOutputData() { log.info("=== RENDERING OUTPUT DATA FOR COMFYUI ==="); - // Check if layers have valid images loaded - const layersWithoutImages = this.canvas.layers.filter(layer => !layer.image || !layer.image.complete); - if (layersWithoutImages.length > 0) { - log.warn(`${layersWithoutImages.length} layer(s) have incomplete image data. Waiting for images to load...`); - // Wait a bit for images to load - await new Promise(resolve => setTimeout(resolve, 100)); - // Check again - const stillIncomplete = this.canvas.layers.filter(layer => !layer.image || !layer.image.complete); - if (stillIncomplete.length > 0) { - throw new Error(`Canvas not ready: ${stillIncomplete.length} layer(s) still have incomplete image data. Try clicking on a layer to force initialization, or wait a moment and try again.`); + // Check if layers have valid images loaded, with retry logic + const maxRetries = 5; + const retryDelay = 200; + for (let attempt = 0; attempt < maxRetries; attempt++) { + const layersWithoutImages = this.canvas.layers.filter(layer => !layer.image || !layer.image.complete); + if (layersWithoutImages.length === 0) { + break; // All images loaded + } + if (attempt === 0) { + log.warn(`${layersWithoutImages.length} layer(s) have incomplete image data. Waiting for images to load...`); + } + if (attempt < maxRetries - 1) { + await new Promise(resolve => setTimeout(resolve, retryDelay)); + } + else { + // Last attempt failed + throw new Error(`Canvas not ready after ${maxRetries} attempts: ${layersWithoutImages.length} layer(s) still have incomplete image data. Try waiting a moment and running again.`); } } // Użyj zunifikowanych funkcji z CanvasLayers diff --git a/src/CanvasIO.ts b/src/CanvasIO.ts index c7708a8..3212b0e 100644 --- a/src/CanvasIO.ts +++ b/src/CanvasIO.ts @@ -218,17 +218,26 @@ export class CanvasIO { async _renderOutputData(): Promise<{ image: string, mask: string }> { log.info("=== RENDERING OUTPUT DATA FOR COMFYUI ==="); - // Check if layers have valid images loaded - const layersWithoutImages = this.canvas.layers.filter(layer => !layer.image || !layer.image.complete); - if (layersWithoutImages.length > 0) { - log.warn(`${layersWithoutImages.length} layer(s) have incomplete image data. Waiting for images to load...`); - // Wait a bit for images to load - await new Promise(resolve => setTimeout(resolve, 100)); + // Check if layers have valid images loaded, with retry logic + const maxRetries = 5; + const retryDelay = 200; - // Check again - const stillIncomplete = this.canvas.layers.filter(layer => !layer.image || !layer.image.complete); - if (stillIncomplete.length > 0) { - throw new Error(`Canvas not ready: ${stillIncomplete.length} layer(s) still have incomplete image data. Try clicking on a layer to force initialization, or wait a moment and try again.`); + for (let attempt = 0; attempt < maxRetries; attempt++) { + const layersWithoutImages = this.canvas.layers.filter(layer => !layer.image || !layer.image.complete); + + if (layersWithoutImages.length === 0) { + break; // All images loaded + } + + if (attempt === 0) { + log.warn(`${layersWithoutImages.length} layer(s) have incomplete image data. Waiting for images to load...`); + } + + if (attempt < maxRetries - 1) { + await new Promise(resolve => setTimeout(resolve, retryDelay)); + } else { + // Last attempt failed + throw new Error(`Canvas not ready after ${maxRetries} attempts: ${layersWithoutImages.length} layer(s) still have incomplete image data. Try waiting a moment and running again.`); } }