Compare commits

...
Author SHA1 Message Date
Josh Hawkins e8949726c7 release model download waiters when a download or its lock fails
A download that raised or a lock that timed out ended the downloader thread before download_complete was set, so every wait_for_download() caller blocked forever and the file stayed marked as downloading. The thread now always sets download_complete and stops its requestor, and a lock failure reports the download failure notice and error state like a failed download.
2026-11-23 18:34:22 -06:00
2 changed files with 47 additions and 18 deletions
+25
View File
@@ -135,6 +135,31 @@ class TestModelDownloadState(unittest.TestCase):
self.assertEqual(states[-1]["state"], "error")
self.assertEqual(states[-1]["model"], "facedet-facedet.onnx")
def test_raising_download_releases_waiters(self):
def failing(path: str) -> None:
raise RuntimeError("boom")
model_downloader = self._downloader(failing)
with self.assertRaises(RuntimeError):
model_downloader._download_models()
self.assertTrue(model_downloader.download_complete.is_set())
model_downloader.requestor.stop.assert_called_once()
def test_lock_timeout_releases_waiters(self):
model_downloader = self._downloader(lambda path: None)
with patch("frigate.util.downloader.FileLock") as file_lock:
file_lock.return_value.__enter__.side_effect = TimeoutError("lock")
with self.assertRaises(TimeoutError):
model_downloader._download_models()
self.assertTrue(model_downloader.download_complete.is_set())
states = self._state_calls(model_downloader.requestor)
self.assertEqual([s["state"] for s in states], ["error"])
def test_missing_file_marks_error(self):
def swallowing(path: str) -> None:
pass
+22 -18
View File
@@ -85,28 +85,35 @@ class ModelDownloader:
)
def _download_models(self):
try:
self._download_files()
finally:
self.download_complete.set()
self.requestor.stop()
def _download_files(self):
for file_name in self.file_names:
path = os.path.join(self.download_path, file_name)
lock_path = f"{path}.lock"
lock = FileLock(lock_path, cleanup_stale_on_init=True)
if not os.path.exists(path):
with lock:
if not os.path.exists(path):
try:
self.download_func(path)
except Exception as e:
self._report_failure(file_name, _first_line(e))
self._send_state(file_name, ModelStatusTypesEnum.error)
raise
try:
with lock:
if not os.path.exists(path):
self._report_failure(
file_name,
last_download_error.pop(path, "download failed"),
)
self._send_state(file_name, ModelStatusTypesEnum.error)
continue
self.download_func(path)
except Exception as e:
self._report_failure(file_name, _first_line(e))
self._send_state(file_name, ModelStatusTypesEnum.error)
raise
if not os.path.exists(path):
self._report_failure(
file_name,
last_download_error.pop(path, "download failed"),
)
self._send_state(file_name, ModelStatusTypesEnum.error)
continue
self._resolve_failure(file_name)
self._send_state(file_name, ModelStatusTypesEnum.downloaded)
@@ -114,9 +121,6 @@ class ModelDownloader:
if self.complete_func:
self.complete_func()
self.requestor.stop()
self.download_complete.set()
@staticmethod
def download_from_url(url: str, save_path: str, silent: bool = False) -> Path:
temporary_filename = Path(save_path).with_name(