node: Protect against non-monotonic clocks

Many (most?) system clocks are not monotonic, and even though we're
getting a `LocalTime`, which *is* monotonic when using `now()`, it is
constructed from a `SystemTime` which isn't.
This commit is contained in:
cloudhead 2024-04-30 10:56:37 +02:00
parent bb5355fcb1
commit e5df616ead
No known key found for this signature in database
2 changed files with 13 additions and 3 deletions

4
Cargo.lock generated
View File

@ -1736,9 +1736,9 @@ checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c"
[[package]] [[package]]
name = "localtime" name = "localtime"
version = "1.3.0" version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71c67b83b03434bb31132aef0b314b8a49a0db55ce195c7e3c29d27bbf003819" checksum = "016a009e0bb8ba6e3229fb74bf11a8fe6ef24542cc6ef35ef38863ac13f96d87"
dependencies = [ dependencies = [
"serde", "serde",
] ]

View File

@ -676,7 +676,17 @@ where
"Tick +{}", "Tick +{}",
now - self.started_at.expect("Service::tick: service must be initialized") now - self.started_at.expect("Service::tick: service must be initialized")
); );
if now >= self.clock {
self.clock = now; self.clock = now;
} else {
// Nb. In tests, we often move the clock forwards in time to test different behaviors,
// so this warning isn't applicable there.
#[cfg(not(test))]
warn!(
target: "service",
"System clock is not monotonic: {now} is not greater or equal to {}", self.clock
);
}
} }
pub fn wake(&mut self) { pub fn wake(&mut self) {