Skip to content

Commit 2dddf75

Browse files
fix: update_prompts validate (#245)
* fix: set_prompts cleanup * fix: add a prompt update validator --------- Co-authored-by: John | Elite Encoder <john@eliteencoder.net>
1 parent 68c93ff commit 2dddf75

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

server/app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ async def on_message(message):
256256
"[Control] Missing prompt in update_prompt message"
257257
)
258258
return
259-
await pipeline.update_prompts(params["prompts"])
259+
try:
260+
await pipeline.update_prompts(params["prompts"])
261+
except Exception as e:
262+
logger.error(f"Error updating prompt: {str(e)}")
260263
response = {"type": "prompts_updated", "success": True}
261264
channel.send(json.dumps(response))
262265
elif params.get("type") == "update_resolution":

src/comfystream/client.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(self, max_workers: int = 1, **kwargs):
2121
self.cleanup_lock = asyncio.Lock()
2222

2323
async def set_prompts(self, prompts: List[PromptDictInput]):
24+
await self.cancel_running_prompts()
2425
self.current_prompts = [convert_prompt(prompt) for prompt in prompts]
2526
for idx in range(len(self.current_prompts)):
2627
task = asyncio.create_task(self.run_prompt(idx))
@@ -32,38 +33,49 @@ async def update_prompts(self, prompts: List[PromptDictInput]):
3233
raise ValueError(
3334
"Number of updated prompts must match the number of currently running prompts."
3435
)
35-
self.current_prompts = [convert_prompt(prompt) for prompt in prompts]
36+
# Validation step before updating the prompt, only meant for a single prompt for now
37+
for idx, prompt in enumerate(prompts):
38+
converted_prompt = convert_prompt(prompt)
39+
try:
40+
await self.comfy_client.queue_prompt(converted_prompt)
41+
self.current_prompts[idx] = converted_prompt
42+
except Exception as e:
43+
raise Exception("Prompt update failed") from e
3644

3745
async def run_prompt(self, prompt_index: int):
3846
while True:
3947
try:
4048
await self.comfy_client.queue_prompt(self.current_prompts[prompt_index])
49+
except asyncio.CancelledError:
50+
raise
4151
except Exception as e:
4252
await self.cleanup()
4353
logger.error(f"Error running prompt: {str(e)}")
4454
raise
4555

4656
async def cleanup(self):
57+
await self.cancel_running_prompts()
4758
async with self.cleanup_lock:
48-
tasks_to_cancel = list(self.running_prompts.values())
49-
for task in tasks_to_cancel:
50-
task.cancel()
51-
try:
52-
await task
53-
except asyncio.CancelledError:
54-
pass
55-
self.running_prompts.clear()
56-
5759
if self.comfy_client.is_running:
5860
try:
5961
await self.comfy_client.__aexit__()
6062
except Exception as e:
6163
logger.error(f"Error during ComfyClient cleanup: {e}")
6264

63-
6465
await self.cleanup_queues()
6566
logger.info("Client cleanup complete")
6667

68+
async def cancel_running_prompts(self):
69+
async with self.cleanup_lock:
70+
tasks_to_cancel = list(self.running_prompts.values())
71+
for task in tasks_to_cancel:
72+
task.cancel()
73+
try:
74+
await task
75+
except asyncio.CancelledError:
76+
pass
77+
self.running_prompts.clear()
78+
6779

6880
async def cleanup_queues(self):
6981
while not tensor_cache.image_inputs.empty():

0 commit comments

Comments
 (0)