Lifetime ownership for conductor Unix listeners.
A persistent adjacent lock serializes Marianne owners without blocking the event
loop. Foreign live listeners are preserved, and cleanup checks the exact inode
held from bind through shutdown. The parent directory must remain trusted: this
is not a sandbox against hostile directory or lock-file replacement.
Classes
OwnedUnixSocket
Bind once under a nonblocking lifetime lock; release only our inode.
Source code in src/marianne/daemon/socket_ownership.py
| def __init__(self, path: Path) -> None:
self.path = Path(os.path.abspath(path))
self._lock_fd: int | None = None
self._inode_pin: _PinnedSocketInode | None = None
self._identity: tuple[int, int] | None = None
self._socket: socket.socket | None = None
|
Methods:
prepare
async
Claim and bind before launching a dependent upstream process.
Source code in src/marianne/daemon/socket_ownership.py
| async def prepare(self) -> None:
"""Claim and bind before launching a dependent upstream process."""
if self._lock_fd is not None:
return
self.path.parent.mkdir(parents=True, exist_ok=True)
lock_path = self.path.with_name(self.path.name + ".lock")
fd = os.open(lock_path, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)
try:
info = os.fstat(fd)
if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
raise OSError(
errno.EINVAL, "Socket ownership lock must be a regular file", str(lock_path)
)
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
current = lock_path.lstat()
if (current.st_dev, current.st_ino) != (info.st_dev, info.st_ino):
raise OSError(errno.EADDRINUSE, "Socket ownership lock changed", str(lock_path))
except BaseException:
os.close(fd)
raise
self._lock_fd = fd
try:
await self._remove_stale_socket()
bound = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self._socket = bound
bound.setblocking(False)
bound.bind(str(self.path))
# Socket FDs identify the kernel socket, not its filesystem inode.
self._inode_pin = _PinnedSocketInode(self.path)
info = self._inode_pin.info
if not stat.S_ISSOCK(info.st_mode):
raise OSError(
errno.EADDRINUSE, "Socket pathname changed while binding", str(self.path)
)
self._identity = (info.st_dev, info.st_ino)
except BaseException:
self.release()
raise
|
start
async
start(callback, *, permissions=None, backlog=100, **kwargs)
Start accepting using our bound socket, avoiding asyncio's unlink.
Source code in src/marianne/daemon/socket_ownership.py
| async def start(
self, callback: Any, *, permissions: int | None = None, backlog: int = 100, **kwargs: Any
) -> asyncio.AbstractServer:
"""Start accepting using our bound socket, avoiding asyncio's unlink."""
await self.prepare()
try:
assert self._socket is not None
if permissions is not None:
os.chmod(self.path, permissions)
server = await asyncio.start_unix_server(
callback, sock=self._socket, backlog=backlog, **kwargs
)
self._socket = None # asyncio now owns and closes the socket FD.
return server
except BaseException:
self.release()
raise
|
release
After closing the server, remove our pathname and release the lock.
Source code in src/marianne/daemon/socket_ownership.py
| def release(self) -> None:
"""After closing the server, remove our pathname and release the lock."""
try:
if self._socket is not None:
self._socket.close()
self._socket = None
if self._identity is not None:
try:
current = self.path.lstat()
except FileNotFoundError:
pass
else:
if (current.st_dev, current.st_ino) == self._identity:
self.path.unlink()
finally:
self._identity = None
if self._inode_pin is not None:
self._inode_pin.close()
self._inode_pin = None
if self._lock_fd is not None:
os.close(self._lock_fd)
self._lock_fd = None
|