Skip to content

Commit 4c6ecff

Browse files
committed
test(daemon-ipc): wait for the startup gate event, not an attempt budget
daemon_ipc_windows_startup_retries_transient_rendezvous_reader failed on a windows-latest CLANG64 runner (ASSERT(startup_observed); the whole suite took 7 s) on a PR that touched only CLI files. ipc_test_startup_gate_wait_acquired polled 50000 x cbm_usleep(200) and returned false once the budget ran out. On POSIX that is ~10 s, but on Windows cbm_usleep(us) is Sleep(us / 1000), so cbm_usleep(200) is Sleep(0): the budget collapsed to 50000 bare yields, which a loaded runner spends in milliseconds before the startup thread reaches the gate. A timeout decided the verdict. The helper now waits until the startup thread has either parked in the gate or returned without reaching it (a new `finished` flag, set by ipc_test_win_startup_call after its call returns). Both flags only go false -> true and one of them is always set: before the gate the startup path only try-acquires, and a thread parked in the gate cannot return until the test releases it. No bound is left to decide the verdict. A thread that returns without reaching the gate fails at once, and the test reports its try-acquire status as the reason. The same shape is removed from two cross-platform tests: - daemon_ipc_wait_forever_is_interruptible waited for the server's forever-wait under a 2 s wall-clock deadline. Past the deadline the test skipped the interrupt, so the join then blocked on a server parked in WAIT_FOREVER: a hang, not a failure. It now waits for `waiting` or for a new `finished` flag set when the server thread returns. - daemon_ipc_connect_waits_for_delayed_listener capped its wait for `delay_started` at 2000 polls. That store is the thread's first action, so the wait needs no attempt bound. No production change and no new sleep: the existing per-poll yields stay. Proof. A standalone harness mirrors the gate, both helpers and the thread wrapper, with cbm_usleep modelled as on Windows (Sleep(us / 1000), so cbm_usleep(200) is a bare yield): - startup thread arriving only after 50000 polls: old helper 20/20 FAIL (startup_observed=false with call.result=1, the CI signature), new helper 20/20 PASS; - startup thread descheduled 500 ms: the old helper gives up after 9 ms, 10/10 FAIL; new helper 10/10 PASS; control, old helper with POSIX usleep: 10/10 PASS; - thread returning without reaching the gate: the new helper fails within 11 ms with call.result=-1 (the old one took 16 s under POSIX). Real daemon_ipc suite on macOS: with the server and listener threads held 4 s and 6 s (beyond both old bounds) the two cross-platform tests pass. The old 2000-poll loop under the 6 s hold fails ASSERT(delay_observed). The old 2 s deadline under the 4 s hold hangs in the join until killed. A server that skips accept makes the new loop fail ASSERT(reached_wait) with no added wait. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 1f9b4db commit 4c6ecff

1 file changed

Lines changed: 48 additions & 20 deletions

File tree

