Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(tooling): 保护测试锁持有者信息的读写与清理

longmeidao2f8d35a6c6d5
diff --git a/docs/TESTING.md b/docs/TESTING.md
index 964bb7a4e..8e501c3e5 100644
--- a/docs/TESTING.md
+++ b/docs/TESTING.md
@@ -113,6 +113,8 @@ macOS 按影响域验证:
锁忙时打印持有者并等待,最长 30 分钟;超时退出且不执行测试或合并。集成事务本身仍由
`integration.lock` 串行保护。部分域验证与 CI 分片不签发独立的本机全量记录。
+锁旁的持有者信息用独立文件锁保护读写和清理;主锁仍负责验证与集成互斥。等锁线程关闭记录文件后,持有者才删除记录,避免 Windows 文件共享冲突。
+
测试入口退出码:`0` 验证有效,`1` 用例失败,`2` 参数或锁等待失败,`3` 环境预检失败,
`4` 验证期间代码或依赖变化、记录无效。Windows 托盘遇到 `4` 最多执行三轮完整验证,
只有取得有效记录才继续打包;用例失败直接停止。
diff --git a/scripts/test_evidence.py b/scripts/test_evidence.py
index cdf10db8f..4098b5a8a 100644
--- a/scripts/test_evidence.py
+++ b/scripts/test_evidence.py
@@ -249,15 +249,18 @@ def held(lock_path: Path, *, wait_seconds: float = 0, **note: object):
lock.acquire(timeout=wait_seconds)
try:
record = holder_path(lock_path)
- record.write_text(json.dumps({
- "pid": os.getpid(),
- "started_at": time.strftime("%Y-%m-%d %H:%M:%S"),
- **note,
- }, ensure_ascii=False), encoding="utf-8")
+ record_lock = FileLock(record.with_suffix(".lock"), timeout=5)
+ with record_lock:
+ record.write_text(json.dumps({
+ "pid": os.getpid(),
+ "started_at": time.strftime("%Y-%m-%d %H:%M:%S"),
+ **note,
+ }, ensure_ascii=False), encoding="utf-8")
try:
yield
finally:
- record.unlink(missing_ok=True)
+ with record_lock:
+ record.unlink(missing_ok=True)
finally:
lock.release()
@@ -265,7 +268,11 @@ def held(lock_path: Path, *, wait_seconds: float = 0, **note: object):
def describe_holder(lock_path: Path) -> str:
"""等锁时打印用:`pid 8772、01:54:39 起、scope full、root C:\\…`。"""
try:
- note = json.loads(holder_path(lock_path).read_text(encoding="utf-8"))
+ record = holder_path(lock_path)
+ with FileLock(record.with_suffix(".lock"), timeout=1):
+ note = json.loads(record.read_text(encoding="utf-8"))
+ except Timeout:
+ return "持有者信息暂未取得"
except (OSError, ValueError):
return "持有者刚退出或没留记录"
parts = [f"pid {note.get('pid')}", f"{note.get('started_at')} 起"]
diff --git a/tests/test_test_evidence.py b/tests/test_test_evidence.py
index b6bf9cb59..e3c5af6ad 100644
--- a/tests/test_test_evidence.py
+++ b/tests/test_test_evidence.py
@@ -240,6 +240,50 @@ class VerificationTests(unittest.TestCase):
self.assertFalse(evidence.holder_path(lock).exists())
self.assertIn("刚退出或没留记录", evidence.describe_holder(lock))
+ def test_holder_note_cleanup_waits_for_a_reader_to_close_the_file(self):
+ lock = evidence.evidence_dir(self.repo) / "full-suite.lock"
+ note = evidence.holder_path(lock)
+ owner_ready = threading.Event()
+ reader_open = threading.Event()
+ owner_close = threading.Event()
+ owner_exiting = threading.Event()
+ reader_close = threading.Event()
+ original = Path.read_text
+
+ def read_note(path, *args, **kwargs):
+ if path != note:
+ return original(path, *args, **kwargs)
+ with path.open(encoding="utf-8") as handle:
+ reader_open.set()
+ if not reader_close.wait(5):
+ raise TimeoutError("测试读者没有收到关闭信号")
+ return handle.read()
+
+ def own():
+ with evidence.held(lock, scope="full"):
+ owner_ready.set()
+ if not owner_close.wait(5):
+ raise TimeoutError("测试持有者没有收到退出信号")
+ owner_exiting.set()
+
+ with ThreadPoolExecutor(max_workers=2) as pool, mock.patch.object(Path, "read_text", new=read_note):
+ owner = pool.submit(own)
+ try:
+ self.assertTrue(owner_ready.wait(5))
+ reader = pool.submit(evidence.describe_holder, lock)
+ self.assertTrue(reader_open.wait(5))
+ owner_close.set()
+ self.assertTrue(owner_exiting.wait(5))
+ with self.assertRaises(TimeoutError):
+ owner.result(timeout=0.1)
+ self.assertTrue(note.exists())
+ finally:
+ owner_close.set()
+ reader_close.set()
+ owner.result(timeout=5)
+ self.assertIn("scope full", reader.result(timeout=5))
+ self.assertFalse(note.exists())
+
def test_the_waiting_runner_names_the_holder_of_the_full_suite_lock(self):
lock = evidence.evidence_dir(self.repo) / "full-suite.lock"
output = io.StringIO()