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.

comfyui: fix zombie engine on install-restart, poll before respawn

purple8861dc257010
diff --git a/src-tauri/src/commands/process.rs b/src-tauri/src/commands/process.rs
index bf8a40e0..791be83d 100644
--- a/src-tauri/src/commands/process.rs
+++ b/src-tauri/src/commands/process.rs
@@ -1035,6 +1035,23 @@ pub fn start_comfyui(state: State<'_, AppState>) -> Result<serde_json::Value, St
return Ok(serde_json::json!({"status": "already_running"}));
}
+ // The port probe alone misses a ComfyUI that is STARTING: it imports for
+ // 20-60s before it binds the port. Spawning a second copy in that window
+ // overwrites the tracked child handle — the first child keeps running
+ // untracked, wins the port bind, and every later stop/restart kills only
+ // the tracked twin while the zombie keeps serving the stale node list
+ // (the "install spinner never ends" chain). Report 'starting' instead;
+ // callers already poll the connection afterwards.
+ {
+ let mut proc = state.comfy_process.lock().unwrap();
+ if let Some(child) = proc.as_mut() {
+ match child.try_wait() {
+ Ok(None) => return Ok(serde_json::json!({"status": "starting"})),
+ _ => *proc = None, // exited or unreapable — clear and start fresh
+ }
+ }
+ }
+
let comfy_path = {
let path = state.comfy_path.lock().unwrap();
path.clone()
@@ -1225,6 +1242,10 @@ pub fn stop_comfyui(state: State<'_, AppState>) -> Result<serde_json::Value, Str
} else {
let _ = child.kill();
}
+ // Reap before returning: guarantees the listen socket is released, so
+ // a start_comfyui right after us cannot lose the port bind (10048) to
+ // the dying process and silently leave the OLD node list being served.
+ let _ = child.wait();
*proc = None;
println!("[ComfyUI] Stopped");
info!(pid = pid, "comfyui stopped");
diff --git a/src/components/create/experimental/CreateContext.tsx b/src/components/create/experimental/CreateContext.tsx
index e453316a..c5cc00c5 100644
--- a/src/components/create/experimental/CreateContext.tsx
+++ b/src/components/create/experimental/CreateContext.tsx
@@ -10,6 +10,30 @@ import { backendCall } from '../../../api/backend'
import { ensureLocalFilename } from './loadImage'
import type { CloudQuota } from '../../../lib/render/cloud-jobs'
+/** Restart ComfyUI so a freshly installed node pack registers (packs only load
+ * on startup). stop_comfyui can only kill the ComfyUI that LU itself spawned:
+ * if the engine was started outside LU (user's own terminal/script), the old
+ * process keeps the port and keeps serving the stale node list — the pack
+ * would look installed but never show up, and the register-poll would burn
+ * 40s to end in a misleading error. Detect that case and state the real fix. */
+async function restartComfyForNewNodes(): Promise<void> {
+ try { await backendCall('stop_comfyui') } catch { /* may already be stopped */ }
+ // stop_comfyui reaps its child before returning, so LU's own engine is down
+ // by now — poll a few extra rounds anyway instead of trusting one sleep.
+ // Whatever still answers after that is an engine LU does not own.
+ let stillUp = await checkComfyConnection()
+ for (let i = 0; stillUp && i < 5; i++) {
+ await new Promise((r) => setTimeout(r, 2000))
+ stillUp = await checkComfyConnection()
+ }
+ if (stillUp) {
+ throw new Error(
+ 'Your ComfyUI is running outside LU, so LU cannot restart it. New node packs only load on startup: restart your ComfyUI yourself, then come back here.',
+ )
+ }
+ await backendCall('start_comfyui')
+}
+
/**
* The seam between the redesigned Create surface and the live backend. Replaces
* the sandbox mockStore's non-persisted actions (generate/cancel) and mockComfy
@@ -223,9 +247,7 @@ export function CreateExpProvider({ children }: { children: ReactNode }) {
: 'Downloading & installing the background removal node. This can take a minute…')
await installCustomNodes([pack])
onProgress?.('Restarting ComfyUI to register the node…')
- try { await backendCall('stop_comfyui') } catch { /* may already be stopped */ }
- await new Promise((r) => setTimeout(r, 2000))
- await backendCall('start_comfyui')
+ await restartComfyForNewNodes()
onProgress?.('Waiting for ComfyUI to come back…')
for (let i = 0; i < 20; i++) {
await new Promise((r) => setTimeout(r, 2000))
@@ -266,9 +288,7 @@ export function CreateExpProvider({ children }: { children: ReactNode }) {
onProgress?.('Installing the required node packs…')
await installCustomNodes(bundle.customNodes)
onProgress?.('Restarting ComfyUI to register the new nodes…')
- try { await backendCall('stop_comfyui') } catch { /* may already be stopped */ }
- await new Promise((r) => setTimeout(r, 2000))
- await backendCall('start_comfyui')
+ await restartComfyForNewNodes()
for (let i = 0; i < 20; i++) {
await new Promise((r) => setTimeout(r, 2000))
if (await checkComfyConnection()) break