fix(webui): serialize manifest reads with atomic replacement

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-03 15:46:53 +08:00
1 parent 432421bceb
commit 9d7cc0204c
2 files changed
+44 -8

No files matched your search

+1 -3
View File
@@ -597,9 +597,7 @@ def _read_segment_manifest_entries(
*, *,
stats: TranscriptReplayStats | None = None, stats: TranscriptReplayStats | None = None,
) -> list[dict[str, Any]]: ) -> list[dict[str, Any]]:
entries = _load_segment_manifest_entries(session_key) # Windows requires readers to close the manifest before atomic replacement.
if entries is not None:
return entries
with _manifest_rebuild_lock(session_key): with _manifest_rebuild_lock(session_key):
entries = _load_segment_manifest_entries(session_key) entries = _load_segment_manifest_entries(session_key)
if entries is not None: if entries is not None:
+43 -5
View File
@@ -4,6 +4,7 @@ from __future__ import annotations
import json import json
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from pathlib import Path from pathlib import Path
from threading import Event, Lock from threading import Event, Lock
@@ -579,26 +580,63 @@ def test_manifest_repair_is_single_flight(tmp_path, monkeypatch) -> None:
manifest = webui_transcript_segments_dir(key) / "manifest.json" manifest = webui_transcript_segments_dir(key) / "manifest.json"
manifest.write_text("{not json", encoding="utf-8") manifest.write_text("{not json", encoding="utf-8")
original = transcript_module._rebuild_segment_manifest original = transcript_module._rebuild_segment_manifest
original_read_text = Path.read_text
original_replace = transcript_module.os.replace
manifest_lock = transcript_module._manifest_rebuild_lock(key)
rebuild_started = Event() rebuild_started = Event()
release_rebuild = Event() second_attempt = Event()
manifest_open = Event()
release_read = Event()
calls = 0 calls = 0
calls_lock = Lock() calls_lock = Lock()
@contextmanager
def track_lock(_session_key):
if rebuild_started.is_set():
second_attempt.set()
with manifest_lock:
yield
def hold_manifest_read(path, *args, **kwargs):
if path != manifest or not rebuild_started.is_set() or release_read.is_set():
return original_read_text(path, *args, **kwargs)
with path.open(encoding="utf-8") as handle:
manifest_open.set()
second_attempt.set()
try:
assert release_read.wait(5)
return handle.read()
finally:
manifest_open.clear()
def replace_manifest(source, destination):
if destination != manifest:
return original_replace(source, destination)
try:
# Windows cannot replace a file held open by a concurrent reader.
if manifest_open.is_set():
raise PermissionError("manifest is open for reading")
return original_replace(source, destination)
finally:
release_read.set()
def slow_rebuild(*args, **kwargs): def slow_rebuild(*args, **kwargs):
nonlocal calls nonlocal calls
with calls_lock: with calls_lock:
calls += 1 calls += 1
rebuild_started.set() rebuild_started.set()
assert release_rebuild.wait(2) assert second_attempt.wait(5)
return original(*args, **kwargs) return original(*args, **kwargs)
monkeypatch.setattr(transcript_module, "_manifest_rebuild_lock", track_lock)
monkeypatch.setattr(Path, "read_text", hold_manifest_read)
monkeypatch.setattr(transcript_module.os, "replace", replace_manifest)
monkeypatch.setattr(transcript_module, "_rebuild_segment_manifest", slow_rebuild) monkeypatch.setattr(transcript_module, "_rebuild_segment_manifest", slow_rebuild)
with ThreadPoolExecutor(max_workers=2) as executor: with ThreadPoolExecutor(max_workers=2) as executor:
first = executor.submit(transcript_module._read_segment_manifest_entries, key) first = executor.submit(transcript_module._read_segment_manifest_entries, key)
assert rebuild_started.wait(2) assert rebuild_started.wait(5)
second = executor.submit(transcript_module._read_segment_manifest_entries, key) second = executor.submit(transcript_module._read_segment_manifest_entries, key)
release_rebuild.set() assert first.result(timeout=5) == second.result(timeout=5)
assert first.result(timeout=2) == second.result(timeout=2)
assert calls == 1 assert calls == 1