|
58 | 58 | import os |
59 | 59 | import shutil |
60 | 60 | import tempfile |
| 61 | +import time |
61 | 62 | from pathlib import Path |
62 | 63 | from typing import IO, TYPE_CHECKING, cast |
63 | 64 | from urllib import parse |
|
79 | 80 | # advisory file locking for posix |
80 | 81 | import fcntl |
81 | 82 |
|
82 | | - def _lock_file(f: IO) -> None: |
83 | | - if f.writable(): |
| 83 | + @contextlib.contextmanager |
| 84 | + def _lock_file(path: str) -> Iterator[IO]: |
| 85 | + with open(path, "wb") as f: |
84 | 86 | fcntl.lockf(f, fcntl.LOCK_EX) |
| 87 | + yield f |
85 | 88 |
|
86 | 89 | except ModuleNotFoundError: |
87 | | - # Windows file locking |
| 90 | + # Windows file locking, belt-and-suspenders style: |
| 91 | + # Use loop that tries |
88 | 92 | import msvcrt |
89 | 93 |
|
90 | | - def _lock_file(f: IO) -> None: |
91 | | - # On Windows we lock a byte range and file must not be empty |
92 | | - f.write(b"\0") |
93 | | - f.flush() |
94 | | - f.seek(0) |
95 | | - |
96 | | - msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1) |
| 94 | + @contextlib.contextmanager |
| 95 | + def _lock_file(path: str) -> Iterator[IO]: |
| 96 | + err = None |
| 97 | + locked = False |
| 98 | + for _ in range(100): |
| 99 | + try: |
| 100 | + with open(path, "wb") as f: |
| 101 | + # file must not be empty |
| 102 | + f.write(b"\0") |
| 103 | + f.flush() |
| 104 | + f.seek(0) |
| 105 | + msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1) |
| 106 | + locked = True |
| 107 | + yield f |
| 108 | + return |
| 109 | + except FileNotFoundError: |
| 110 | + # could be from yield or from open() -- either way we bail |
| 111 | + raise |
| 112 | + except OSError as e: |
| 113 | + if locked: |
| 114 | + # yield has raised, let's not continue loop |
| 115 | + raise e |
| 116 | + err = e |
| 117 | + logger.warning("Unsuccessful lock attempt for %s: %s", path, e) |
| 118 | + time.sleep(0.3) |
| 119 | + |
| 120 | + # raise the last failure if we never got a lock |
| 121 | + if err is not None: |
| 122 | + raise err |
97 | 123 |
|
98 | 124 |
|
99 | 125 | class Updater: |
@@ -171,9 +197,9 @@ def _lock_metadata(self) -> Iterator[None]: |
171 | 197 | # Ensure the whole metadata directory structure exists |
172 | 198 | rootdir = Path(self._dir, "root_history") |
173 | 199 | rootdir.mkdir(exist_ok=True, parents=True) |
| 200 | + |
174 | 201 | logger.debug("Getting metadata lock...") |
175 | | - with open(os.path.join(self._dir, ".lock"), "wb") as f: |
176 | | - _lock_file(f) |
| 202 | + with _lock_file(os.path.join(self._dir, ".lock")): |
177 | 203 | yield |
178 | 204 | logger.debug("Released metadata lock") |
179 | 205 |
|
@@ -336,8 +362,7 @@ def download_target( |
336 | 362 | targetinfo.verify_length_and_hashes(target_file) |
337 | 363 |
|
338 | 364 | target_file.seek(0) |
339 | | - with open(filepath, "wb") as destination_file: |
340 | | - _lock_file(destination_file) |
| 365 | + with _lock_file(filepath) as destination_file: |
341 | 366 | shutil.copyfileobj(target_file, destination_file) |
342 | 367 |
|
343 | 368 | logger.debug("Downloaded target %s", targetinfo.path) |
|
0 commit comments