From 20c4b19989b2871b6adf6e77b8fdfacfabc7300d Mon Sep 17 00:00:00 2001
From: MOZGIII <mike-n@narod.ru>
Date: Sat, 6 Apr 2024 06:21:58 -0300
Subject: [PATCH] fix(mdns): don't use Duration::MAX on TokioTimer's

at() function. Closes #5296
---
 Cargo.lock                            |  2 +-
 Cargo.toml                            |  2 +-
 protocols/mdns/CHANGELOG.md           |  4 ++++
 protocols/mdns/Cargo.toml             |  2 +-
 protocols/mdns/src/behaviour/timer.rs | 19 +++++++++++++++++--
 5 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 579416ac740..c01d163f966 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2935,7 +2935,7 @@ dependencies = [
 
 [[package]]
 name = "libp2p-mdns"
-version = "0.45.1"
+version = "0.45.2"
 dependencies = [
  "async-io 2.3.2",
  "async-std",
diff --git a/Cargo.toml b/Cargo.toml
index c1f39ec74c5..147cf3d106d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -85,7 +85,7 @@ libp2p-gossipsub = { version = "0.46.1", path = "protocols/gossipsub" }
 libp2p-identify = { version = "0.44.2", path = "protocols/identify" }
 libp2p-identity = { version = "0.2.8" }
 libp2p-kad = { version = "0.46.0", path = "protocols/kad" }
-libp2p-mdns = { version = "0.45.1", path = "protocols/mdns" }
+libp2p-mdns = { version = "0.45.2", path = "protocols/mdns" }
 libp2p-memory-connection-limits = { version = "0.2.0", path = "misc/memory-connection-limits" }
 libp2p-metrics = { version = "0.14.1", path = "misc/metrics" }
 libp2p-mplex = { version = "0.41.0", path = "muxers/mplex" }
diff --git a/protocols/mdns/CHANGELOG.md b/protocols/mdns/CHANGELOG.md
index cfd02232b07..a8cecd14eca 100644
--- a/protocols/mdns/CHANGELOG.md
+++ b/protocols/mdns/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.45.2 - unreleased
+- Fix `TokioTimer::at` panic.
+  See [PR 5297](https://github.com/libp2p/rust-libp2p/pull/5297).
+
 ## 0.45.1
 
 - Ensure `Multiaddr` handled and returned by `Behaviour` are `/p2p` terminated.
diff --git a/protocols/mdns/Cargo.toml b/protocols/mdns/Cargo.toml
index 3d40e2dfe71..160da64484a 100644
--- a/protocols/mdns/Cargo.toml
+++ b/protocols/mdns/Cargo.toml
@@ -2,7 +2,7 @@
 name = "libp2p-mdns"
 edition = "2021"
 rust-version = { workspace = true }
-version = "0.45.1"
+version = "0.45.2"
 description = "Implementation of the libp2p mDNS discovery method"
 authors = ["Parity Technologies <admin@parity.io>"]
 license = "MIT"
diff --git a/protocols/mdns/src/behaviour/timer.rs b/protocols/mdns/src/behaviour/timer.rs
index 4f6ceec306d..6536a080b0e 100644
--- a/protocols/mdns/src/behaviour/timer.rs
+++ b/protocols/mdns/src/behaviour/timer.rs
@@ -95,10 +95,12 @@ pub(crate) mod tokio {
     pub(crate) type TokioTimer = Timer<Interval>;
     impl Builder for TokioTimer {
         fn at(instant: Instant) -> Self {
-            // Taken from: https://docs.rs/async-io/1.7.0/src/async_io/lib.rs.html#91
             let mut inner = time::interval_at(
                 TokioInstant::from_std(instant),
-                Duration::new(std::u64::MAX, 1_000_000_000 - 1),
+                // Setting this to Duration::MAX panics,
+                // see https://github.com/libp2p/rust-libp2p/issues/5296, so we set it to a millenium,
+                // which while still impossible to reach is far from Duration::MAX.
+                Duration::from_secs(31_560_000_000),
             );
             inner.set_missed_tick_behavior(MissedTickBehavior::Skip);
             Self { inner }
@@ -128,4 +130,17 @@ pub(crate) mod tokio {
             (std::usize::MAX, None)
         }
     }
+
+    #[cfg(test)]
+    mod tests {
+        use futures::{future::poll_fn, StreamExt};
+
+        use super::Builder;
+
+        #[tokio::test]
+        async fn timer_does_not_panic() {
+            let mut timer = super::TokioTimer::at(std::time::Instant::now());
+            poll_fn(|cx| timer.poll_next_unpin(cx)).await;
+        }
+    }
 }