Merge pull request #1818 from dajiaohuang/fix/settings-lock-path

fix(settings): normalize Windows lock path aliases
This commit is contained in:
Bingxi Zhao
2026-10-19 00:20:01 +08:00
committed by GitHub
2 changed files with 13 additions and 7 deletions

View File

@@ -62,7 +62,16 @@ def _settings_lock(path: Path) -> threading.Lock:
multi-user deployment each account has its own ``interface.json``; making
them contend would serialise unrelated users' preference writes.
"""
key = str(path.resolve() if path.is_absolute() else path)
# Resolve the directory only: resolving the file can open it on Windows
# before its lock is acquired, preventing another writer from replacing it.
key = os.path.normcase(str(path.parent.resolve() / path.name))
# Windows may return an extended path while a directory is being created.
# Both spellings must identify the same lock.
if os.name == "nt":
if key.startswith("\\\\?\\unc\\"):
key = "\\\\" + key[8:]
elif key.startswith("\\\\?\\"):
key = key[4:]
with _LOCKS_GUARD:
lock = _LOCKS.get(key)
if lock is None:

View File

@@ -623,8 +623,8 @@ def test_concurrent_preference_writes_do_not_lose_updates(isolated_settings: Pat
Unlocked, twelve concurrent writers left two of their fields on disk — the
rest were overwritten by whichever writer had read an older snapshot.
"""
from concurrent.futures import ThreadPoolExecutor
import json as json_module
import threading
from deeptutor.services.settings.interface_settings import (
_interface_settings_file,
@@ -635,11 +635,8 @@ def test_concurrent_preference_writes_do_not_lose_updates(isolated_settings: Pat
for _ in range(20):
set_ui_setting(f"probe_{index}", index)
threads = [threading.Thread(target=writer, args=(i,)) for i in range(12)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
with ThreadPoolExecutor(max_workers=12) as executor:
list(executor.map(writer, range(12)))
stored = json_module.loads(_interface_settings_file().read_text(encoding="utf-8"))
assert {f"probe_{i}" for i in range(12)} <= set(stored)