From 98a20bfaaba90fa00de01d4a825ffdc260cd94f6 Mon Sep 17 00:00:00 2001 From: mosaic-coder Date: Sat, 25 Jul 2026 16:47:42 -0500 Subject: [PATCH] fix(lease-broker): wait_ready() polls real connect-readiness not socket-file existence (flaky CI race) setUp() spawned the daemon and polled only for the unix-socket file to exist before proceeding to connect(). Under concurrent CI-runner load the socket file can appear (created by bind()) before the daemon calls listen(), so the client's immediate connect() raced ahead and hit ConnectionRefusedError: [Errno 111], erroring the whole suite in setUp(). wait_ready() now attempts a real socket.connect() in a retry loop (catching ConnectionRefusedError/FileNotFoundError) until a connection actually succeeds (then closes it) or the same 5s deadline elapses, at which point it fails exactly as before. Readiness is now "the daemon is actually accepting connections," not "a file happens to exist," eliminating the TOCTOU race. Scope: only wait_ready() in this file changed. Zero assertion changes, zero daemon/lease-broker source changes, zero gate-timeout relaxation. Closes #897 --- .../src/lease-broker/recovery_b1_adversarial_unittest.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/mosaic/src/lease-broker/recovery_b1_adversarial_unittest.py b/packages/mosaic/src/lease-broker/recovery_b1_adversarial_unittest.py index 3df91ec7..ce388ef7 100644 --- a/packages/mosaic/src/lease-broker/recovery_b1_adversarial_unittest.py +++ b/packages/mosaic/src/lease-broker/recovery_b1_adversarial_unittest.py @@ -51,8 +51,13 @@ def request(socket_path: Path, value: dict[str, object]) -> dict[str, object]: def wait_ready(process: subprocess.Popen[str], socket_path: Path) -> None: deadline = time.monotonic() + 5.0 while time.monotonic() < deadline: - if socket_path.exists(): + try: + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as probe: + probe.settimeout(0.25) + probe.connect(str(socket_path)) return + except (ConnectionRefusedError, FileNotFoundError): + pass if process.poll() is not None: output = process.stdout.read() if process.stdout is not None else "" raise RuntimeError(f"daemon exited before READY: {output}")