From 3c2598b55d3078da6f0ad1fd84d5ee0e1b4cf6e5 Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Thu, 8 Oct 2026 12:35:33 +0800 Subject: [PATCH] fix(settings): share locks across equivalent Windows paths --- deeptutor/services/settings/interface_settings.py | 11 ++++++++++- tests/capabilities/test_setup_capability.py | 9 +++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/deeptutor/services/settings/interface_settings.py b/deeptutor/services/settings/interface_settings.py index 2448d568c..f3eb160bd 100644 --- a/deeptutor/services/settings/interface_settings.py +++ b/deeptutor/services/settings/interface_settings.py @@ -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: diff --git a/tests/capabilities/test_setup_capability.py b/tests/capabilities/test_setup_capability.py index e139acaf5..38ef3b635 100644 --- a/tests/capabilities/test_setup_capability.py +++ b/tests/capabilities/test_setup_capability.py @@ -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)