"""Serialize configuration access across REST, MCP and recovery jobs.""" import asyncio command_lock = asyncio.Lock() class ConfigurationMiddleware: def __init__(self, app): self.app = app async def __call__(self, scope, receive, send): path = scope.get("path", "") # Status/login remain responsive while a datapath is being prepared. Socket.IO # is also independent: it must be able to deliver invalidations during a write. guarded = path.startswith(("/api/services", "/api/firewall")) or path in { "/api/import", "/api/export", "/api/reset", "/api/change-password", "/api/auth-mode", } if scope["type"] == "http" and guarded: async with command_lock: await self.app(scope, receive, send) else: await self.app(scope, receive, send)