Skip to content

Transform your 3D texturing workflow with the power of generative AI, directly within Blender!

follow a prompt over http when its websocket drops

sakalond3ef0345e313f
diff --git a/stablegen/workflows.py b/stablegen/workflows.py
index b370e35..0c9561e 100644
--- a/stablegen/workflows.py
+++ b/stablegen/workflows.py
@@ -317,6 +317,51 @@ class _WorkflowBase:
detail = f"history: {history_error}; queue: {queue_error}"
return 'unavailable', detail
+ def _follow_prompt_without_websocket(self, server_address, prompt_id, ws,
+ interval=5.0):
+ """Follow a prompt over HTTP after its WebSocket dropped.
+
+ A connection to a remote server can drop while the prompt still runs.
+ The HTTP image fallback then ran at once, found no output yet, and
+ failed the view although ComfyUI finished it moments later. Returns
+ when the prompt completes (the caller then downloads its image), fails
+ or stays out of reach for a heartbeat interval, and at once when the
+ drop was a cancel: cancelling closes the socket and clears the
+ operator's reference to it.
+ """
+ owner = type(self.operator)
+ patience = get_timeout('generation_heartbeat')
+ lost_since = None
+ while True:
+ if (getattr(self.operator, '_cancelled', False)
+ or (hasattr(owner, '_active_ws')
+ and getattr(owner, '_active_ws', None) is not ws)):
+ return
+ state, detail = self._get_prompt_runtime_state(
+ server_address, prompt_id)
+ if state == 'completed':
+ print("[StableGen] ComfyUI finished the prompt after the "
+ "WebSocket dropped. Recovering the result via HTTP.")
+ return
+ if state == 'error':
+ self.operator._error = f"ComfyUI execution error: {detail}"
+ print(f"[StableGen] {self.operator._error}")
+ return
+ if state in {'running', 'queued'}:
+ lost_since = None
+ else:
+ lost_since = lost_since or time.monotonic()
+ if time.monotonic() - lost_since > patience:
+ self.operator._error = (
+ f"Lost contact with ComfyUI prompt {prompt_id} after "
+ f"its WebSocket dropped ({detail})."
+ )
+ print(f"[StableGen] {self.operator._error}")
+ return
+ print(f"[StableGen] WebSocket dropped; prompt is {state} "
+ f"({detail}). Following it over HTTP.")
+ time.sleep(interval)
+
def _connect_to_websocket(self, server_address, client_id):
"""Establishes WebSocket connection to ComfyUI server."""
try:
@@ -469,6 +514,7 @@ class _WorkflowBase:
break
except (ConnectionError, OSError, Exception) as ws_err:
print(f"[StableGen] WebSocket error during generation: {ws_err}")
+ self._follow_prompt_without_websocket(server_address, prompt_id, ws)
break
if isinstance(out, str):