@@ -112,6 +112,8 @@ def __init__(
112112 self ._stop_event = threading .Event ()
113113 self ._downloads_changed_event = threading .Event ()
114114 self ._install_completed_event = threading .Event ()
115+ self ._restore_completed_event = threading .Event ()
116+ self ._restore_completed_event .set ()
115117 self ._download_queue = download_queue
116118 self ._download_cache : Dict [int , ModelInstallJob ] = {}
117119 self ._running = False
@@ -264,16 +266,23 @@ def _restore_incomplete_installs(self) -> None:
264266 self ._safe_rmtree (job ._install_tmpdir , self ._logger )
265267
266268 def _restore_incomplete_installs_async (self ) -> None :
269+ self ._restore_completed_event .clear ()
270+
267271 def _run () -> None :
268272 try :
269273 self ._logger .info ("Restoring incomplete installs" )
270274 self ._restore_incomplete_installs ()
271275 self ._logger .info ("Finished restoring incomplete installs" )
272276 except Exception as e :
273277 self ._logger .error (f"Failed to restore incomplete installs: { e } " )
278+ finally :
279+ self ._restore_completed_event .set ()
274280
275281 threading .Thread (target = _run , daemon = True ).start ()
276282
283+ def _wait_for_restore_complete (self ) -> None :
284+ self ._restore_completed_event .wait ()
285+
277286 def _resume_remote_download (self , job : ModelInstallJob ) -> None :
278287 job .status = InstallStatus .WAITING
279288 if job .download_parts :
@@ -459,6 +468,8 @@ def heuristic_import(
459468 return self .import_model (source_obj , config )
460469
461470 def import_model (self , source : ModelSource , config : Optional [ModelRecordChanges ] = None ) -> ModelInstallJob : # noqa D102
471+ self ._wait_for_restore_complete ()
472+
462473 similar_jobs = [x for x in self .list_jobs () if x .source == source and not x .in_terminal_state ]
463474 if similar_jobs :
464475 self ._logger .warning (f"There is already an active install job for { source } . Not enqueuing." )
@@ -506,6 +517,8 @@ def wait_for_job(self, job: ModelInstallJob, timeout: int = 0) -> ModelInstallJo
506517
507518 def wait_for_installs (self , timeout : int = 0 ) -> List [ModelInstallJob ]: # noqa D102
508519 """Block until all installation jobs are done."""
520+ self ._wait_for_restore_complete ()
521+
509522 start = time .time ()
510523 while len (self ._download_cache ) > 0 :
511524 if self ._downloads_changed_event .wait (timeout = 0.25 ): # in case we miss an event
0 commit comments