Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(config): plain reads take no lock (#2399)

Call White83da7f24637c
diff --git a/config/v2_config.py b/config/v2_config.py
index b6334afeb..73376a98e 100644
--- a/config/v2_config.py
+++ b/config/v2_config.py
@@ -3474,25 +3474,35 @@ class V2Config:
Only the replacement service's lock-owning startup opts into migration
persistence; incidental reads must not invalidate that older reader.
Explicit saves remain writes, and recovery keeps its backup evidence.
+
+ A plain load (``persist_migrations=False``) never writes config.json and
+ takes no config lock. Its only disk effects are data-dir bootstrap and
+ uniquely named, append-only evidence backups, both outside the config
+ lock. Every writer publishes the file with an atomic replace, so the read
+ sees one whole snapshot: the old file or the new one. The read takes no
+ lock in either mode: migration persistence re-reads the file under the
+ config lock and replaces it only if it still holds these bytes. A lock
+ here would also invert the canonical order (config lock, then SQLite
+ writer) for every caller that reads config inside an open SQLite write
+ transaction, while a config writer waits for that SQLite writer.
"""
paths.ensure_data_dirs()
path = config_path or paths.get_config_path()
- with CONFIG_LOCK:
- if not path.exists():
- raise FileNotFoundError(f"Config not found: {path}")
- raw_bytes = b""
- try:
- raw_bytes = path.read_bytes()
- raw = raw_bytes.decode("utf-8")
- except UnicodeDecodeError as exc:
- backup = _backup_config_file(path, "invalid-encoding", content=raw_bytes)
- warning = f"Config is not valid UTF-8; using recovery defaults: {exc.reason}"
- logger.error("%s (backup=%s)", warning, backup)
- config = cls._recovery_default()
- config.load_warnings = (warning,)
- config.recovered_sections = ()
- config.whole_config_recovery = True
- return config
+ if not path.exists():
+ raise FileNotFoundError(f"Config not found: {path}")
+ raw_bytes = b""
+ try:
+ raw_bytes = path.read_bytes()
+ raw = raw_bytes.decode("utf-8")
+ except UnicodeDecodeError as exc:
+ backup = _backup_config_file(path, "invalid-encoding", content=raw_bytes)
+ warning = f"Config is not valid UTF-8; using recovery defaults: {exc.reason}"
+ logger.error("%s (backup=%s)", warning, backup)
+ config = cls._recovery_default()
+ config.load_warnings = (warning,)
+ config.recovered_sections = ()
+ config.whole_config_recovery = True
+ return config
try:
payload = json.loads(raw)
diff --git a/tests/test_config_write_transaction.py b/tests/test_config_write_transaction.py
index ab1010ae7..be249679b 100644
--- a/tests/test_config_write_transaction.py
+++ b/tests/test_config_write_transaction.py
@@ -368,3 +368,127 @@ def test_transaction_reentrant_with_save(isolated_config_home: Path) -> None:
"provider_id": "OpenAI",
"base_url": "https://r/v1",
}
+
+
+def test_plain_load_completes_while_another_thread_holds_the_config_lock(
+ isolated_config_home: Path,
+) -> None:
+ """A plain read takes no config lock: a held writer section cannot stall it."""
+ from config import v2_config as module
+
+ held = threading.Event()
+ release = threading.Event()
+ loaded: list[V2Config] = []
+
+ def writer_section() -> None:
+ with module.CONFIG_LOCK:
+ held.set()
+ release.wait()
+
+ def read() -> None:
+ loaded.append(V2Config.load())
+
+ holder = threading.Thread(target=writer_section)
+ holder.start()
+ reader = threading.Thread(target=read)
+ try:
+ assert held.wait(30)
+ reader.start()
+ # A hang guard only: the read waits on nothing while the lock stays held.
+ reader.join(30)
+ assert not reader.is_alive(), "a plain load waited for the config lock"
+ assert loaded[0].language == "en"
+ finally:
+ release.set()
+ holder.join()
+ if reader.is_alive():
+ reader.join()
+
+
+def test_a_load_racing_a_save_sees_the_old_file_or_the_new_one(isolated_config_home: Path) -> None:
+ """Writers publish config.json atomically, so a lock-free read never sees a partial file."""
+
+ # Large enough that an in-place rewrite would span many writes and reads.
+ padding = "x" * 500_000
+ versions = {"en": f"/old/{padding}", "zh": f"/new/{padding}"}
+
+ def write(language: str) -> None:
+ def mutate(cfg: V2Config) -> None:
+ cfg.language = language
+ cfg.runtime.default_cwd = versions[language]
+
+ update_config_fields(mutate)
+
+ write("en")
+ stop = threading.Event()
+ errors: list[BaseException] = []
+
+ def writer() -> None:
+ try:
+ for index in range(60):
+ write(("zh", "en")[index % 2])
+ except BaseException as exc:
+ errors.append(exc)
+ finally:
+ stop.set()
+
+ thread = threading.Thread(target=writer)
+ thread.start()
+ observed = 0
+ try:
+ while not stop.is_set() or observed == 0:
+ config = V2Config.load()
+ assert config.load_warnings == ()
+ assert config.runtime.default_cwd == versions[config.language]
+ observed += 1
+ finally:
+ stop.set()
+ thread.join()
+ assert not errors
+ assert not list(isolated_config_home.parent.glob(f"{isolated_config_home.name}.bak-*"))
+
+
+def test_pair_preclaim_migration_reads_config_under_the_sqlite_writer_without_the_config_lock(
+ isolated_config_home: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """pair()'s pre-claim migration holds the SQLite writer when it reads the config.
+
+ A reconcile in another thread holds config_file_lock and waits for that writer.
+ When the read also took CONFIG_LOCK, the two waited on each other until SQLite's
+ busy_timeout failed the reconcile with "database is locked".
+ """
+ from config.v2_config import config_file_lock
+ from storage import remote_access_authorization_service as authorization
+ from storage import resource_access_service
+ from vibe import remote_access
+
+ # Initialize state first so neither thread migrates the schema below.
+ remote_access._run_pending_deferred_context_migration()
+ holds_sqlite_writer = threading.Event()
+ original = resource_access_service._configured_resource_state
+
+ def configured_resource_state():
+ # Called after reserve_write_lock(): this thread owns the SQLite writer now.
+ holds_sqlite_writer.set()
+ return original()
+
+ monkeypatch.setattr(resource_access_service, "_configured_resource_state", configured_resource_state)
+ outcome: dict[str, object] = {}
+
+ def preclaim() -> None:
+ try:
+ outcome["preclaim"] = remote_access._run_pending_deferred_context_migration()
+ except BaseException as exc:
+ outcome["preclaim"] = exc
+
+ worker = threading.Thread(target=preclaim)
+ try:
+ with config_file_lock():
+ worker.start()
+ assert holds_sqlite_writer.wait(30)
+ result = authorization.reconcile_instance_binding(instance_id="inst_A", instance_kind="personal")
+ finally:
+ # After the config lock is released, so a pre-claim still waiting for it can finish.
+ worker.join()
+ assert result["ok"]
+ assert isinstance(outcome["preclaim"], dict)