Skip to content

Commit 8f970e0

Browse files
committed
Don't swallow exception on unlink failure
When our fallback unlink_file() "deletes" a file that is open elsewhere, it is marked by Windows to be deleted when all others have closed it. But this means that the file is still there, and rmdir() on its parent fails. But we don't want rmtree() to "succeed" when we have leftover directories. Instead, we let that be an error, and rely on rmtree()'s retry loop (if enabled) to go back and try to delete such directories. (Assisted by Claude Code; any errors are mine.)
1 parent 39c2c8a commit 8f970e0

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

src/cjdk/_utils.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,23 @@ def backoff_seconds(initial_interval, max_interval, max_total, factor=1.5):
5050
def rmtree(path, timeout=2.5):
5151
# Try extra hard to clean up a temporary directory.
5252

53-
def retry_unlink(func, path, excinfo):
54-
if func is os.unlink:
55-
unlink_file(path, timeout=0) # Try again with our special version
56-
5753
if sys.version_info >= (3, 12):
54+
55+
def retry_unlink(func, path, exc):
56+
if func is os.unlink:
57+
unlink_file(path, timeout=0)
58+
else:
59+
raise exc
60+
5861
rmtree_kwargs = {"onexc": retry_unlink}
5962
else:
63+
64+
def retry_unlink(func, path, exc_info):
65+
if func is os.unlink:
66+
unlink_file(path, timeout=0)
67+
else:
68+
raise exc_info[1].with_traceback(exc_info[2])
69+
6070
rmtree_kwargs = {"onerror": retry_unlink}
6171

6272
for wait_seconds in backoff_seconds(0.001, 0.5, timeout):

0 commit comments

Comments
 (0)