The ``registry.py`` module
==========================
.. py:module:: ansys.aedt.mcp.lanes.registry
Summary
-------
.. py:currentmodule:: registry
.. tab-set::
.. tab-item:: Classes
.. list-table::
:header-rows: 0
:widths: auto
* - :py:obj:`~ansys.aedt.mcp.lanes.registry.ServerEntry`
- One lane backend recorded in the registry.
* - :py:obj:`~ansys.aedt.mcp.lanes.registry.Registry`
- In-memory view of the on-disk registry.
* - :py:obj:`~ansys.aedt.mcp.lanes.registry.StopReport`
- Outcome of :func:`stop_entry`.
* - :py:obj:`~ansys.aedt.mcp.lanes.registry.ReapReport`
- Lanes removed and kept by :func:`reap_registry`.
* - :py:obj:`~ansys.aedt.mcp.lanes.registry.ResetReport`
- Summary of :func:`reset_registry`.
.. tab-item:: Exceptions
.. list-table::
:header-rows: 0
:widths: auto
* - :py:obj:`~ansys.aedt.mcp.lanes.registry.RegistryLockTimeoutError`
- Raised when the registry lock cannot be acquired in time.
.. tab-item:: Functions
.. list-table::
:header-rows: 0
:widths: auto
* - :py:obj:`~state_dir`
- Return the per-user state directory, creating it if needed.
* - :py:obj:`~registry_path`
- Return the path to the registry JSON file.
* - :py:obj:`~lockfile_path`
- Return the path to the registry lock file.
* - :py:obj:`~registry_lock`
- Hold the registry lock for the duration of the block.
* - :py:obj:`~force_unlock`
- Delete the registry lock file.
* - :py:obj:`~load_registry`
- Read the registry from disk.
* - :py:obj:`~save_registry`
- Write the registry to disk atomically.
* - :py:obj:`~is_port_open`
- Check whether a TCP connection to ``host:port`` succeeds.
* - :py:obj:`~pid_alive`
- Check whether a process exists and is not a zombie.
* - :py:obj:`~kill_pid`
- Kill a process and wait for it to exit.
* - :py:obj:`~kill_keeping_aedt`
- Kill a lane backend and its helpers but keep AEDT running.
* - :py:obj:`~stop_entry`
- Kill a lane backend and remove it from the registry.
* - :py:obj:`~reap_registry`
- Remove registry entries whose backend is no longer live.
* - :py:obj:`~reset_registry`
- Clear every registry entry.
* - :py:obj:`~allocate_http_port`
- Return the first port in range that is neither registered nor bound.
* - :py:obj:`~upsert_entry`
- Insert or replace one entry.
* - :py:obj:`~update_aedt_port`
- Set the AEDT gRPC port of one entry.
* - :py:obj:`~remove_entry`
- Remove one entry.
* - :py:obj:`~get_entry`
- Return one entry.
* - :py:obj:`~list_entries`
- Return every entry.
.. tab-item:: Constants
.. list-table::
:header-rows: 0
:widths: auto
* - :py:obj:`~SCHEMA_VERSION`
- Version of the on-disk registry format.
* - :py:obj:`~DEFAULT_LOCK_TIMEOUT`
- Seconds to wait for the registry lock before giving up.
* - :py:obj:`~DEFAULT_HTTP_PORT_RANGE`
- Inclusive port range for lane backends. It avoids the default HTTP port 8080.
* - :py:obj:`~STATE_DIR_NAME`
-
* - :py:obj:`~REGISTRY_FILENAME`
-
* - :py:obj:`~LOCKFILE_NAME`
-
* - :py:obj:`~AEDT_PROCESS_PREFIXES`
- Process name prefixes identifying AEDT, which must survive a lane recycle.
.. toctree::
:titlesonly:
:maxdepth: 1
:hidden:
ServerEntry
Registry
StopReport
ReapReport
ResetReport
.. toctree::
:titlesonly:
:maxdepth: 1
:hidden:
RegistryLockTimeoutError
Description
-----------
File-backed registry of lane backends.
A *lane* is a long-lived ``ansys-aedt-mcp`` process serving the HTTP
transport on a loopback port. Each lane owns at most one AEDT session. The
registry records every running lane so that several MCP clients (IDE
windows, agents) can find and reuse the same backends.
The registry lives in a per-user state directory (``%LOCALAPPDATA%`` on
Windows, ``$XDG_STATE_HOME`` on POSIX) and is guarded by a bounded-timeout
:class:`filelock.FileLock`. A corrupt file is moved aside and replaced by an
empty registry, so every function here is safe to call from a recovery path.
..
!! processed by numpydoc !!
Module detail
-------------
.. py:function:: state_dir() -> pathlib.Path
Return the per-user state directory, creating it if needed.
:Returns:
:obj:`pathlib.Path`
Directory holding the registry, its lock file, and the lane logs.
..
!! processed by numpydoc !!
.. py:function:: registry_path() -> pathlib.Path
Return the path to the registry JSON file.
:Returns:
:obj:`pathlib.Path`
Registry file path.
..
!! processed by numpydoc !!
.. py:function:: lockfile_path() -> pathlib.Path
Return the path to the registry lock file.
:Returns:
:obj:`pathlib.Path`
Lock file path.
..
!! processed by numpydoc !!
.. py:function:: registry_lock(timeout: float | None = None) -> collections.abc.Iterator[None]
Hold the registry lock for the duration of the block.
The OS releases the lock when its holder dies, even if the lock file
stays on disk.
:Parameters:
**timeout** : :class:`python:float`, :obj:`optional`
Seconds to wait for the lock. Defaults to ``DEFAULT_LOCK_TIMEOUT``.
:Raises:
:obj:`RegistryLockTimeoutError`
If the lock is not acquired within ``timeout`` seconds.
..
!! processed by numpydoc !!
.. py:function:: force_unlock() -> bool
Delete the registry lock file.
Use only when the lock is stuck and no legitimate holder exists. The
registry file itself is left untouched.
:Returns:
:ref:`bool `
``True`` if a lock file was removed.
..
!! processed by numpydoc !!
.. py:function:: load_registry() -> Registry
Read the registry from disk.
A missing file yields an empty registry. A corrupt file is backed up and
replaced by an empty registry.
:Returns:
:obj:`Registry`
Current registry content.
..
!! processed by numpydoc !!
.. py:function:: save_registry(reg: Registry) -> None
Write the registry to disk atomically.
The caller must hold :func:`registry_lock`.
:Parameters:
**reg** : :obj:`Registry`
Registry to persist.
..
!! processed by numpydoc !!
.. py:function:: is_port_open(host: str, port: int, timeout: float = 0.3) -> bool
Check whether a TCP connection to ``host:port`` succeeds.
:Parameters:
**host** : :class:`python:str`
Host name or address.
**port** : :class:`python:int`
TCP port.
**timeout** : :class:`python:float`, default: 0.3
Connection timeout in seconds.
:Returns:
:ref:`bool `
``True`` if something accepts connections on the port.
..
!! processed by numpydoc !!
.. py:function:: pid_alive(pid: int) -> bool
Check whether a process exists and is not a zombie.
:Parameters:
**pid** : :class:`python:int`
Process ID.
:Returns:
:ref:`bool `
``True`` if the process is running.
..
!! processed by numpydoc !!
.. py:function:: kill_pid(pid: int, timeout: float = 10.0, tree: bool = True) -> bool
Kill a process and wait for it to exit.
:Parameters:
**pid** : :class:`python:int`
Process ID.
**timeout** : :class:`python:float`, default: 10.0
Seconds to wait for the processes to exit.
**tree** : :ref:`bool `, default: :data:`python:True`
Whether to kill the descendants too. With ``False`` only ``pid`` is
killed, so neither its children (such as an AEDT it launched) nor its
exit handlers run.
:Returns:
:ref:`bool `
``True`` if ``pid`` is gone.
..
!! processed by numpydoc !!
.. py:function:: kill_keeping_aedt(pid: int, timeout: float = 10.0, keep_names: tuple[str, ...] = AEDT_PROCESS_PREFIXES) -> bool
Kill a lane backend and its helpers but keep AEDT running.
AEDT processes and their children are spared. Every other process is
killed alone and abruptly, so no exit handler can close AEDT.
:Parameters:
**pid** : :class:`python:int`
Process ID of the lane backend.
**timeout** : :class:`python:float`, default: 10.0
Seconds to wait for each process to exit.
**keep_names** : :class:`python:tuple`\[:class:`python:str`, ...], default: ("ansysedt",)
Lower-case process name prefixes to keep alive.
:Returns:
:ref:`bool `
``True`` if the backend process is gone.
..
!! processed by numpydoc !!
.. py:function:: stop_entry(server_id: str, timeout: float = 10.0) -> StopReport | None
Kill a lane backend and remove it from the registry.
:Parameters:
**server_id** : :class:`python:str`
Lane name.
**timeout** : :class:`python:float`, default: 10.0
Seconds to wait for the backend to exit.
:Returns:
:obj:`StopReport` or :data:`python:None`
What was killed and removed, or ``None`` if the lane is not registered.
..
!! processed by numpydoc !!
.. py:function:: reap_registry(host: str = '127.0.0.1') -> ReapReport
Remove registry entries whose backend is no longer live.
A lane is live when its process exists and its HTTP port accepts
connections.
:Parameters:
**host** : :class:`python:str`, default: "127.0.0.1"
Host the lanes listen on.
:Returns:
:obj:`ReapReport`
Dropped and kept lane names.
..
!! processed by numpydoc !!
.. py:function:: reset_registry(kill_orphans: bool = False, unlock: bool = False) -> ResetReport
Clear every registry entry.
:Parameters:
**kill_orphans** : :ref:`bool `, default: :data:`python:False`
Whether to kill each lane process before clearing its entry.
**unlock** : :ref:`bool `, default: :data:`python:False`
Whether to delete the lock file first. Use only when the lock is stuck.
:Returns:
:obj:`ResetReport`
What was dropped, killed, and unlocked.
..
!! processed by numpydoc !!
.. py:function:: allocate_http_port(reg: Registry, host: str = '127.0.0.1', port_range: tuple[int, int] = DEFAULT_HTTP_PORT_RANGE) -> int
Return the first port in range that is neither registered nor bound.
The caller must hold :func:`registry_lock`. The port is reserved only
once an entry using it is saved.
:Parameters:
**reg** : :obj:`Registry`
Current registry.
**host** : :class:`python:str`, default: "127.0.0.1"
Host the lanes listen on.
**port_range** : :class:`python:tuple`\[:class:`python:int`, :class:`python:int`], default: (8090, 8189)
Inclusive range of candidate ports.
:Returns:
:class:`python:int`
Free port.
:Raises:
:obj:`RuntimeError`
If every port in the range is taken.
..
!! processed by numpydoc !!
.. py:function:: upsert_entry(entry: ServerEntry) -> None
Insert or replace one entry.
:Parameters:
**entry** : :obj:`ServerEntry`
Entry to store.
..
!! processed by numpydoc !!
.. py:function:: update_aedt_port(server_id: str, aedt_gpc_port: int | None) -> bool
Set the AEDT gRPC port of one entry.
:Parameters:
**server_id** : :class:`python:str`
Lane name.
**aedt_gpc_port** : :class:`python:int`, :obj:`optional`
New AEDT gRPC port, or ``None`` to clear it.
:Returns:
:ref:`bool `
``False`` if the entry no longer exists.
..
!! processed by numpydoc !!
.. py:function:: remove_entry(server_id: str) -> bool
Remove one entry.
:Parameters:
**server_id** : :class:`python:str`
Lane name.
:Returns:
:ref:`bool `
``True`` if an entry was removed.
..
!! processed by numpydoc !!
.. py:function:: get_entry(server_id: str) -> ServerEntry | None
Return one entry.
:Parameters:
**server_id** : :class:`python:str`
Lane name.
:Returns:
:obj:`ServerEntry` or :data:`python:None`
The entry, or ``None`` if it does not exist.
..
!! processed by numpydoc !!
.. py:function:: list_entries() -> list[ServerEntry]
Return every entry.
:Returns:
:class:`python:list`\[:obj:`ServerEntry`]
Registered lanes.
..
!! processed by numpydoc !!
.. py:data:: SCHEMA_VERSION
:value: 1
Version of the on-disk registry format.
..
!! processed by numpydoc !!
.. py:data:: DEFAULT_LOCK_TIMEOUT
:value: 10.0
Seconds to wait for the registry lock before giving up.
..
!! processed by numpydoc !!
.. py:data:: DEFAULT_HTTP_PORT_RANGE
:value: (8090, 8189)
Inclusive port range for lane backends. It avoids the default HTTP port 8080.
..
!! processed by numpydoc !!
.. py:data:: STATE_DIR_NAME
:value: 'ansys-aedt-mcp'
.. py:data:: REGISTRY_FILENAME
:value: 'registry.json'
.. py:data:: LOCKFILE_NAME
:value: 'registry.lock'
.. py:data:: AEDT_PROCESS_PREFIXES
:value: ('ansysedt',)
Process name prefixes identifying AEDT, which must survive a lane recycle.
..
!! processed by numpydoc !!