mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:08:38 +00:00
ds_permute_probe: ride out a short Windows hold on the report, name a long one
Kal0s-design's check of #3090: on Windows, writing the report over one another program holds open without share-delete now failed with "Access is denied", where the old truncating write overwrote it. The rename now goes through replace_retry, as the other atomic writers in tools/ do, so a short hold (scanner, indexer, sync client) is ridden out; a hold that lasts says the report may be open in another program. The old report is kept and no temporary file is left either way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -110,6 +110,49 @@ class DsPermuteProbeTests(unittest.TestCase):
|
||||
self.assertNotIn(b"\r\n", raw)
|
||||
self.assertEqual(json.loads(raw.decode("utf-8")), payload)
|
||||
|
||||
@staticmethod
|
||||
def _held_open(winerror):
|
||||
# PermissionError as Windows raises it while another handle holds the file without share-delete.
|
||||
e = PermissionError(13, "Access is denied")
|
||||
e.winerror = winerror
|
||||
return e
|
||||
|
||||
def test_write_json_report_rides_out_a_short_windows_hold(self):
|
||||
import replace_retry
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
out_path = os.path.join(tmp, "out.json")
|
||||
with open(out_path, "w", encoding="utf-8") as f:
|
||||
f.write("{}")
|
||||
real_replace = os.replace
|
||||
calls = []
|
||||
|
||||
def held_once(src, dst):
|
||||
calls.append(dst)
|
||||
if len(calls) == 1:
|
||||
raise self._held_open(32)
|
||||
return real_replace(src, dst)
|
||||
|
||||
with mock.patch.object(replace_retry.os, "replace", side_effect=held_once):
|
||||
err = ds_permute_probe.write_json_report(out_path, {"ok": True})
|
||||
self.assertIsNone(err)
|
||||
self.assertEqual(len(calls), 2)
|
||||
with open(out_path, encoding="utf-8") as f:
|
||||
self.assertEqual(json.load(f), {"ok": True})
|
||||
self.assertEqual(os.listdir(tmp), ["out.json"])
|
||||
|
||||
def test_write_json_report_names_a_report_held_open(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
out_path = os.path.join(tmp, "out.json")
|
||||
with open(out_path, "w", encoding="utf-8") as f:
|
||||
f.write('{"old": true}')
|
||||
with mock.patch.object(ds_permute_probe, "replace_retry", side_effect=self._held_open(5)):
|
||||
err = ds_permute_probe.write_json_report(out_path, {"ok": True})
|
||||
self.assertIn("cannot write --json", err)
|
||||
self.assertIn("open in another program", err)
|
||||
with open(out_path, encoding="utf-8") as f:
|
||||
self.assertEqual(json.load(f), {"old": True})
|
||||
self.assertEqual(os.listdir(tmp), ["out.json"])
|
||||
|
||||
def test_write_json_report_and_main_catch_oserror_cleanly(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
out_path = os.path.join(tmp, "out.json")
|
||||
|
||||
@ -23,6 +23,8 @@ sys.dont_write_bytecode = True
|
||||
if HERE not in sys.path:
|
||||
sys.path.insert(0, HERE)
|
||||
|
||||
from replace_retry import replace as replace_retry # noqa: E402
|
||||
|
||||
SENTINEL = 0xCAFE0000
|
||||
PAYLOAD_BASE = 0x1000
|
||||
|
||||
@ -209,8 +211,13 @@ def write_json_report(path, data):
|
||||
) as f:
|
||||
tmp_path = f.name
|
||||
json.dump(data, f, indent=2)
|
||||
os.replace(tmp_path, path)
|
||||
# Windows refuses the rename while another program holds the report open without share-delete (a viewer,
|
||||
# an editor, a virus scanner); replace_retry rides out a short hold, and a long one is reported as such.
|
||||
replace_retry(tmp_path, path)
|
||||
tmp_path = None
|
||||
except PermissionError as e:
|
||||
hint = " (is the report open in another program? close it and run again)" if getattr(e, "winerror", None) else ""
|
||||
return f"cannot write --json {path}: {e.strerror or e}{hint}"
|
||||
except OSError as e:
|
||||
return f"cannot write --json {path}: {e.strerror or e}"
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user