Skip to content

Root lock

root_lock

Process-wide serialization of scaffold writes by canonical target root.

Every scaffold kind writing into a project root shares this single registry, so a learning-profile write and a protocol-profile write aimed at the same root exclude each other instead of each holding its own private lock.

target_root_lock(root)

Hold the process-wide lock guarding writes on the canonical root.

Parameters:

Name Type Description Default
root Path

The target root, resolved before lookup so that distinct spellings of one directory share a single lock.

required

Yields:

Type Description
None

None, while the caller owns the root exclusively.

Source code in packages/axm-init/src/axm_init/core/root_lock.py
Python
@contextmanager
def target_root_lock(root: Path) -> Iterator[None]:
    """Hold the process-wide lock guarding writes on the canonical *root*.

    Args:
        root: The target root, resolved before lookup so that distinct
            spellings of one directory share a single lock.

    Yields:
        ``None``, while the caller owns the root exclusively.
    """
    canonical_root = root.resolve()
    with _ROOT_LOCKS_GUARD:
        entry = _ROOT_LOCKS.get(canonical_root)
        if entry is None:
            entry = _RootLockEntry(lock=_thread.RLock())
            _ROOT_LOCKS[canonical_root] = entry
        entry.users += 1
    entry.lock.acquire()
    try:
        yield
    finally:
        entry.lock.release()
        with _ROOT_LOCKS_GUARD:
            entry.users -= 1
            if entry.users == 0:
                _ROOT_LOCKS.pop(canonical_root, None)