Skip to content

Commit 5f2c146

Browse files
committed
test(daemon_ipc): pin the Windows startup interleaving instead of polling for it
Two Windows tests needed the startup thread to be observed HOLDING the startup lock, and looked for it by polling the lock for BUSY up to 200 times. That state has no lower bound: whenever the rendezvous handoff does not block, acquire -> handoff -> release completes in microseconds, so the window can close between two polls and startup_observed asserts on a coin flip. This blocked release run 30374135923 on test-windows (windows-2025, CLANG64, x86_64). Budget-tuning the poll was tried and rejected -- a wider budget only lowers the failure rate, and a transient window is never a fixed test. Instead the production path exposes a gate that fires once the startup lock is held and before the handoff runs, matching the hook pattern already used for POSIX publication. The test parks the startup thread there and releases it when it is ready, so the interleaving is pinned by construction. The acquired flag is monotonic, so the observation cannot be missed; the remaining bound only catches a thread that never started at all. The gate is inert unless a test installs it. Also fixes an unrelated use-after-free this exposed. ipc_forever_wait_server closed the connection and NULLed it from the server thread, while the test was about to call cbm_daemon_ipc_connection_interrupt on the pointer it had already loaded -- and receive_frame can return on its own before that call lands. It crashed roughly one Windows run in five, independent of the change above. Ownership now stays with the test, which closes after joining, so interrupt can only ever see a live connection. Verified on real Windows: 5 consecutive daemon_ipc runs, no failure and no crash. macOS 46 passed x5 under ASan/UBSan; Linux 6606 passed, 0 failed. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 2740208 commit 5f2c146

3 files changed

Lines changed: 95 additions & 22 deletions

File tree

‎src/daemon/ipc.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,30 @@ void cbm_daemon_ipc_windows_legacy_guard_release_failures_set_for_test(unsigned
114114
memory_order_release);
115115
}
116116

117+
#ifdef _WIN32
118+
static cbm_daemon_ipc_startup_gate_fn g_startup_gate_for_test;
119+
static void *g_startup_gate_context_for_test;
120+
#endif
121+
122+
void cbm_daemon_ipc_startup_gate_set_for_test(cbm_daemon_ipc_startup_gate_fn gate, void *context) {
123+
#ifdef _WIN32
124+
g_startup_gate_context_for_test = context;
125+
g_startup_gate_for_test = gate;
126+
#else
127+
(void)gate;
128+
(void)context;
129+
#endif
130+
}
131+
132+
#ifdef _WIN32
133+
static void ipc_startup_gate_run(void) {
134+
cbm_daemon_ipc_startup_gate_fn gate = g_startup_gate_for_test;
135+
if (gate) {
136+
gate(g_startup_gate_context_for_test);
137+
}
138+
}
139+
#endif
140+
117141
bool cbm_daemon_ipc_windows_legacy_names(const char *canonical_runtime_parent,
118142
const char *instance_key,
119143
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
57555779
lock->legacy_guard = legacy_guard;
57565780
lock->legacy_sentinel = INVALID_HANDLE_VALUE;
57575781
*lock_out = lock;
5782+
/* The lock is held and the handoff has not run yet: the one point where a
5783+
* test can pin this interleaving deterministically. No-op in production. */
5784+
ipc_startup_gate_run();
57585785
return 1;
57595786
}
57605787

‎src/daemon/ipc_internal.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,13 @@ void cbm_daemon_ipc_posix_publication_hook_set_for_test(
9696
cbm_daemon_ipc_posix_publication_hook_fn hook, void *context);
9797
void cbm_daemon_ipc_windows_legacy_guard_release_failures_set_for_test(unsigned int count);
9898

99+
/* Deterministic-interleaving seam: fires on the Windows startup path once the
100+
* startup lock is held, before the rendezvous handoff. A test parks here to
101+
* pin the "startup is holding the lock" interleaving by construction. Polling
102+
* for that state instead is a race — the whole acquire → handoff → release
103+
* sequence completes in microseconds whenever the handoff does not block, so
104+
* the observation window has no lower bound. */
105+
typedef void (*cbm_daemon_ipc_startup_gate_fn)(void *context);
106+
void cbm_daemon_ipc_startup_gate_set_for_test(cbm_daemon_ipc_startup_gate_fn gate, void *context);
107+
99108
#endif /* CBM_DAEMON_IPC_INTERNAL_H */

‎tests/test_daemon_ipc.c‎

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,36 @@ typedef struct {
399399
int result;
400400
} ipc_test_win_startup_call_t;
401401

