@@ -1262,6 +1262,19 @@ static bool runtime_test_fixture_start_failed(const char *tag, const char *stage
12621262
12631263static bool runtime_test_fixture_permanent = false;
12641264
1265+ typedef struct {
1266+ cbm_version_cohort_manager_t * manager ;
1267+ cbm_version_cohort_lease_t * lease ;
1268+ cbm_daemon_ipc_participant_guard_t * guard ;
1269+ cbm_version_cohort_daemon_claim_t * claim ;
1270+ bool held ;
1271+ } runtime_test_coordination_t ;
1272+
1273+ /* When set, the fixture takes the host's coordination handles in the host's
1274+ * order before starting the service: cohort admission refuses once a
1275+ * generation is already serving. */
1276+ static runtime_test_coordination_t * runtime_test_fixture_coordination = NULL ;
1277+
12651278static bool runtime_test_fixture_start_configured (
12661279 runtime_test_fixture_t * fixture , const char * tag , const cbm_daemon_build_identity_t * identity ,
12671280 uint32_t max_clients , uint64_t lease_timeout_ms ,
@@ -1317,6 +1330,19 @@ static bool runtime_test_fixture_start_configured(
13171330 if (application ) {
13181331 config .application = * application ;
13191332 }
1333+ runtime_test_coordination_t * coordination = runtime_test_fixture_coordination ;
1334+ if (coordination ) {
1335+ cbm_daemon_conflict_t conflict ;
1336+ coordination -> manager = cbm_version_cohort_manager_new (fixture -> endpoint );
1337+ coordination -> held =
1338+ coordination -> manager &&
1339+ cbm_version_cohort_acquire (coordination -> manager , identity , cbm_now_ms () + 5000 ,
1340+ & coordination -> lease , & conflict ) == CBM_VERSION_COHORT_OK &&
1341+ cbm_daemon_ipc_participant_guard_try_join (fixture -> endpoint , & coordination -> guard ) ==
1342+ 1 &&
1343+ cbm_version_cohort_daemon_claim_acquire (coordination -> manager , & coordination -> claim ) ==
1344+ CBM_VERSION_COHORT_OK ;
1345+ }
13201346 fixture -> service = cbm_daemon_runtime_service_start (& config );
13211347 if (!fixture -> service ) {
13221348 return runtime_test_fixture_start_failed (tag , "service-start" ,
@@ -1370,6 +1396,110 @@ TEST(daemon_runtime_convenience_service_owns_participant_guard) {
13701396 PASS ();
13711397}
13721398
1399+ #ifndef _WIN32
1400+ /* The runtime's threads log concurrently while the loop stops the service, so
1401+ * the sink only sets flags. */
1402+ static atomic_bool runtime_test_coordination_lost_logged ;
1403+ static atomic_bool runtime_test_window_expired_logged ;
1404+
1405+ static void runtime_test_lifetime_log_sink (const char * line ) {
1406+ /* Substrings, not key=value spelling, so text and JSON formats both match. */
1407+ if (line && strstr (line , "daemon.lifetime_end" ) && strstr (line , "coordination_file_lost" ) &&
1408+ strstr (line , "cohort_lease" )) {
1409+ atomic_store (& runtime_test_coordination_lost_logged , true);
1410+ }
1411+ if (line && strstr (line , "initial_window_expired" )) {
1412+ atomic_store (& runtime_test_window_expired_logged , true);
1413+ }
1414+ }
1415+ #endif
1416+
1417+ /* #2178: the host lifetime loop must refresh every held coordination file on
1418+ * its tick and stop the generation once one is lost. The claim marker is aged
1419+ * and the cohort lifetime lock unlinked: the tick must refresh the marker
1420+ * through its held handle, name the lease (touched after the claim) as lost,
1421+ * and stop the runtime. */
1422+ TEST (daemon_runtime_host_loop_stops_on_coordination_file_loss ) {
1423+ #ifdef _WIN32
1424+ SKIP_PLATFORM ("unlinking a held lock file is POSIX behavior" );
1425+ #else
1426+ cbm_daemon_build_identity_t identity =
1427+ runtime_test_identity ("2.4.0" , runtime_test_self_build ());
1428+ identity .cache_fingerprint = RUNTIME_CACHE_A ; /* cohort admission requires one */
1429+ runtime_test_fixture_t fixture ;
1430+ runtime_test_coordination_t coordination = {0 };
1431+ runtime_test_fixture_coordination = & coordination ;
1432+ bool started = runtime_test_fixture_start (& fixture , "coordination-loss" , & identity );
1433+ runtime_test_fixture_coordination = NULL ;
1434+ bool held = coordination .held ;
1435+
1436+ char marker [RUNTIME_TEST_PATH_CAP ];
1437+ char lifetime [RUNTIME_TEST_PATH_CAP ];
1438+ int marker_written = snprintf (marker , sizeof (marker ), "%s/cbm-version-cohort-daemon-v1.lock" ,
1439+ fixture .runtime_dir );
1440+ int lifetime_written = snprintf (lifetime , sizeof (lifetime ),
1441+ "%s/cbm-version-cohort-lifetime-v1.lock" , fixture .runtime_dir );
1442+ const time_t stale = 1000000000 ;
1443+ struct timespec stale_times [2 ] = {{.tv_sec = stale }, {.tv_sec = stale }};
1444+ bool staged = held && marker_written > 0 && marker_written < (int )sizeof (marker ) &&
1445+ lifetime_written > 0 && lifetime_written < (int )sizeof (lifetime ) &&
1446+ utimensat (AT_FDCWD , marker , stale_times , 0 ) == 0 && unlink (lifetime ) == 0 ;
1447+
1448+ bool loop_ok = false;
1449+ if (staged ) {
1450+ atomic_store (& runtime_test_coordination_lost_logged , false);
1451+ atomic_store (& runtime_test_window_expired_logged , false);
1452+ CBMLogLevel previous_log_level = cbm_log_get_level ();
1453+ cbm_log_set_level (CBM_LOG_INFO );
1454+ cbm_log_set_sink (runtime_test_lifetime_log_sink );
1455+ loop_ok = cbm_daemon_host_wait_for_lifetime_for_test (
1456+ fixture .service , fixture .endpoint , coordination .lease , coordination .claim ,
1457+ coordination .guard );
1458+ cbm_log_set_sink (NULL );
1459+ cbm_log_set_level (previous_log_level );
1460+ }
1461+ cbm_daemon_runtime_service_state_t state =
1462+ started ? cbm_daemon_runtime_service_state (fixture .service )
1463+ : CBM_DAEMON_RUNTIME_SERVICE_RUNNING ;
1464+ struct stat marker_status = {0 };
1465+ bool marker_refreshed = staged && stat (marker , & marker_status ) == 0 &&
1466+ marker_status .st_mtime > stale && marker_status .st_atime > stale ;
1467+
1468+ /* Host teardown order: service first, then guard, claim and lease. Every
1469+ * handle references the endpoint, which the fixture frees. */
1470+ if (started && state != CBM_DAEMON_RUNTIME_SERVICE_EXITED ) {
1471+ (void )cbm_daemon_runtime_service_stop (fixture .service , RUNTIME_TEST_TIMEOUT_MS );
1472+ }
1473+ if (fixture .service && cbm_daemon_runtime_service_free (fixture .service )) {
1474+ fixture .service = NULL ;
1475+ }
1476+ (void )cbm_daemon_ipc_participant_guard_release (& coordination .guard );
1477+ while (coordination .claim && cbm_version_cohort_daemon_claim_release (& coordination .claim ) !=
1478+ CBM_PRIVATE_FILE_LOCK_OK ) {
1479+ cbm_usleep (1000 );
1480+ }
1481+ while (coordination .lease &&
1482+ cbm_version_cohort_lease_release (& coordination .lease ) != CBM_PRIVATE_FILE_LOCK_OK ) {
1483+ cbm_usleep (1000 );
1484+ }
1485+ while (coordination .manager &&
1486+ cbm_version_cohort_manager_free (& coordination .manager ) != CBM_PRIVATE_FILE_LOCK_OK ) {
1487+ cbm_usleep (1000 );
1488+ }
1489+ runtime_test_fixture_finish (& fixture );
1490+
1491+ ASSERT_TRUE (started );
1492+ ASSERT_TRUE (held );
1493+ ASSERT_TRUE (staged );
1494+ ASSERT_TRUE (loop_ok );
1495+ ASSERT_EQ (state , CBM_DAEMON_RUNTIME_SERVICE_EXITED );
1496+ ASSERT_TRUE (marker_refreshed );
1497+ ASSERT_TRUE (atomic_load (& runtime_test_coordination_lost_logged ));
1498+ ASSERT_FALSE (atomic_load (& runtime_test_window_expired_logged ));
1499+ PASS ();
1500+ #endif
1501+ }
1502+
13731503static bool runtime_test_fixture_start_application (runtime_test_fixture_t * fixture , const char * tag ,
13741504 const cbm_daemon_build_identity_t * identity ,
13751505 runtime_application_context_t * context ) {
@@ -5106,6 +5236,7 @@ SUITE(daemon_runtime) {
51065236 RUN_TEST (daemon_runtime_process_fingerprint_never_hashes_replacement_path );
51075237#endif
51085238 RUN_TEST (daemon_runtime_convenience_service_owns_participant_guard );
5239+ RUN_TEST (daemon_runtime_host_loop_stops_on_coordination_file_loss );
51095240 RUN_TEST (daemon_runtime_rendezvous_layout_is_frozen_and_detailed_abi_independent );
51105241 RUN_TEST (daemon_runtime_exact_hello_issues_connection_bound_identity );
51115242 RUN_TEST (daemon_runtime_image_rejection_reaches_client_issue1383 );
0 commit comments