Skip to content

Commit

Permalink
fix(mdns): don't use Duration::MAX on TokioTimer's
Browse files Browse the repository at this point in the history
at() function. Closes #5296
  • Loading branch information
MOZGIII authored and jxs committed Apr 15, 2024
1 parent 47e19f7 commit 20c4b19
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 4 additions & 0 deletions protocols/mdns/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion protocols/mdns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
license = "MIT"
Expand Down
19 changes: 17 additions & 2 deletions protocols/mdns/src/behaviour/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 20c4b19

Please sign in to comment.