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(show): release adopted install lock handles (#2009)

Call White9d6097b5251a
diff --git a/core/show_runtime.py b/core/show_runtime.py
index 5c77f39a4..f9966150b 100644
--- a/core/show_runtime.py
+++ b/core/show_runtime.py
@@ -3702,26 +3702,27 @@ class ShowRuntimeManager:
yield unavailable
return
file_lock = MigrationFileLock(self._install_guard_path, timeout_seconds=timeout_seconds)
- file_lock._handle = os.fdopen(lock_fd, "a+", encoding="utf-8")
+ handle = os.fdopen(lock_fd, "a+", encoding="utf-8")
deadline = (
None
if timeout_seconds is None
else time.monotonic() + timeout_seconds
)
while True:
- file_lock._handle.seek(0)
- if storage_lock_try_lock(file_lock._handle):
- file_lock._handle.seek(0)
- file_lock._handle.truncate()
- file_lock._handle.write(str(os.getpid()))
- file_lock._handle.flush()
+ handle.seek(0)
+ if storage_lock_try_lock(handle):
+ handle.seek(0)
+ handle.truncate()
+ handle.write(str(os.getpid()))
+ handle.flush()
+ file_lock.adopt_locked_handle(handle)
break
if deadline is not None and time.monotonic() >= deadline:
- file_lock._handle.close()
+ handle.close()
yield busy
return
time.sleep(0.1)
- if not self._guard_path_matches_fd(file_lock._handle.fileno()):
+ if not self._guard_path_matches_fd(handle.fileno()):
logger.warning(
"Show Runtime install guard path was replaced after lock acquisition; refusing",
)
diff --git a/storage/lock.py b/storage/lock.py
index ec309c573..5cb4a46e5 100644
--- a/storage/lock.py
+++ b/storage/lock.py
@@ -122,6 +122,23 @@ class MigrationFileLock:
self._key = _state_key(self.lock_path)
self._state: _PathLockState | None = None
self._entries = 0
+ # A small number of security-sensitive callers open and validate the
+ # descriptor themselves before taking the advisory lock. They still
+ # need MigrationFileLock to own the unlock/close lifecycle afterward.
+ self._adopted_handle: IO[str] | None = None
+
+ def adopt_locked_handle(self, handle: IO[str]) -> None:
+ """Transfer an already-locked descriptor into this lock's ownership.
+
+ The caller remains responsible for acquiring and validating the OS
+ lock. Once adopted, ``release()`` performs the matching unlock and
+ closes the descriptor, just like a normal ``acquire()`` path.
+ """
+ if self._state is not None or self._entries:
+ raise RuntimeError("cannot adopt a handle while the lock is acquired")
+ if self._adopted_handle is not None:
+ raise RuntimeError("a locked handle is already adopted")
+ self._adopted_handle = handle
def acquire(self) -> None:
deadline = None if self.timeout_seconds is None else time.monotonic() + self.timeout_seconds
@@ -152,6 +169,10 @@ class MigrationFileLock:
def release(self) -> None:
state = self._state
if state is None or self._entries == 0:
+ handle = self._adopted_handle
+ if handle is not None:
+ self._adopted_handle = None
+ _release_file_lock(handle)
return
self._entries -= 1
if self._entries == 0:
diff --git a/tests/test_migration_lock.py b/tests/test_migration_lock.py
index f3e836427..f4dd86817 100644
--- a/tests/test_migration_lock.py
+++ b/tests/test_migration_lock.py
@@ -18,7 +18,12 @@ from pathlib import Path
import pytest
-from storage.lock import MigrationFileLock, MigrationLockTimeout, migration_lock_path_for
+from storage.lock import (
+ MigrationFileLock,
+ MigrationLockTimeout,
+ _try_lock,
+ migration_lock_path_for,
+)
REPO_ROOT = Path(__file__).resolve().parents[1]
@@ -78,6 +83,20 @@ def test_re_entrance_belongs_to_the_path_not_to_one_lock_object(tmp_path: Path)
assert _free_for_another_thread(lock_path) is True
+def test_release_closes_an_adopted_locked_handle(tmp_path: Path) -> None:
+ lock_path = tmp_path / "migration.lock"
+ lock = MigrationFileLock(lock_path, timeout_seconds=0)
+ handle = open(lock_path, "a+", encoding="utf-8")
+ assert _try_lock(handle)
+ lock.adopt_locked_handle(handle)
+
+ assert _free_for_another_thread(lock_path) is False
+ lock.release()
+
+ assert handle.closed
+ assert _free_for_another_thread(lock_path) is True
+
+
def test_failed_reentrant_validation_preserves_the_outer_lock(tmp_path: Path) -> None:
lock_path = tmp_path / "migration.lock"
validation_allowed = True