Skip to content

Commit d57552c

Browse files
committed
Fix crash if the watchdog is not set
1 parent e3e6537 commit d57552c

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

crates/cli/src/lifecycle.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use std::{future::Future, process::ExitCode, time::Duration};
77

8-
use futures_util::future::BoxFuture;
8+
use futures_util::future::{BoxFuture, Either};
99
use mas_handlers::ActivityTracker;
1010
use mas_templates::Templates;
1111
use tokio::signal::unix::{Signal, SignalKind};
@@ -131,12 +131,28 @@ impl LifecycleManager {
131131
pub async fn run(mut self) -> ExitCode {
132132
notify(&[sd_notify::NotifyState::Ready]);
133133

134-
let mut watchdog_usec = 0;
135-
let watchdog_enabled = sd_notify::watchdog_enabled(false, &mut watchdog_usec);
136-
let mut watchdog_interval = tokio::time::interval(Duration::from_micros(watchdog_usec / 2));
134+
// This will be `Some` if we have the watchdog enabled, and `None` if not
135+
let mut watchdog_interval = {
136+
let mut watchdog_usec = 0;
137+
if sd_notify::watchdog_enabled(false, &mut watchdog_usec) {
138+
Some(tokio::time::interval(Duration::from_micros(
139+
watchdog_usec / 2,
140+
)))
141+
} else {
142+
None
143+
}
144+
};
137145

138146
// Wait for a first shutdown signal and trigger the soft shutdown
139147
let likely_crashed = loop {
148+
// This makes a Future that will either yield the watchdog tick if enabled, or a
149+
// pending Future if not
150+
let watchdog_tick = if let Some(watchdog_interval) = &mut watchdog_interval {
151+
Either::Left(watchdog_interval.tick())
152+
} else {
153+
Either::Right(futures_util::future::pending())
154+
};
155+
140156
tokio::select! {
141157
() = self.soft_shutdown_token.cancelled() => {
142158
tracing::warn!("Another task triggered a shutdown, it likely crashed! Shutting down");
@@ -153,7 +169,7 @@ impl LifecycleManager {
153169
break false;
154170
},
155171

156-
_ = watchdog_interval.tick(), if watchdog_enabled => {
172+
_ = watchdog_tick => {
157173
notify(&[
158174
sd_notify::NotifyState::Watchdog,
159175
]);

0 commit comments

Comments
 (0)