| 876 | } |
| 877 | |
| 878 | static bool host_wait_for_lifetime(cbm_daemon_runtime_service_t *service, |
| 879 | atomic_int *stop_requested, host_state_t *host, bool permanent) { |
| 880 | uint64_t initial_deadline = cbm_now_ms() + HOST_INITIAL_CLIENT_TIMEOUT_MS; |
| 881 | uint64_t stopping_deadline = 0; |
| 882 | for (;;) { |
| 883 | cbm_daemon_runtime_service_state_t state = cbm_daemon_runtime_service_state(service); |
| 884 | if (state == CBM_DAEMON_RUNTIME_SERVICE_EXITED) { |
| 885 | cbm_log_info("daemon.lifetime_end", "reason", "runtime_exited"); |
| 886 | return true; |
| 887 | } |
| 888 | if (state == CBM_DAEMON_RUNTIME_SERVICE_STOPPING) { |
| 889 | if (stopping_deadline == 0) { |
| 890 | stopping_deadline = cbm_now_ms() + HOST_RUNTIME_SHUTDOWN_MS; |
| 891 | } |
| 892 | if (cbm_now_ms() >= stopping_deadline) { |
| 893 | return false; |
| 894 | } |
| 895 | (void)cbm_daemon_runtime_service_wait_exited(service, HOST_WAIT_TICK_MS); |
| 896 | continue; |
| 897 | } |
| 898 | /* The nobody-ever-connected window latches on the monotonic admission |
| 899 | * total, not the live client count: a short first session can begin |
| 900 | * and end entirely between two polls of this loop, and a sampled |
| 901 | * count then reads zero forever — stopping a daemon whose client is |
| 902 | * mid-conversation between per-request connections. */ |
| 903 | bool admitted = cbm_daemon_runtime_service_clients_admitted_total(service) > 0; |
| 904 | bool stop = stop_requested && atomic_load(stop_requested); |
| 905 | /* A permanent generation (`daemon start`) skips the nobody-ever- |
| 906 | * connected window — it exists precisely to idle ahead of clients. |
| 907 | * Explicit stop requests (signals) are still honored. */ |
| 908 | if (stop || (!permanent && !admitted && cbm_now_ms() >= initial_deadline)) { |
| 909 | cbm_log_info("daemon.lifetime_end", "reason", |
| 910 | stop ? "stop_requested" : "initial_window_expired"); |
| 911 | return cbm_daemon_runtime_service_stop(service, HOST_RUNTIME_SHUTDOWN_MS); |
| 912 | } |
| 913 | host_http_reconcile_at(host, cbm_now_ms(), false); |
| 914 | /* Retire an ephemeral generation that lingered for cold-storm cohort |
| 915 | * participants once they drain, or once its bounded linger elapses. |
| 916 | * A no-op unless a cohort-participant hook armed a linger. */ |
| 917 | cbm_daemon_runtime_service_reconcile_lifetime(service); |
| 918 | (void)cbm_daemon_runtime_service_wait_exited(service, HOST_WAIT_TICK_MS); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | static bool host_application_shutdown(host_state_t *host) { |
| 923 | if (cbm_daemon_application_shutdown(host->application, HOST_APPLICATION_SHUTDOWN_MS)) { |
no test coverage detected