Skip to content

Commit 544609a

Browse files
committed
fix: Add lock timeouts and tmp download with atomic rename
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent c672479 commit 544609a

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

nc_py_api/ex_app/integration_fastapi.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ def __fetch_model_as_file(
206206
current_progress: int, progress_for_task: int, nc: NextcloudApp, model_path: str, download_options: dict
207207
) -> str:
208208
result_path = download_options.pop("save_path", urlparse(model_path).path.split("/")[-1])
209-
with SoftFileLock(result_path + ".lock"):
209+
temp_path = result_path + ".tmp"
210+
with SoftFileLock(result_path + ".lock", timeout=3600):
210211
with niquests.get(model_path, stream=True) as response:
211212
if not response.ok:
212213
raise ModelFetchError(
@@ -234,7 +235,7 @@ def __fetch_model_as_file(
234235
nc.set_init_status(min(current_progress + progress_for_task, 99))
235236
return result_path
236237

237-
with builtins.open(result_path, "wb") as file:
238+
with builtins.open(temp_path, "wb") as file:
238239
last_progress = current_progress
239240
for chunk in response.iter_raw(-1):
240241
downloaded_size += file.write(chunk)
@@ -243,6 +244,7 @@ def __fetch_model_as_file(
243244
if new_progress != last_progress:
244245
nc.set_init_status(new_progress)
245246
last_progress = new_progress
247+
os.replace(temp_path, result_path)
246248

247249
return result_path
248250

@@ -272,7 +274,7 @@ def display(self, msg=None, pos=None):
272274
if sep:
273275
safe_model_name = safe_model_name.replace(sep, "_")
274276
lock_path = os.path.join(cache, f"{safe_model_name}.lock")
275-
with SoftFileLock(lock_path):
277+
with SoftFileLock(lock_path, timeout=3600):
276278
return snapshot_download(
277279
model_name, tqdm_class=TqdmProgress, **download_options, max_workers=workers, cache_dir=cache
278280
)

0 commit comments

Comments
 (0)