Skip to content

Commit 9ef8f71

Browse files
committed
Fix hang when images finish faster than they can be saved to document history
* usually only happened when comfy execution was cached * _save_results is called from inside asyncio eventloop
1 parent ad4ee26 commit 9ef8f71

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

ai_diffusion/eventloop.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ def stop():
4848
pass
4949

5050

51-
def wait(future: asyncio.Future):
52-
event = threading.Event()
53-
future.add_done_callback(lambda _: event.set())
54-
event.wait()
55-
return future.result()
51+
def run_until_complete(future: asyncio.Future):
52+
return _loop.run_until_complete(future)
5653

5754

5855
async def wait_until(condition: Callable[[], bool], iterations=10, no_error=False):

ai_diffusion/persistence.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def __init__(self, model: Model):
134134
def __del__(self):
135135
try:
136136
if self._image_task is not None and not self._image_task.done():
137-
eventloop.wait(self._image_task)
137+
eventloop.run_until_complete(self._image_task)
138138
except Exception as e:
139139
log.warning(f"Persistence: failed to wait for image task completion: {e}")
140140

@@ -229,12 +229,13 @@ def _save_results(self, job: Job):
229229
if job.kind in [JobKind.diffusion, JobKind.animation] and len(job.results) > 0:
230230
slot = self._slot_index
231231
self._slot_index += 1
232+
self._image_task = eventloop.run(self._save_result_images(job, slot, self._image_task))
232233

233-
if self._image_task is not None and not self._image_task.done():
234-
eventloop.wait(self._image_task)
235-
self._image_task = eventloop.run(self._save_result_images(job, slot))
236-
237-
async def _save_result_images(self, job: Job, slot: int):
234+
async def _save_result_images(
235+
self, job: Job, slot: int, prev_task: asyncio.Future | None = None
236+
):
237+
if prev_task is not None:
238+
await prev_task
238239
if settings.multi_threading:
239240
loop = asyncio.get_running_loop()
240241
image_data, image_offsets = await loop.run_in_executor(

0 commit comments

Comments
 (0)