‎tests/test_daemon_ipc.c‎

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ static int ipc_test_win_lock_child(const char *kind, const char *key, const char
401401
typedef struct {
402402
const cbm_daemon_ipc_endpoint_t *endpoint;
403403
int result;
404+
atomic_bool finished; /* set once the startup call has fully returned */
404405
} ipc_test_win_startup_call_t;
405406

406407
typedef struct {
@@ -421,16 +422,22 @@ static void ipc_test_startup_gate(void *context) {
421422
}
422423
}
423424

424-
static bool ipc_test_startup_gate_wait_acquired(ipc_test_startup_gate_t *gate) {
425-
/* `acquired` never clears, so this wait cannot miss the event; the bound
426-
* only catches a thread that never started at all. */
427-
for (size_t attempt = 0; attempt < 50000U; attempt++) {
428-
if (atomic_load(&gate->acquired)) {
429-
return true;
430-
}
425+
/* Waits until the startup thread has either parked in the gate or returned
426+
* without reaching it. Both flags only ever go false -> true, and one of them
427+
* is always eventually set: before the gate the startup path only try-acquires
428+
* (no blocking wait on anything this test holds), and a thread parked in the
429+
* gate cannot return until the test releases it. So no attempt or time bound
430+
* is needed, and none may decide the verdict: the previous 50000 x
431+
* cbm_usleep(200) budget meant ~10 s on POSIX but, since cbm_usleep(200) is
432+
* Sleep(0) on Windows, only 50000 bare yields there -- a loaded CI runner
433+
* exhausted them before the thread reached the gate. A thread that never
434+
* started is caught by the caller's thread_started check. */
435+
static bool ipc_test_startup_gate_wait_acquired(ipc_test_startup_gate_t *gate,
436+
ipc_test_win_startup_call_t *call) {
437+
while (!atomic_load(&gate->acquired) && !atomic_load(&call->finished)) {
431438
cbm_usleep(200);
432439
}
433-
return false;
440+
return atomic_load(&gate->acquired);
434441
}
435442

436443
static void *ipc_test_win_startup_call(void *opaque) {
@@ -441,6 +448,7 @@ static void *ipc_test_win_startup_call(void *opaque) {
441448
call->result = 0;
442449
}
443450
cbm_daemon_ipc_startup_lock_release(&startup);
451+
atomic_store(&call->finished, true);
444452
return NULL;
445453
}
446454

@@ -1395,6 +1403,7 @@ TEST(daemon_ipc_windows_startup_retries_transient_rendezvous_reader) {
13951403
bool startup_observed = false;
13961404
int join_status = -1;
13971405
ipc_test_win_startup_call_t call = {.result = -1};
1406+
atomic_init(&call.finished, false);
13981407
ipc_test_startup_gate_t gate;
13991408
atomic_init(&gate.acquired, false);
14001409
atomic_init(&gate.may_proceed, false);
@@ -1420,7 +1429,7 @@ TEST(daemon_ipc_windows_startup_retries_transient_rendezvous_reader) {
14201429
thread_started = cbm_thread_create(&thread, 0, ipc_test_win_startup_call, &call) == 0;
14211430
}
14221431
if (thread_started) {
1423-
startup_observed = ipc_test_startup_gate_wait_acquired(&gate);
1432+
startup_observed = ipc_test_startup_gate_wait_acquired(&gate, &call);
14241433
}
14251434
ipc_test_win_lock_release(&record_reader);
14261435
atomic_store(&gate.may_proceed, true);
@@ -1441,7 +1450,14 @@ TEST(daemon_ipc_windows_startup_retries_transient_rendezvous_reader) {
14411450
ASSERT_EQ(directory_status, CBM_PRIVATE_FILE_LOCK_OK);
14421451
ASSERT_EQ(record_status, CBM_PRIVATE_FILE_LOCK_OK);
14431452
ASSERT_TRUE(thread_started);
1444-
ASSERT_TRUE(startup_observed);
1453+
if (!startup_observed) {
1454+
/* The startup thread returned without reaching the gate; its
1455+
* try-acquire status is the true reason. */
1456+
char reason[96];
1457+
(void)snprintf(reason, sizeof(reason), "startup call returned %d without reaching the gate",
1458+
call.result);
1459+
FAIL(reason);
1460+
}
14451461
ASSERT_EQ(join_status, 0);
14461462
ASSERT_EQ(call.result, 1);
14471463
ASSERT_TRUE(address[0] != '\0');
@@ -1464,6 +1480,7 @@ TEST(daemon_ipc_windows_rendezvous_bridges_concurrent_lifetime_owner) {
14641480
bool startup_observed = false;
14651481
int join_status = -1;
14661482
ipc_test_win_startup_call_t call = {.result = -1};
1483+
atomic_init(&call.finished, false);
14671484

14681485
bool parent_ok = ipc_test_parent_new(parent, "win-lifetime-bridge");
14691486
if (parent_ok) {
@@ -1501,7 +1518,7 @@ TEST(daemon_ipc_windows_rendezvous_bridges_concurrent_lifetime_owner) {
15011518
thread_started = cbm_thread_create(&thread, 0, ipc_test_win_startup_call, &call) == 0;
15021519
}
15031520
if (thread_started) {
1504-
startup_observed = ipc_test_startup_gate_wait_acquired(&gate);
1521+
startup_observed = ipc_test_startup_gate_wait_acquired(&gate, &call);
15051522
}
15061523
/* Claim the lifetime lock while startup is parked in the gate, so the
15071524
* concurrent-owner bridge under test is exercised deterministically. */
@@ -1534,7 +1551,14 @@ TEST(daemon_ipc_windows_rendezvous_bridges_concurrent_lifetime_owner) {
15341551
ASSERT_EQ(directory_status, CBM_PRIVATE_FILE_LOCK_OK);
15351552
ASSERT_EQ(record_status, CBM_PRIVATE_FILE_LOCK_OK);
15361553
ASSERT_TRUE(thread_started);
1537-
ASSERT_TRUE(startup_observed);
1554+
if (!startup_observed) {
1555+
/* The startup thread returned without reaching the gate; its
1556+
* try-acquire status is the true reason. */
1557+
char reason[96];
1558+
(void)snprintf(reason, sizeof(reason), "startup call returned %d without reaching the gate",
1559+
call.result);
1560+
FAIL(reason);
1561+
}
15381562
ASSERT_EQ(lifetime_status, CBM_PRIVATE_FILE_LOCK_OK);
15391563
ASSERT_EQ(join_status, 0);
15401564
ASSERT_EQ(call.result, 0);
@@ -2442,6 +2466,7 @@ typedef struct {
24422466
cbm_daemon_ipc_listener_t *listener;
24432467
cbm_daemon_ipc_connection_t *connection;
24442468
atomic_bool waiting;
2469+
atomic_bool finished; /* set once the server thread has returned */
24452470
int receive_result;
24462471
} ipc_forever_wait_server_t;
24472472

@@ -2464,6 +2489,7 @@ static void *ipc_forever_wait_server(void *opaque) {
24642489
server->connection, CBM_DAEMON_IPC_WAIT_FOREVER, &frame, &payload);
24652490
}
24662491
free(payload);
2492+
atomic_store_explicit(&server->finished, true, memory_order_release);
24672493
return NULL;
24682494
}
24692495

@@ -2480,6 +2506,7 @@ TEST(daemon_ipc_wait_forever_is_interruptible) {
24802506
int join_result = -1;
24812507
ipc_forever_wait_server_t server = {0};
24822508
atomic_init(&server.waiting, false);
2509+
atomic_init(&server.finished, false);
24832510

24842511
if (ipc_test_parent_new(parent, "wait-forever")) {
24852512
endpoint = cbm_daemon_ipc_endpoint_new(key, parent);
@@ -2495,9 +2522,11 @@ TEST(daemon_ipc_wait_forever_is_interruptible) {
24952522
if (thread_started) {
24962523
client = cbm_daemon_ipc_connect(endpoint, 2000);
24972524
}
2498-
uint64_t deadline = cbm_now_ms() + 2000;
2499-
while (client && cbm_now_ms() < deadline &&
2500-
!atomic_load_explicit(&server.waiting, memory_order_acquire)) {
2525+
/* Wait for the event itself, never a deadline: the server either enters
2526+
* its forever-wait or returns (its accept is bounded by the call's own
2527+
* timeout), and both flags only ever go false -> true. */
2528+
while (client && !atomic_load_explicit(&server.waiting, memory_order_acquire) &&
2529+
!atomic_load_explicit(&server.finished, memory_order_acquire)) {
25012530
cbm_usleep(1000);
25022531
}
25032532
reached_wait = atomic_load_explicit(&server.waiting, memory_order_acquire);
@@ -2561,14 +2590,13 @@ TEST(daemon_ipc_connect_waits_for_delayed_listener) {
25612590
thread_started = cbm_thread_create(&thread, 0, ipc_delayed_listener_start, &delayed) == 0;
25622591
}
25632592
if (thread_started) {
2593+
/* `delay_started` is the thread's first store, so it cannot return
2594+
* without setting it: wait for that event with no attempt bound. */
25642595
struct timespec poll_delay = {.tv_sec = 0, .tv_nsec = 1000000};
2565-
for (size_t i = 0; i < 2000; i++) {
2566-
if (atomic_load_explicit(&delayed.delay_started, memory_order_acquire)) {
2567-
delay_observed = true;
2568-
break;
2569-
}
2596+
while (!atomic_load_explicit(&delayed.delay_started, memory_order_acquire)) {
25702597
(void)cbm_nanosleep(&poll_delay, NULL);
25712598
}
2599+
delay_observed = true;
25722600
}
25732601
if (delay_observed) {
25742602
client = cbm_daemon_ipc_connect(endpoint, 1500);

0 commit comments

Comments
 (0)