Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions tests/test_replace_retry_bounds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""Tests for ERROR_LOCK_VIOLATION retry and seconds validation in tools/replace_retry.py."""
import errno
import os
import sys
import tempfile
import unittest
from unittest import mock

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))

import replace_retry # noqa: E402


def _win_perm_error(winerror):
err = PermissionError(errno.EACCES, f"mock transient error {winerror}")
err.winerror = winerror
return err


class ReplaceRetryBoundsTests(unittest.TestCase):
def test_retries_lock_violation_winerror_33(self):
with tempfile.TemporaryDirectory() as tmp:
src = os.path.join(tmp, "src.tmp")
dst = os.path.join(tmp, "dst.txt")
with open(src, "w", encoding="utf-8", newline="\n") as f:
f.write("updated")
with open(dst, "w", encoding="utf-8", newline="\n") as f:
f.write("original")

real_replace = os.replace
calls = []

def fake_replace(s, d):
calls.append((s, d))
if len(calls) <= 2:
raise _win_perm_error(33)
return real_replace(s, d)

with mock.patch.object(replace_retry.os, "replace", fake_replace), \
mock.patch.object(replace_retry.time, "sleep") as sleep_mock:
replace_retry.replace(src, dst)

self.assertEqual(len(calls), 3)
self.assertEqual(sleep_mock.call_count, 2)
with open(dst, encoding="utf-8") as f:
self.assertEqual(f.read(), "updated")

def test_rejects_invalid_seconds_before_calling_replace(self):
invalid_values = (
float("nan"),
float("inf"),
float("-inf"),
-0.5,
10**1000,
-10**1000,
True,
False,
"1",
)
for bad in invalid_values:
with self.subTest(seconds=bad):
calls = []

def guarded_replace(s, d):
calls.append((s, d))
if len(calls) > 1:
raise AssertionError("replace loop did not terminate")
return None

with mock.patch.object(replace_retry.os, "replace", guarded_replace), \
mock.patch.object(replace_retry.time, "sleep") as sleep_mock:
with self.assertRaises(ValueError):
replace_retry.replace("a", "b", seconds=bad)

self.assertEqual(calls, [])
sleep_mock.assert_not_called()

def test_zero_seconds_attempts_once_without_sleeping(self):
calls = []
err = _win_perm_error(32)

def fail_once(s, d):
calls.append((s, d))
if len(calls) > 1:
raise AssertionError("seconds=0 must attempt os.replace at most once")
raise err

with mock.patch.object(replace_retry.os, "replace", fail_once), \
mock.patch.object(replace_retry.time, "sleep") as sleep_mock:
with self.assertRaises(PermissionError) as caught:
replace_retry.replace("a", "b", seconds=0)

self.assertIs(caught.exception, err)
self.assertEqual(len(calls), 1)
sleep_mock.assert_not_called()


if __name__ == "__main__":
unittest.main()
16 changes: 13 additions & 3 deletions tools/replace_retry.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
"""os.replace() that rides out the short windows in which Windows refuses to rename a file it has just seen written.

On Windows, os.replace raises PermissionError (winerror 5 or 32) while another process holds the source or the
On Windows, os.replace raises PermissionError (winerror 5, 32, or 33) while another process holds the source or the
destination open without share-delete: a virus scanner, the search indexer, a cloud-sync client on Documents. The
hold lasts milliseconds to a few seconds, so the rename is tried again for a bounded time and only then re-raised.
Elsewhere, and for every other error, this is os.replace.
"""
import math
import os
import time

RETRY_SECONDS = 5.0
FIRST_DELAY = 0.01
MAX_DELAY = 0.25
_TRANSIENT_WINERRORS = (5, 32) # ERROR_ACCESS_DENIED, ERROR_SHARING_VIOLATION
_TRANSIENT_WINERRORS = (5, 32, 33) # ERROR_ACCESS_DENIED, ERROR_SHARING_VIOLATION, ERROR_LOCK_VIOLATION


def _transient(error):
Expand All @@ -21,7 +22,16 @@ def _transient(error):
def replace(src, dst, *, seconds=None):
"""os.replace(src, dst), retried with a doubling delay for `seconds` (default RETRY_SECONDS) on a transient
Windows PermissionError; the last error is raised once the time is up."""
deadline = time.monotonic() + (RETRY_SECONDS if seconds is None else seconds)
budget = RETRY_SECONDS if seconds is None else seconds
if isinstance(budget, bool) or not isinstance(budget, (int, float)):
raise ValueError(f"seconds must be a finite non-negative number, got {seconds!r}")
try:
finite = math.isfinite(budget)
except OverflowError:
finite = False
if not finite or budget < 0:
raise ValueError(f"seconds must be a finite non-negative number, got {seconds!r}")
deadline = time.monotonic() + budget
delay = FIRST_DELAY
while True:
try:
Expand Down
Loading