The registry.py module#

Summary#

ServerEntry

One lane backend recorded in the registry.

Registry

In-memory view of the on-disk registry.

StopReport

Outcome of stop_entry().

ReapReport

Lanes removed and kept by reap_registry().

ResetReport

Summary of reset_registry().

RegistryLockTimeoutError

Raised when the registry lock cannot be acquired in time.

state_dir

Return the per-user state directory, creating it if needed.

registry_path

Return the path to the registry JSON file.

lockfile_path

Return the path to the registry lock file.

registry_lock

Hold the registry lock for the duration of the block.

force_unlock

Delete the registry lock file.

load_registry

Read the registry from disk.

save_registry

Write the registry to disk atomically.

is_port_open

Check whether a TCP connection to host:port succeeds.

pid_alive

Check whether a process exists and is not a zombie.

kill_pid

Kill a process and wait for it to exit.

kill_keeping_aedt

Kill a lane backend and its helpers but keep AEDT running.

stop_entry

Kill a lane backend and remove it from the registry.

reap_registry

Remove registry entries whose backend is no longer live.

reset_registry

Clear every registry entry.

allocate_http_port

Return the first port in range that is neither registered nor bound.

upsert_entry

Insert or replace one entry.

update_aedt_port

Set the AEDT gRPC port of one entry.

remove_entry

Remove one entry.

get_entry

Return one entry.

list_entries

Return every entry.

SCHEMA_VERSION

Version of the on-disk registry format.

DEFAULT_LOCK_TIMEOUT

Seconds to wait for the registry lock before giving up.

DEFAULT_HTTP_PORT_RANGE

Inclusive port range for lane backends. It avoids the default HTTP port 8080.

STATE_DIR_NAME

REGISTRY_FILENAME

LOCKFILE_NAME

AEDT_PROCESS_PREFIXES

Process name prefixes identifying AEDT, which must survive a lane recycle.

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 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.

Module detail#

registry.state_dir() → pathlib.Path#

Return the per-user state directory, creating it if needed.

Returns:
pathlib.Path

Directory holding the registry, its lock file, and the lane logs.

registry.registry_path() → pathlib.Path#

Return the path to the registry JSON file.

Returns:
pathlib.Path

Registry file path.

registry.lockfile_path() → pathlib.Path#

Return the path to the registry lock file.

Returns:
pathlib.Path

Lock file path.

registry.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:
timeoutfloat, optional

Seconds to wait for the lock. Defaults to DEFAULT_LOCK_TIMEOUT.

Raises:
RegistryLockTimeoutError

If the lock is not acquired within timeout seconds.

registry.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:
bool

True if a lock file was removed.

registry.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:
Registry

Current registry content.

registry.save_registry(reg: Registry) → None#

Write the registry to disk atomically.

The caller must hold registry_lock().

Parameters:
regRegistry

Registry to persist.

registry.is_port_open(host: str, port: int, timeout: float = 0.3) → bool#

Check whether a TCP connection to host:port succeeds.

Parameters:
hoststr

Host name or address.

portint

TCP port.

timeoutfloat, default: 0.3

Connection timeout in seconds.

Returns:
bool

True if something accepts connections on the port.

registry.pid_alive(pid: int) → bool#

Check whether a process exists and is not a zombie.

Parameters:
pidint

Process ID.

Returns:
bool

True if the process is running.

registry.kill_pid(pid: int, timeout: float = 10.0, tree: bool = True) → bool#

Kill a process and wait for it to exit.

Parameters:
pidint

Process ID.

timeoutfloat, default: 10.0

Seconds to wait for the processes to exit.

treebool, default: 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:
bool

True if pid is gone.

registry.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:
pidint

Process ID of the lane backend.

timeoutfloat, default: 10.0

Seconds to wait for each process to exit.

keep_namestuple[str, …], default: (“ansysedt”,)

Lower-case process name prefixes to keep alive.

Returns:
bool

True if the backend process is gone.

registry.stop_entry(server_id: str, timeout: float = 10.0) → StopReport | None#

Kill a lane backend and remove it from the registry.

Parameters:
server_idstr

Lane name.

timeoutfloat, default: 10.0

Seconds to wait for the backend to exit.

Returns:
StopReport or None

What was killed and removed, or None if the lane is not registered.

registry.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:
hoststr, default: “127.0.0.1”

Host the lanes listen on.

Returns:
ReapReport

Dropped and kept lane names.

registry.reset_registry(kill_orphans: bool = False, unlock: bool = False) → ResetReport#

Clear every registry entry.

Parameters:
kill_orphansbool, default: False

Whether to kill each lane process before clearing its entry.

unlockbool, default: False

Whether to delete the lock file first. Use only when the lock is stuck.

Returns:
ResetReport

What was dropped, killed, and unlocked.

registry.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 registry_lock(). The port is reserved only once an entry using it is saved.

Parameters:
regRegistry

Current registry.

hoststr, default: “127.0.0.1”

Host the lanes listen on.

port_rangetuple[int, int], default: (8090, 8189)

Inclusive range of candidate ports.

Returns:
int

Free port.

Raises:
RuntimeError

If every port in the range is taken.

registry.upsert_entry(entry: ServerEntry) → None#

Insert or replace one entry.

Parameters:
entryServerEntry

Entry to store.

registry.update_aedt_port(server_id: str, aedt_gpc_port: int | None) → bool#

Set the AEDT gRPC port of one entry.

Parameters:
server_idstr

Lane name.

aedt_gpc_portint, optional

New AEDT gRPC port, or None to clear it.

Returns:
bool

False if the entry no longer exists.

registry.remove_entry(server_id: str) → bool#

Remove one entry.

Parameters:
server_idstr

Lane name.

Returns:
bool

True if an entry was removed.

registry.get_entry(server_id: str) → ServerEntry | None#

Return one entry.

Parameters:
server_idstr

Lane name.

Returns:
ServerEntry or None

The entry, or None if it does not exist.

registry.list_entries() → list[ServerEntry]#

Return every entry.

Returns:
list[ServerEntry]

Registered lanes.

registry.SCHEMA_VERSION = 1#

Version of the on-disk registry format.

registry.DEFAULT_LOCK_TIMEOUT = 10.0#

Seconds to wait for the registry lock before giving up.

registry.DEFAULT_HTTP_PORT_RANGE = (8090, 8189)#

Inclusive port range for lane backends. It avoids the default HTTP port 8080.

registry.STATE_DIR_NAME = 'ansys-aedt-mcp'#
registry.REGISTRY_FILENAME = 'registry.json'#
registry.LOCKFILE_NAME = 'registry.lock'#
registry.AEDT_PROCESS_PREFIXES = ('ansysedt',)#

Process name prefixes identifying AEDT, which must survive a lane recycle.