Skip to content

Commit 61e0578

Browse files
Handle error if cached file was deleted concurrently
Wrap cache file read in try-except to handle race condition where another async task deletes the file between validation and lock acquisition. Automatically retry with force=True to trigger re-download. Signed-off-by: Marcel Bochtler <marcel.bochtler@bosch.com>
1 parent b3339a1 commit 61e0578

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/python_inspector/utils_pypi.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,9 +1738,24 @@ async def get(
17381738
else:
17391739
if TRACE_DEEP:
17401740
print(f" FILE CACHE HIT: {path_or_url}")
1741-
# also lock on read to avoid race conditions
1742-
with lockfile.FileLock(lock_file).locked(timeout=PYINSP_CACHE_LOCK_TIMEOUT):
1743-
return await get_local_file_content(path=cached, as_text=as_text), cached
1741+
1742+
# File passed validation, lock and read
1743+
# Handle race condition where file might be deleted between validation and lock
1744+
try:
1745+
with lockfile.FileLock(lock_file).locked(timeout=PYINSP_CACHE_LOCK_TIMEOUT):
1746+
return await get_local_file_content(path=cached, as_text=as_text), cached
1747+
except FileNotFoundError:
1748+
# File was deleted by another task after validation - retry with force.
1749+
if TRACE_DEEP:
1750+
print(f" FILE VANISHED after validation, re-downloading: {path_or_url}")
1751+
return await self.get(
1752+
credentials=credentials,
1753+
path_or_url=path_or_url,
1754+
as_text=as_text,
1755+
force=True,
1756+
verbose=verbose,
1757+
echo_func=echo_func,
1758+
)
17441759

17451760

17461761
CACHE = Cache()

0 commit comments

Comments
 (0)