fix(skills): reclaim idle projection lock entries (#8564)

Co-authored-by: zzj84862-creator <252897181+zzj84862-creator@users.noreply.github.com>
This commit is contained in:
zzj84862-creator 2026-11-24 23:31:10 +08:00 committed by GitHub
parent 8d6e774de6
commit 4223e0710c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 153 additions and 15 deletions

View File

@ -11,6 +11,7 @@ import shutil
import stat
import tempfile
import threading
import time
from collections.abc import Iterator
from contextlib import contextmanager
from dataclasses import dataclass
@ -33,8 +34,15 @@ except ImportError: # pragma: no cover - Windows
fcntl = None # type: ignore[assignment]
import msvcrt
@dataclass(slots=True)
class _ProcessLockEntry:
lock: threading.RLock
refcount: int = 0
_locks_guard = threading.Lock()
_process_locks: dict[Path, threading.RLock] = {}
_process_locks: dict[Path, _ProcessLockEntry] = {}
_held_projection_locks = threading.local()
_MANIFEST_VERSION = 1
_MAX_REBUILD_ATTEMPTS = 2
@ -98,28 +106,42 @@ def thread_skill_projection_exists(storage: SkillStorage, thread_id: str) -> boo
return paths.public.parent.exists()
def _lock_for(path: Path) -> threading.RLock:
resolved = path.resolve()
@contextmanager
def _hold_process_lock(key: Path, *, deadline: float | None, check=None) -> Iterator[None]:
with _locks_guard:
return _process_locks.setdefault(resolved, threading.RLock())
entry = _process_locks.get(key)
if entry is None:
entry = _ProcessLockEntry(threading.RLock())
_process_locks[key] = entry
# Count waiters before releasing the guard so they share one lock with later callers.
entry.refcount += 1
process_lock = entry.lock
try:
while not process_lock.acquire(timeout=0.05):
if check:
check()
if deadline is not None and time.monotonic() >= deadline:
raise TimeoutError("Skill projection lock timeout")
try:
yield
finally:
process_lock.release()
finally:
with _locks_guard:
entry.refcount -= 1
if entry.refcount == 0 and _process_locks.get(key) is entry:
_process_locks.pop(key)
@contextmanager
def _projection_lock(root: Path, *, timeout: float | None = None, check=None) -> Iterator[None]:
"""Serialize projection replacement in-process and across POSIX workers."""
import time
lock_path = root.parent / f".{root.name}.projection.lock"
lock_path.parent.mkdir(parents=True, exist_ok=True)
process_lock = _lock_for(lock_path)
key = lock_path.resolve()
deadline = None if timeout is None else time.monotonic() + timeout
while not process_lock.acquire(timeout=0.05):
if check:
check()
if deadline is not None and time.monotonic() >= deadline:
raise TimeoutError("Skill projection lock timeout")
try:
with _hold_process_lock(key, deadline=deadline, check=check):
held = getattr(_held_projection_locks, "paths", None)
if held is None:
held = _held_projection_locks.paths = set()
@ -156,8 +178,6 @@ def _projection_lock(root: Path, *, timeout: float | None = None, check=None) ->
else: # pragma: no cover - Windows
lock_file.seek(0)
msvcrt.locking(lock_file.fileno(), msvcrt.LK_UNLCK, 1)
finally:
process_lock.release()
def _copy_into_view(source: str, target: str, *, follow_symlinks: bool = True) -> str:

View File

@ -17,6 +17,7 @@ import pytest
from deerflow.config.extensions_config import ExtensionsConfig, SkillStateConfig
from deerflow.config.paths import Paths
from deerflow.sandbox.middleware import SandboxMiddleware
from deerflow.skills import projection
from deerflow.skills.projection import (
ensure_public_skill_projection,
ensure_skill_projections,
@ -1163,3 +1164,120 @@ def test_undecodable_skill_states_file_is_treated_as_empty(projection_env) -> No
state_file.write_bytes(b"\xff\xfe")
assert projection_env.storage._read_skill_states() == {}
@pytest.fixture
def projection_locks(monkeypatch):
locks = {}
monkeypatch.setattr(projection, "_process_locks", locks)
return locks
def test_completed_thread_projections_reclaim_idle_locks(projection_env, projection_locks):
for index in range(4):
paths = ensure_thread_skill_projection(projection_env.storage, f"thread-{index}", set())
assert paths is not None and paths.public.is_dir()
assert not projection_locks
def test_waiter_and_late_arrival_share_the_same_projection_lock(tmp_path, projection_locks):
root = tmp_path / "view"
waiting, retry_waiter, waiter_blocked_again = Event(), Event(), Event()
late_holding, release_late = Event(), Event()
def check_waiter():
if not waiting.is_set():
waiting.set()
assert retry_waiter.wait(5)
else:
waiter_blocked_again.set()
def waiter():
with projection._projection_lock(root, check=check_waiter):
assert not late_holding.is_set()
def late_arrival():
with projection._projection_lock(root):
late_holding.set()
try:
assert release_late.wait(5)
finally:
late_holding.clear()
with ThreadPoolExecutor(max_workers=2) as executor:
try:
with projection._projection_lock(root):
(entry,) = projection_locks.values()
queued = executor.submit(waiter)
assert waiting.wait(5)
late = executor.submit(late_arrival)
assert late_holding.wait(5)
(late_entry,) = projection_locks.values()
assert late_entry is entry
retry_waiter.set()
assert waiter_blocked_again.wait(5)
finally:
retry_waiter.set()
release_late.set()
queued.result(timeout=5)
late.result(timeout=5)
assert not projection_locks
def test_nested_projection_lock_keeps_entry_until_outer_exit(tmp_path, projection_locks):
root = tmp_path / "view"
with projection._projection_lock(root):
(entry,) = projection_locks.values()
with projection._projection_lock(root, timeout=0):
(nested_entry,) = projection_locks.values()
assert nested_entry is entry
(remaining_entry,) = projection_locks.values()
assert remaining_entry is entry
assert not projection_locks
@pytest.mark.parametrize("abort_wait", [False, True], ids=["timeout", "check-error"])
def test_projection_lock_reclaims_entry_after_failed_wait(tmp_path, projection_locks, abort_wait):
root = tmp_path / "view"
checked = Event()
def check():
checked.set()
if abort_wait:
raise RuntimeError("stop waiting")
def waiter():
with projection._projection_lock(root, timeout=0, check=check):
pytest.fail("waiter entered while the lock was held")
with ThreadPoolExecutor(max_workers=1) as executor:
with projection._projection_lock(root):
future = executor.submit(waiter)
expected = RuntimeError if abort_wait else TimeoutError
with pytest.raises(expected, match="stop waiting" if abort_wait else "Skill projection lock timeout"):
future.result(timeout=5)
assert checked.is_set()
assert not projection_locks
def test_projection_lock_reclaims_entry_when_body_raises(tmp_path, projection_locks):
with pytest.raises(RuntimeError, match="projection failed"):
with projection._projection_lock(tmp_path / "view"):
raise RuntimeError("projection failed")
assert not projection_locks
def test_distinct_projection_paths_can_hold_locks_in_parallel(tmp_path, projection_locks):
both_holding = Barrier(2, timeout=5)
def acquire(root):
with projection._projection_lock(root):
both_holding.wait()
with ThreadPoolExecutor(max_workers=2) as executor:
first = executor.submit(acquire, tmp_path / "first")
second = executor.submit(acquire, tmp_path / "second")
first.result(timeout=10)
second.result(timeout=10)
assert not projection_locks