402+
typedef struct {
403+
atomic_bool acquired;
404+
atomic_bool may_proceed;
405+
} ipc_test_startup_gate_t;
406+
407+
/* Runs on the startup thread INSIDE cbm_daemon_ipc_startup_lock_try_acquire,
408+
* with the startup lock held and the rendezvous handoff not yet attempted.
409+
* Announce that state, then park until the test has released its reader. This
410+
* pins the interleaving by construction; the previous version polled for the
411+
* lock being busy, which is unobservable whenever the handoff does not block. */
412+
static void ipc_test_startup_gate(void *context) {
413+
ipc_test_startup_gate_t *gate = context;
414+
atomic_store(&gate->acquired, true);
415+
while (!atomic_load(&gate->may_proceed)) {
416+
cbm_usleep(200);
417+
}
418+
}
419+
420+
static bool ipc_test_startup_gate_wait_acquired(ipc_test_startup_gate_t *gate) {
421+
/* `acquired` never clears, so this wait cannot miss the event; the bound
422+
* only catches a thread that never started at all. */
423+
for (size_t attempt = 0; attempt < 50000U; attempt++) {
424+
if (atomic_load(&gate->acquired)) {
425+
return true;
426+
}
427+
cbm_usleep(200);
428+
}
429+
return false;
430+
}
431+
402432
static void *ipc_test_win_startup_call(void *opaque) {
403433
ipc_test_win_startup_call_t *call = opaque;
404434
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) {
416446
cbm_usleep(1000);
417447
}
418448
}
419-
420-
static bool ipc_test_win_lock_busy(cbm_private_lock_directory_t *directory, const char *base_name) {
421-
cbm_private_file_lock_t *probe = NULL;
422-
cbm_private_file_lock_status_t status =
423-
cbm_private_file_lock_try_acquire(directory, base_name, CBM_PRIVATE_FILE_LOCK_SH, &probe);
424-
ipc_test_win_lock_release(&probe);
425-
return status == CBM_PRIVATE_FILE_LOCK_BUSY;
426-
}
427449
#endif
428450

