Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

test(daemon_ipc): pin the Windows startup interleaving instead of polling for it

Martin Vogel5f2c1468663f
diff --git a/src/daemon/ipc.c b/src/daemon/ipc.c
index 2b0d729c8..0c1ae652c 100644
--- a/src/daemon/ipc.c
+++ b/src/daemon/ipc.c
@@ -114,6 +114,30 @@ void cbm_daemon_ipc_windows_legacy_guard_release_failures_set_for_test(unsigned
memory_order_release);
}
+#ifdef _WIN32
+static cbm_daemon_ipc_startup_gate_fn g_startup_gate_for_test;
+static void *g_startup_gate_context_for_test;
+#endif
+
+void cbm_daemon_ipc_startup_gate_set_for_test(cbm_daemon_ipc_startup_gate_fn gate, void *context) {
+#ifdef _WIN32
+ g_startup_gate_context_for_test = context;
+ g_startup_gate_for_test = gate;
+#else
+ (void)gate;
+ (void)context;
+#endif
+}
+
+#ifdef _WIN32
+static void ipc_startup_gate_run(void) {
+ cbm_daemon_ipc_startup_gate_fn gate = g_startup_gate_for_test;
+ if (gate) {
+ gate(g_startup_gate_context_for_test);
+ }
+}
+#endif
+
bool cbm_daemon_ipc_windows_legacy_names(const char *canonical_runtime_parent,
const char *instance_key,
char pipe_out[CBM_DAEMON_IPC_WINDOWS_NAME_CAP],
@@ -5755,6 +5779,9 @@ int cbm_daemon_ipc_startup_lock_try_acquire(const cbm_daemon_ipc_endpoint_t *end
lock->legacy_guard = legacy_guard;
lock->legacy_sentinel = INVALID_HANDLE_VALUE;
*lock_out = lock;
+ /* The lock is held and the handoff has not run yet: the one point where a
+ * test can pin this interleaving deterministically. No-op in production. */
+ ipc_startup_gate_run();
return 1;
}
diff --git a/src/daemon/ipc_internal.h b/src/daemon/ipc_internal.h
index 4b358ef16..b791a6b02 100644
--- a/src/daemon/ipc_internal.h
+++ b/src/daemon/ipc_internal.h
@@ -96,4 +96,13 @@ void cbm_daemon_ipc_posix_publication_hook_set_for_test(
cbm_daemon_ipc_posix_publication_hook_fn hook, void *context);
void cbm_daemon_ipc_windows_legacy_guard_release_failures_set_for_test(unsigned int count);
+/* Deterministic-interleaving seam: fires on the Windows startup path once the
+ * startup lock is held, before the rendezvous handoff. A test parks here to
+ * pin the "startup is holding the lock" interleaving by construction. Polling
+ * for that state instead is a race — the whole acquire → handoff → release
+ * sequence completes in microseconds whenever the handoff does not block, so
+ * the observation window has no lower bound. */
+typedef void (*cbm_daemon_ipc_startup_gate_fn)(void *context);
+void cbm_daemon_ipc_startup_gate_set_for_test(cbm_daemon_ipc_startup_gate_fn gate, void *context);
+
#endif /* CBM_DAEMON_IPC_INTERNAL_H */
diff --git a/tests/test_daemon_ipc.c b/tests/test_daemon_ipc.c
index b9cd6b95f..fae5e4fe3 100644
--- a/tests/test_daemon_ipc.c
+++ b/tests/test_daemon_ipc.c
@@ -399,6 +399,36 @@ typedef struct {
int result;
} ipc_test_win_startup_call_t;
+typedef struct {
+ atomic_bool acquired;
+ atomic_bool may_proceed;
+} ipc_test_startup_gate_t;
+
+/* Runs on the startup thread INSIDE cbm_daemon_ipc_startup_lock_try_acquire,
+ * with the startup lock held and the rendezvous handoff not yet attempted.
+ * Announce that state, then park until the test has released its reader. This
+ * pins the interleaving by construction; the previous version polled for the
+ * lock being busy, which is unobservable whenever the handoff does not block. */
+static void ipc_test_startup_gate(void *context) {
+ ipc_test_startup_gate_t *gate = context;
+ atomic_store(&gate->acquired, true);
+ while (!atomic_load(&gate->may_proceed)) {
+ cbm_usleep(200);
+ }
+}
+
+static bool ipc_test_startup_gate_wait_acquired(ipc_test_startup_gate_t *gate) {
+ /* `acquired` never clears, so this wait cannot miss the event; the bound
+ * only catches a thread that never started at all. */
+ for (size_t attempt = 0; attempt < 50000U; attempt++) {
+ if (atomic_load(&gate->acquired)) {
+ return true;
+ }
+ cbm_usleep(200);
+ }
+ return false;
+}
+
static void *ipc_test_win_startup_call(void *opaque) {
ipc_test_win_startup_call_t *call = opaque;
cbm_daemon_ipc_startup_lock_t *startup = NULL;
@@ -416,14 +446,6 @@ static void ipc_test_win_lock_release(cbm_private_file_lock_t **lock_io) {
cbm_usleep(1000);
}
}
-
-static bool ipc_test_win_lock_busy(cbm_private_lock_directory_t *directory, const char *base_name) {
- cbm_private_file_lock_t *probe = NULL;
- cbm_private_file_lock_status_t status =
- cbm_private_file_lock_try_acquire(directory, base_name, CBM_PRIVATE_FILE_LOCK_SH, &probe);
- ipc_test_win_lock_release(&probe);
- return status == CBM_PRIVATE_FILE_LOCK_BUSY;
-}
#endif
typedef struct {
@@ -1028,6 +1050,10 @@ TEST(daemon_ipc_windows_startup_retries_transient_rendezvous_reader) {
bool startup_observed = false;
int join_status = -1;
ipc_test_win_startup_call_t call = {.result = -1};
+ ipc_test_startup_gate_t gate;
+ atomic_init(&gate.acquired, false);
+ atomic_init(&gate.may_proceed, false);
+ cbm_daemon_ipc_startup_gate_set_for_test(ipc_test_startup_gate, &gate);
bool parent_ok = ipc_test_parent_new(parent, "win-record-reader");
if (parent_ok) {
@@ -1048,14 +1074,11 @@ TEST(daemon_ipc_windows_startup_retries_transient_rendezvous_reader) {
if (record_status == CBM_PRIVATE_FILE_LOCK_OK) {
thread_started = cbm_thread_create(&thread, 0, ipc_test_win_startup_call, &call) == 0;
}
- for (size_t attempt = 0; thread_started && attempt < 200U; attempt++) {
- if (ipc_test_win_lock_busy(directory, "cbm-startup-v2.lock")) {
- startup_observed = true;
- break;
- }
- cbm_usleep(1000);
+ if (thread_started) {
+ startup_observed = ipc_test_startup_gate_wait_acquired(&gate);
}
ipc_test_win_lock_release(&record_reader);
+ atomic_store(&gate.may_proceed, true);
if (thread_started) {
join_status = cbm_thread_join(&thread);
}
@@ -1063,6 +1086,7 @@ TEST(daemon_ipc_windows_startup_retries_transient_rendezvous_reader) {
ipc_test_copy_path(address, cbm_daemon_ipc_endpoint_address(endpoint));
}
+ cbm_daemon_ipc_startup_gate_set_for_test(NULL, NULL);
ipc_test_win_lock_release(&record_reader);
cbm_private_lock_directory_close(directory);
cbm_daemon_ipc_endpoint_free(endpoint);
@@ -1115,6 +1139,11 @@ TEST(daemon_ipc_windows_rendezvous_bridges_concurrent_lifetime_owner) {
cbm_daemon_ipc_startup_lock_release(&initial_startup);
initial_startup = NULL;
+ ipc_test_startup_gate_t gate;
+ atomic_init(&gate.acquired, false);
+ atomic_init(&gate.may_proceed, false);
+ cbm_daemon_ipc_startup_gate_set_for_test(ipc_test_startup_gate, &gate);
+
cbm_private_file_lock_status_t directory_status =
endpoint ? cbm_daemon_ipc_private_lock_directory_new(endpoint, &directory)
: CBM_PRIVATE_FILE_LOCK_IO;
@@ -1126,19 +1155,18 @@ TEST(daemon_ipc_windows_rendezvous_bridges_concurrent_lifetime_owner) {
if (record_status == CBM_PRIVATE_FILE_LOCK_OK) {
thread_started = cbm_thread_create(&thread, 0, ipc_test_win_startup_call, &call) == 0;
}
- for (size_t attempt = 0; thread_started && attempt < 200U; attempt++) {
- if (ipc_test_win_lock_busy(directory, "cbm-startup-v2.lock")) {
- startup_observed = true;
- break;
- }
- cbm_usleep(1000);
+ if (thread_started) {
+ startup_observed = ipc_test_startup_gate_wait_acquired(&gate);
}
+ /* Claim the lifetime lock while startup is parked in the gate, so the
+ * concurrent-owner bridge under test is exercised deterministically. */
cbm_private_file_lock_status_t lifetime_status =
startup_observed
? cbm_private_file_lock_try_acquire(directory, "cbm-lifetime.lock",
CBM_PRIVATE_FILE_LOCK_EX, &lifetime_owner)
: CBM_PRIVATE_FILE_LOCK_IO;
ipc_test_win_lock_release(&record_reader);
+ atomic_store(&gate.may_proceed, true);
if (thread_started) {
join_status = cbm_thread_join(&thread);
}
@@ -1147,6 +1175,7 @@ TEST(daemon_ipc_windows_rendezvous_bridges_concurrent_lifetime_owner) {
}
ipc_test_win_lock_release(&lifetime_owner);
+ cbm_daemon_ipc_startup_gate_set_for_test(NULL, NULL);
ipc_test_win_lock_release(&record_reader);
ipc_test_win_lock_release(&lifetime_owner);
cbm_private_lock_directory_close(directory);
@@ -2072,6 +2101,13 @@ typedef struct {
int receive_result;
} ipc_forever_wait_server_t;
+/* The connection is deliberately NOT closed here. The test interrupts this
+ * thread's wait through server->connection, and `receive_frame` can also return
+ * on its own (a torn connection, an early frame) before that call lands.
+ * Closing from this thread therefore freed the connection while the test was
+ * still dereferencing the pointer it had already loaded — a use-after-free that
+ * crashed roughly one Windows run in five. Ownership stays with the test, which
+ * closes it after joining, so `interrupt` can only ever see a live connection. */
static void *ipc_forever_wait_server(void *opaque) {
ipc_forever_wait_server_t *server = (ipc_forever_wait_server_t *)opaque;
cbm_daemon_frame_t frame = {0};
@@ -2084,8 +2120,6 @@ static void *ipc_forever_wait_server(void *opaque) {
server->connection, CBM_DAEMON_IPC_WAIT_FOREVER, &frame, &payload);
}
free(payload);
- cbm_daemon_ipc_connection_close(server->connection);
- server->connection = NULL;
return NULL;
}
@@ -2129,6 +2163,9 @@ TEST(daemon_ipc_wait_forever_is_interruptible) {
if (thread_started) {
join_result = cbm_thread_join(&thread);
}
+ /* Safe only after the join: the server thread no longer touches it. */
+ cbm_daemon_ipc_connection_close(server.connection);
+ server.connection = NULL;
cbm_daemon_ipc_connection_close(client);
cbm_daemon_ipc_listener_close(listener);
cbm_daemon_ipc_endpoint_free(endpoint);