use super::*;
impl SupervisorSnapshot {
pub(super) fn crash_restarts_in_window(&mut self, window: Duration, now: Instant) -> u32 {
while self
.crash_restarts
.front()
.is_some_and(|oldest| now.duration_since(*oldest) > window)
{
self.crash_restarts.pop_front();
}
u32::try_from(self.crash_restarts.len()).unwrap_or(u32::MAX)
}
pub(super) fn record_crash_restart(&mut self, policy: &RestartPolicy, spawned_at: Instant) {
self.crash_restarts_in_window(policy.window, spawned_at);
self.crash_restarts.push_back(spawned_at);
while self.crash_restarts.len() > policy.max_restarts as usize {
self.crash_restarts.pop_front();
}
self.lifetime_restarts += 1;
}
pub(super) fn next_crash_restart(
&mut self,
policy: &RestartPolicy,
now: Instant,
) -> Option<CrashRestartSchedule> {
let restart_in_window = self.crash_restarts_in_window(policy.window, now);
(restart_in_window < policy.max_restarts).then(|| CrashRestartSchedule {
restart_in_window,
delay: policy.delay_for_restart(restart_in_window),
})
}
}
impl RespawnKind {
pub(super) fn after_exit(schedule: Option<CrashRestartSchedule>) -> Self {
if schedule.is_some() {
Self::CrashRestart
} else {
Self::Spawn
}
}
}
pub(super) async fn spawn_and_mark_running_async(
spec: &ModuleSpec,
runtime: &SupervisorRuntimeConfig,
snapshot: &SharedSnapshot,
) -> Result<SupervisedChild, SuperviseError> {
admit_running_child_async(spec, runtime, snapshot, RespawnKind::Spawn).await
}
pub(super) async fn admit_running_child_async(
spec: &ModuleSpec,
runtime: &SupervisorRuntimeConfig,
snapshot: &SharedSnapshot,
kind: RespawnKind,
) -> Result<SupervisedChild, SuperviseError> {
let activity_spawn = runtime.begin_activity_spawn(&spec.module_id);
let registration_watch = watch_registration(&runtime.registry, spec);
#[cfg(unix)]
let joining_group = containment::current(&runtime.child_roster.containment)
.is_some_and(|container| container.group().is_some());
let result = spawn_child_async(spec, runtime).await;
#[cfg(unix)]
let result = match result {
Err(error)
if joining_group
&& !is_launch_deferral(&error)
&& !runtime.child_roster.is_closed()
&& lock_snapshot(snapshot).is_ok_and(|state| state.enabled)
&& runtime
.child_roster
.previous_instance(&spec.module_id)
.is_none() =>
{
warn!(module_id = %spec.module_id, %error, "carried process-group spawn failed; trying one fresh container");
spawn_child_async(spec, runtime).await
}
other => other,
};
let mut child = result?;
if matches!(kind, RespawnKind::CrashRestart) {
let _ = update_snapshot(snapshot, Some(&spec.module_id), |state| {
state.record_crash_restart(&runtime.restart_policy, child.spawned_at);
});
}
arm_registration_deadline(&mut child, runtime.register_timeout, registration_watch);
if let Err(error) = set_running(snapshot, &child, &spec.module_id, &runtime.spawn_events) {
containment::terminate_blocking(&runtime.child_roster.containment);
return Err(error);
}
if let Some(spawn) = activity_spawn {
spawn.complete(snapshot);
}
Ok(child)
}
const DEFAULT_MAX_RESTARTS: u32 = 3;
const DEFAULT_BACKOFF: Duration = Duration::from_millis(100);
const DEFAULT_MAX_BACKOFF: Duration = Duration::from_secs(30);
const DEFAULT_RESTART_WINDOW: Duration = Duration::from_secs(600);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RestartPolicy {
pub max_restarts: u32,
pub backoff: Duration,
pub max_backoff: Duration,
pub window: Duration,
}
impl RestartPolicy {
pub fn new(max_restarts: u32, backoff: Duration) -> Self {
Self {
max_restarts,
backoff,
max_backoff: DEFAULT_MAX_BACKOFF,
window: DEFAULT_RESTART_WINDOW,
}
}
pub fn with_max_backoff(mut self, max_backoff: Duration) -> Self {
self.max_backoff = max_backoff;
self
}
pub fn with_window(mut self, window: Duration) -> Self {
self.window = window;
self
}
pub(super) fn delay_for_restart(&self, restart_in_window: u32) -> Duration {
if self.backoff.is_zero() || self.max_backoff.is_zero() {
return Duration::ZERO;
}
let mut delay = self.backoff;
for _ in 0..restart_in_window {
if delay >= self.max_backoff {
return self.max_backoff;
}
delay = delay
.checked_mul(10)
.unwrap_or(self.max_backoff)
.min(self.max_backoff);
}
delay.min(self.max_backoff)
}
pub(super) fn budget_exhausted_detail(&self) -> String {
format!(
"crash budget exhausted: max_restarts={} within window_secs={}",
self.max_restarts,
self.window.as_secs()
)
}
}
impl Default for RestartPolicy {
fn default() -> Self {
Self {
max_restarts: DEFAULT_MAX_RESTARTS,
backoff: DEFAULT_BACKOFF,
max_backoff: DEFAULT_MAX_BACKOFF,
window: DEFAULT_RESTART_WINDOW,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct CrashRestartSchedule {
pub(super) restart_in_window: u32,
pub(super) delay: Duration,
}
pub(super) fn daemon_will_restart(
state: &mut SupervisorSnapshot,
policy: &RestartPolicy,
now: Instant,
) -> bool {
state.enabled && state.crash_restarts_in_window(policy.window, now) < policy.max_restarts
}
impl SupervisedModule {
#[cfg(all(test, unix))]
pub(crate) fn seed_crash_budget_for_test(&self, count: u32) {
let policy = self.inner.configuration.lock().unwrap().restart_policy;
let mut state = self.inner.snapshot.lock().unwrap();
for _ in 0..count {
state.record_crash_restart(&policy, Instant::now());
}
}
}
#[allow(clippy::too_many_arguments)]
pub(super) async fn budgeted_restart_child(
spec: &ModuleSpec,
runtime: &SupervisorRuntimeConfig,
registry: &Registry,
process_liveness: &SupervisorProcessLiveness,
snapshot: &SharedSnapshot,
child: &mut Option<SupervisedChild>,
status: SupervisorHealthStatus,
detail: Option<&str>,
now_ms: u64,
health_failure: bool,
) -> Result<(), SuperviseError> {
if let Some(error) = runtime
.config_delivery
.as_ref()
.and_then(|delivery| delivery.0.check_module_hold(&spec.module_id).err())
.filter(|error| error.code != "config_delivery_deferred")
{
return Err(config_delivery::gate_hold_error(
spec,
error,
runtime.config_delivery.as_ref(),
));
}
let recovery_reason = if health_failure {
"health_check"
} else {
"configuration_delivery"
};
let (enabled, schedule) = {
let mut state = lock_snapshot(snapshot)?;
let enabled = state.enabled;
let schedule = if enabled {
state.next_crash_restart(&runtime.restart_policy, Instant::now())
} else {
None
};
(enabled, schedule)
};
if !enabled {
return Err(SuperviseError::Disabled {
module_id: spec.module_id.clone(),
});
}
if schedule.is_none() {
if health_failure {
record_health_action(snapshot, &spec.module_id, "failed".to_string(), now_ms);
}
error!(
module_id = %spec.module_id,
status = ?status,
detail,
max_restarts = runtime.restart_policy.max_restarts,
window_secs = runtime.restart_policy.window.as_secs(),
reason = %runtime.restart_policy.budget_exhausted_detail(),
recovery_reason,
"automatic restart budget exhausted; marking module failed"
);
let stop_notice = begin_forwarding_drain_if_configured(
spec,
runtime,
registry,
snapshot,
Some(true),
RouteCloseReason::Disable,
)
.await?;
update_snapshot(snapshot, Some(&spec.module_id), |state| {
state.drain_disposition_detail = Some(runtime.restart_policy.budget_exhausted_detail());
})?;
let result = drain_optional_child(
&spec.module_id,
spec.protocol,
stop_notice,
registry,
runtime.forwarding.as_deref(),
snapshot,
&runtime.terminal_ring,
&runtime.spawn_events,
child,
runtime.drain_timeout,
ModuleState::Failed,
Some(true),
&runtime.failure_context(true),
)
.await;
update_snapshot(snapshot, Some(&spec.module_id), |state| {
if state.state == ModuleState::Failed && !runtime.child_roster.is_closed() {
state.failed_retry.schedule(&spec.module_id);
}
})?;
result?;
process_liveness.untrack_if_current(&spec.module_id, snapshot);
return Ok(());
}
let schedule = schedule.expect("a health restart must have a crash-restart schedule");
let mut restart_count = 0;
update_snapshot(snapshot, Some(&spec.module_id), |state| {
restart_count = state.crash_restarts.len();
state.state = ModuleState::Unresponsive;
if health_failure {
state.health.status = status;
state.health.last_action = Some(HealthAction::Restart.to_string());
state.health.last_action_ms = Some(now_ms);
}
})?;
warn!(
module_id = %spec.module_id,
status = ?status,
detail,
restart_count,
restart_in_window = schedule.restart_in_window,
delay_ms = schedule.delay.as_millis() as u64,
recovery_reason,
"automatic module restart"
);
let stop_notice = begin_forwarding_drain_if_configured(
spec,
runtime,
registry,
snapshot,
Some(true),
RouteCloseReason::Restart,
)
.await?;
drain_optional_child(
&spec.module_id,
spec.protocol,
stop_notice,
registry,
runtime.forwarding.as_deref(),
snapshot,
&runtime.terminal_ring,
&runtime.spawn_events,
child,
runtime.drain_timeout,
ModuleState::Restarting,
Some(true),
&runtime.failure_context(true),
)
.await?;
schedule_respawn(
runtime,
snapshot,
&spec.module_id,
schedule.delay,
RespawnKind::CrashRestart,
true,
)
}
pub(super) fn schedule_respawn(
runtime: &SupervisorRuntimeConfig,
snapshot: &SharedSnapshot,
module_id: &str,
delay: Duration,
kind: RespawnKind,
daemon_initiated: bool,
) -> Result<(), SuperviseError> {
cancel_deferred_reload(runtime, module_id, "respawn superseded by another restart");
update_snapshot(snapshot, Some(module_id), |state| {
state.respawn_pending = true
})?;
*runtime
.scheduled_respawn
.lock()
.unwrap_or_else(|p| p.into_inner()) = Some(PendingRespawn {
deadline: Instant::now() + delay,
kind,
daemon_initiated,
approval: None,
});
Ok(())
}
pub(super) fn log_crash_respawn(module_id: &str, schedule: CrashRestartSchedule) {
info!(
module_id,
restart_in_window = schedule.restart_in_window,
delay_ms = schedule.delay.as_millis() as u64,
"respawning after crash"
);
}
pub(super) fn respawn_still_pending(snapshot: &SharedSnapshot) -> bool {
matches!(
lock_snapshot(snapshot),
Ok(state) if state.enabled && state.state == ModuleState::Restarting
)
}
pub(super) fn reset_restart_count(
snapshot: &SharedSnapshot,
module_id: &str,
) -> Result<(), SuperviseError> {
update_snapshot(snapshot, Some(module_id), |state| {
state.clear_crash_restarts();
})
}