@@ -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+
402432static 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
429451typedef 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. */
20752111static 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