mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-28 00:07:08 +03:00
release model download waiters when a download or its lock fails (#25079)
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.
This commit is contained in:
@@ -135,6 +135,31 @@ class TestModelDownloadState(unittest.TestCase):
|
|||||||
self.assertEqual(states[-1]["state"], "error")
|
self.assertEqual(states[-1]["state"], "error")
|
||||||
self.assertEqual(states[-1]["model"], "facedet-facedet.onnx")
|
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 test_missing_file_marks_error(self):
|
||||||
def swallowing(path: str) -> None:
|
def swallowing(path: str) -> None:
|
||||||
pass
|
pass
|
||||||
|
|||||||
+22
-18
@@ -85,28 +85,35 @@ class ModelDownloader:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def _download_models(self):
|
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:
|
for file_name in self.file_names:
|
||||||
path = os.path.join(self.download_path, file_name)
|
path = os.path.join(self.download_path, file_name)
|
||||||
lock_path = f"{path}.lock"
|
lock_path = f"{path}.lock"
|
||||||
lock = FileLock(lock_path, cleanup_stale_on_init=True)
|
lock = FileLock(lock_path, cleanup_stale_on_init=True)
|
||||||
|
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
with lock:
|
try:
|
||||||
if not os.path.exists(path):
|
with lock:
|
||||||
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
|
|
||||||
|
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
self._report_failure(
|
self.download_func(path)
|
||||||
file_name,
|
except Exception as e:
|
||||||
last_download_error.pop(path, "download failed"),
|
self._report_failure(file_name, _first_line(e))
|
||||||
)
|
self._send_state(file_name, ModelStatusTypesEnum.error)
|
||||||
self._send_state(file_name, ModelStatusTypesEnum.error)
|
raise
|
||||||
continue
|
|
||||||
|
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._resolve_failure(file_name)
|
||||||
self._send_state(file_name, ModelStatusTypesEnum.downloaded)
|
self._send_state(file_name, ModelStatusTypesEnum.downloaded)
|
||||||
@@ -114,9 +121,6 @@ class ModelDownloader:
|
|||||||
if self.complete_func:
|
if self.complete_func:
|
||||||
self.complete_func()
|
self.complete_func()
|
||||||
|
|
||||||
self.requestor.stop()
|
|
||||||
self.download_complete.set()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def download_from_url(url: str, save_path: str, silent: bool = False) -> Path:
|
def download_from_url(url: str, save_path: str, silent: bool = False) -> Path:
|
||||||
temporary_filename = Path(save_path).with_name(
|
temporary_filename = Path(save_path).with_name(
|
||||||
|
|||||||
Reference in New Issue
Block a user