429451
typedef struct {
@@ -1028,6 +1050,10 @@ TEST(daemon_ipc_windows_startup_retries_transient_rendezvous_reader) {
10281050
bool startup_observed = false;
10291051
int join_status = -1;
10301052
ipc_test_win_startup_call_t call = {.result = -1};
1053+
ipc_test_startup_gate_t gate;
1054+
atomic_init(&gate.acquired, false);
1055+
atomic_init(&gate.may_proceed, false);
1056+
cbm_daemon_ipc_startup_gate_set_for_test(ipc_test_startup_gate, &gate);
10311057

10321058
bool parent_ok = ipc_test_parent_new(parent, "win-record-reader");
10331059
if (parent_ok) {
@@ -1048,21 +1074,19 @@ TEST(daemon_ipc_windows_startup_retries_transient_rendezvous_reader) {
10481074
if (record_status == CBM_PRIVATE_FILE_LOCK_OK) {
10491075
thread_started = cbm_thread_create(&thread, 0, ipc_test_win_startup_call, &call) == 0;
10501076
}
1051-
for (size_t attempt = 0; thread_started && attempt < 200U; attempt++) {
1052-
if (ipc_test_win_lock_busy(directory, "cbm-startup-v2.lock")) {
1053-
startup_observed = true;
1054-
break;
1055-
}
1056-
cbm_usleep(1000);
1077+
if (thread_started) {
1078+
startup_observed = ipc_test_startup_gate_wait_acquired(&gate);
10571079
}
10581080
ipc_test_win_lock_release(&record_reader);
1081+
atomic_store(&gate.may_proceed, true);
10591082
if (thread_started) {
10601083
join_status = cbm_thread_join(&thread);
10611084
}
10621085
if (endpoint) {
10631086
ipc_test_copy_path(address, cbm_daemon_ipc_endpoint_address(endpoint));
10641087
}
10651088

1089+
cbm_daemon_ipc_startup_gate_set_for_test(NULL, NULL);
10661090
ipc_test_win_lock_release(&record_reader);
10671091
cbm_private_lock_directory_close(directory);
10681092
cbm_daemon_ipc_endpoint_free(endpoint);
@@ -1115,6 +1139,11 @@ TEST(daemon_ipc_windows_rendezvous_bridges_concurrent_lifetime_owner) {
11151139
cbm_daemon_ipc_startup_lock_release(&initial_startup);
11161140
initial_startup = NULL;
11171141

1142+
ipc_test_startup_gate_t gate;
1143+
atomic_init(&gate.acquired, false);
1144+
atomic_init(&gate.may_proceed, false);
1145+
cbm_daemon_ipc_startup_gate_set_for_test(ipc_test_startup_gate, &gate);
1146+
11181147
cbm_private_file_lock_status_t directory_status =
11191148
endpoint ? cbm_daemon_ipc_private_lock_directory_new(endpoint, &directory)
11201149
: CBM_PRIVATE_FILE_LOCK_IO;
@@ -1126,19 +1155,18 @@ TEST(daemon_ipc_windows_rendezvous_bridges_concurrent_lifetime_owner) {
11261155
if (record_status == CBM_PRIVATE_FILE_LOCK_OK) {
11271156
thread_started = cbm_thread_create(&thread, 0, ipc_test_win_startup_call, &call) == 0;
11281157
}
1129-
for (size_t attempt = 0; thread_started && attempt < 200U; attempt++) {
1130-
if (ipc_test_win_lock_busy(directory, "cbm-startup-v2.lock")) {
1131-
startup_observed = true;
1132-
break;
1133-
}
1134-
cbm_usleep(1000);
1158+
if (thread_started) {
1159+
startup_observed = ipc_test_startup_gate_wait_acquired(&gate);
11351160
}
1161+
/* Claim the lifetime lock while startup is parked in the gate, so the
1162+
* concurrent-owner bridge under test is exercised deterministically. */
11361163
cbm_private_file_lock_status_t lifetime_status =
11371164
startup_observed
11381165
? cbm_private_file_lock_try_acquire(directory, "cbm-lifetime.lock",
11391166
CBM_PRIVATE_FILE_LOCK_EX, &lifetime_owner)
11401167
: CBM_PRIVATE_FILE_LOCK_IO;
11411168
ipc_test_win_lock_release(&record_reader);
1169+
atomic_store(&gate.may_proceed, true);
11421170
if (thread_started) {
11431171
join_status = cbm_thread_join(&thread);
11441172
}
@@ -1147,6 +1175,7 @@ TEST(daemon_ipc_windows_rendezvous_bridges_concurrent_lifetime_owner) {
11471175
}
11481176
ipc_test_win_lock_release(&lifetime_owner);
11491177

1178+
cbm_daemon_ipc_startup_gate_set_for_test(NULL, NULL);
11501179
ipc_test_win_lock_release(&record_reader);
11511180
ipc_test_win_lock_release(&lifetime_owner);
11521181
cbm_private_lock_directory_close(directory);
@@ -2072,6 +2101,13 @@ typedef struct {
20722101
int receive_result;
20732102
} ipc_forever_wait_server_t;
20742103

2104+
/* The connection is deliberately NOT closed here. The test interrupts this
2105+
* thread's wait through server->connection, and `receive_frame` can also return
2106+
* on its own (a torn connection, an early frame) before that call lands.
2107+
* Closing from this thread therefore freed the connection while the test was
2108+
* still dereferencing the pointer it had already loaded — a use-after-free that
2109+
* crashed roughly one Windows run in five. Ownership stays with the test, which
2110+
* closes it after joining, so `interrupt` can only ever see a live connection. */
20752111
static void *ipc_forever_wait_server(void *opaque) {
20762112
ipc_forever_wait_server_t *server = (ipc_forever_wait_server_t *)opaque;
20772113
cbm_daemon_frame_t frame = {0};
@@ -2084,8 +2120,6 @@ static void *ipc_forever_wait_server(void *opaque) {
20842120
server->connection, CBM_DAEMON_IPC_WAIT_FOREVER, &frame, &payload);
20852121
}
20862122
free(payload);
2087-
cbm_daemon_ipc_connection_close(server->connection);
2088-
server->connection = NULL;
20892123
return NULL;
20902124
}
20912125

@@ -2129,6 +2163,9 @@ TEST(daemon_ipc_wait_forever_is_interruptible) {
21292163
if (thread_started) {
21302164
join_result = cbm_thread_join(&thread);
21312165
}
2166+
/* Safe only after the join: the server thread no longer touches it. */
2167+
cbm_daemon_ipc_connection_close(server.connection);
2168+
server.connection = NULL;
21322169
cbm_daemon_ipc_connection_close(client);
21332170
cbm_daemon_ipc_listener_close(listener);
21342171
cbm_daemon_ipc_endpoint_free(endpoint);

0 commit comments

Comments
 (0)