5252 HOST_HTTP_RETRY_INITIAL_MS = 1000 ,
5353 HOST_HTTP_RETRY_MAX_MS = 30000 ,
5454 HOST_WATCH_INTERVAL_MS = 5000 ,
55+ /* Age-based temp cleaners need days of inactivity (macOS tmp_cleaner: 3);
56+ * a minute bounds the blind window after a long sleep (#2178). */
57+ HOST_COORDINATION_TOUCH_MS = 60000 ,
5558 HOST_CONFLICT_LOG_CAP = 1024 * 1024 ,
5659 HOST_OPERATION_LOG_CAP = 5 * 1024 * 1024 ,
5760 HOST_PATH_CAP = 4096 ,
@@ -875,10 +878,58 @@ static bool host_background_start(host_state_t *host) {
875878 return true;
876879}
877880
881+ /* Coordination handles the host retains for the generation's whole lifetime. */
882+ typedef struct {
883+ const cbm_daemon_ipc_endpoint_t * endpoint ;
884+ cbm_version_cohort_lease_t * cohort_lease ;
885+ cbm_version_cohort_daemon_claim_t * daemon_claim ;
886+ cbm_daemon_ipc_participant_guard_t * participant_guard ;
887+ } host_coordination_t ;
888+
889+ typedef struct {
890+ const char * lost ; /* first held file whose path no longer names it */
891+ const char * transient ; /* first still-valid file that could not be refreshed */
892+ } host_touch_result_t ;
893+
894+ static void host_touch_note (host_touch_result_t * result , const char * file , int status ) {
895+ if (status == 0 && !result -> lost ) {
896+ result -> lost = file ;
897+ } else if (status < 0 && !result -> transient ) {
898+ result -> transient = file ;
899+ }
900+ }
901+
902+ static int host_cohort_touch_status (cbm_version_cohort_status_t status ) {
903+ return status == CBM_VERSION_COHORT_OK ? 1 : (status == CBM_VERSION_COHORT_IO ? -1 : 0 );
904+ }
905+
906+ /* Refresh every long-held runtime file. A held lock whose path now names
907+ * another inode coordinates nothing: forked index workers read the daemon as
908+ * uncoordinated and a peer can start a second generation (#2178). Every file
909+ * is visited even after one fails so the rest keep being refreshed. */
910+ static host_touch_result_t host_coordination_touch (const host_coordination_t * coordination ,
911+ cbm_daemon_runtime_service_t * service ) {
912+ host_touch_result_t result = {0 };
913+ host_touch_note (& result , "cohort_daemon_claim" ,
914+ host_cohort_touch_status (
915+ cbm_version_cohort_daemon_claim_touch (coordination -> daemon_claim )));
916+ host_touch_note (
917+ & result , "cohort_lease" ,
918+ host_cohort_touch_status (cbm_version_cohort_lease_touch (coordination -> cohort_lease )));
919+ host_touch_note (& result , "participant_guard" ,
920+ cbm_daemon_ipc_participant_guard_touch (coordination -> endpoint ,
921+ coordination -> participant_guard ));
922+ host_touch_note (& result , "listener" , cbm_daemon_runtime_service_touch_listener (service ));
923+ return result ;
924+ }
925+
878926static bool host_wait_for_lifetime (cbm_daemon_runtime_service_t * service ,
879- atomic_int * stop_requested , host_state_t * host , bool permanent ) {
927+ atomic_int * stop_requested , host_state_t * host , bool permanent ,
928+ const host_coordination_t * coordination ) {
880929 uint64_t initial_deadline = cbm_now_ms () + HOST_INITIAL_CLIENT_TIMEOUT_MS ;
881930 uint64_t stopping_deadline = 0 ;
931+ uint64_t next_touch = 0 ;
932+ bool touch_degraded = false;
882933 for (;;) {
883934 cbm_daemon_runtime_service_state_t state = cbm_daemon_runtime_service_state (service );
884935 if (state == CBM_DAEMON_RUNTIME_SERVICE_EXITED ) {
@@ -910,6 +961,28 @@ static bool host_wait_for_lifetime(cbm_daemon_runtime_service_t *service,
910961 stop ? "stop_requested" : "initial_window_expired" );
911962 return cbm_daemon_runtime_service_stop (service , HOST_RUNTIME_SHUTDOWN_MS );
912963 }
964+ /* Permanent generations too: exiting lets the next client start a
965+ * coordinated generation, while re-claiming would race that client. */
966+ if (cbm_now_ms () >= next_touch ) {
967+ next_touch = cbm_now_ms () + HOST_COORDINATION_TOUCH_MS ;
968+ host_touch_result_t touched = host_coordination_touch (coordination , service );
969+ if (touched .lost ) {
970+ cbm_log_warn ("daemon.lifetime_end" , "reason" , "coordination_file_lost" , "file" ,
971+ touched .lost );
972+ return cbm_daemon_runtime_service_stop (service , HOST_RUNTIME_SHUTDOWN_MS );
973+ }
974+ /* A refresh failure on a still-valid file (read-only remount,
975+ * EMFILE) is not loss: keep serving, retry next tick, and log
976+ * only transitions so a persistent condition cannot flood. */
977+ if ((touched .transient != NULL ) != touch_degraded ) {
978+ touch_degraded = touched .transient != NULL ;
979+ if (touch_degraded ) {
980+ cbm_log_warn ("daemon.coordination_touch_failed" , "file" , touched .transient );
981+ } else {
982+ cbm_log_info ("daemon.coordination_touch_recovered" );
983+ }
984+ }
985+ }
913986 host_http_reconcile_at (host , cbm_now_ms (), false);
914987 /* Retire an ephemeral generation that lingered for cold-storm cohort
915988 * participants once they drain, or once its bounded linger elapses.
@@ -919,6 +992,22 @@ static bool host_wait_for_lifetime(cbm_daemon_runtime_service_t *service,
919992 }
920993}
921994
995+ bool cbm_daemon_host_wait_for_lifetime_for_test (cbm_daemon_runtime_service_t * service ,
996+ const cbm_daemon_ipc_endpoint_t * endpoint ,
997+ cbm_version_cohort_lease_t * cohort_lease ,
998+ cbm_version_cohort_daemon_claim_t * daemon_claim ,
999+ cbm_daemon_ipc_participant_guard_t * guard ) {
1000+ host_state_t host = {0 };
1001+ atomic_int stop_requested = 0 ;
1002+ host_coordination_t coordination = {
1003+ .endpoint = endpoint ,
1004+ .cohort_lease = cohort_lease ,
1005+ .daemon_claim = daemon_claim ,
1006+ .participant_guard = guard ,
1007+ };
1008+ return host_wait_for_lifetime (service , & stop_requested , & host , false, & coordination );
1009+ }
1010+
9221011static bool host_application_shutdown (host_state_t * host ) {
9231012 if (cbm_daemon_application_shutdown (host -> application , HOST_APPLICATION_SHUTDOWN_MS )) {
9241013 return true;
@@ -1130,7 +1219,14 @@ int cbm_daemon_host_run(const cbm_daemon_host_config_t *config) {
11301219 : "unavailable" ,
11311220 "memory_budget_bytes" , memory_budget , "physical_job_limit" , physical_job_limit ,
11321221 "worker_memory_budget_bytes" , worker_memory_budget );
1133- if (!host_wait_for_lifetime (service , config -> stop_requested , & host , config -> permanent )) {
1222+ host_coordination_t coordination = {
1223+ .endpoint = config -> endpoint ,
1224+ .cohort_lease = cohort_lease ,
1225+ .daemon_claim = daemon_claim ,
1226+ .participant_guard = participant_guard ,
1227+ };
1228+ if (!host_wait_for_lifetime (service , config -> stop_requested , & host , config -> permanent ,
1229+ & coordination )) {
11341230 host_force_terminate ("runtime" );
11351231 }
11361232
0 commit comments