Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: instant panic #349

Merged
merged 2 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ p2p = { path = "../tentacle", package = "tentacle" }
rand = "0.8"
futures = { version = "0.3.0" }
tokio = { version = "1.0.0", features = ["time", "io-util", "net", "rt-multi-thread"] }
tokio-util = { version = "0.6.0", features = ["codec"] }
tokio-util = { version = "0.7.0", features = ["codec"] }
crossbeam-channel = "0.5"
env_logger = "0.6.0"
bytes = "1.0.0"
4 changes: 2 additions & 2 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tentacle-secio = { path = "../secio" }
tokio-yamux = { path = "../yamux" }
rand = "0.8"
bytes = "1.0.0"
tokio-util = { version = "0.6.0", features = ["codec"] }
tokio-util = { version = "0.7.0", features = ["codec"] }

# Prevent this from interfering with workspaces
[workspace]
Expand All @@ -30,4 +30,4 @@ path = "fuzz_targets/secio/crypto/encrypt_cipher.rs"

[[bin]]
name = "yamux_frame_codec"
path = "fuzz_targets/yamux/frame_codec.rs"
path = "fuzz_targets/yamux/frame_codec.rs"
2 changes: 1 addition & 1 deletion secio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ no-default-features = true
bytes = "1.0"
futures = { version = "0.3.0" }
tokio = { version = "1.0", features = ["io-util"] }
tokio-util = { version = "0.6.0", features = ["codec"] }
tokio-util = { version = "0.7.0", features = ["codec"] }
log = "0.4.1"

molecule = "0.7.0"
Expand Down
6 changes: 3 additions & 3 deletions tentacle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ secio = { path = "../secio", version = "0.5.0", package = "tentacle-secio" }

futures = { version = "0.3.0" }
tokio = { version = "1.0.0", features = ["macros"] }
tokio-util = { version = "0.6.0", features = ["codec"] }
tokio-util = { version = "0.7.0", features = ["codec"] }
async-trait = "0.1"
log = "0.4"
bytes = "1.0.0"
thiserror = "1.0"
once_cell = "1.0"
nohash-hasher = "0.2"

parking_lot = { version = "0.11", optional = true }
parking_lot = { version = "0.12", optional = true }
tokio-tungstenite = { version = "0.16", optional = true }
futures-timer = { version = "3.0.2", optional = true }
async-std = { version = "1", features = ["unstable"], optional = true }
Expand Down Expand Up @@ -66,7 +66,7 @@ env_logger = "0.6.0"
crossbeam-channel = "0.5"
systemstat = "0.1.3"
futures-test = "0.3.5"
rustls-pemfile = "0.2"
rustls-pemfile = "0.3"

[target.'cfg(unix)'.dev-dependencies]
nix = "0.23.0"
Expand Down
4 changes: 2 additions & 2 deletions tentacle/src/runtime/async_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ mod os {
/// this will force users to ensure that they are used in an async environment
recv: Receiver<io::Result<(AsyncStream, SocketAddr)>>,
local_addr: SocketAddr,
close_sender: Sender<()>,
_close_sender: Sender<()>,
}

impl TcpListener {
Expand All @@ -63,7 +63,7 @@ mod os {
TcpListener {
recv: rx,
local_addr,
close_sender: tx_c,
_close_sender: tx_c,
}
}

Expand Down
4 changes: 3 additions & 1 deletion tentacle/src/upnp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ impl IgdClient {
pub fn process_only_leases_support(&mut self) {
for (addr, interval) in self.leases.iter_mut() {
let register = interval
.map(|inner| inner.elapsed() > Duration::from_secs(40))
.map(|inner| {
Instant::now().saturating_duration_since(inner) > Duration::from_secs(40)
})
.unwrap_or(true);

if register {
Expand Down
2 changes: 1 addition & 1 deletion yamux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ edition = "2021"
bytes = "1.0.0"
futures = { version = "0.3.0" }
tokio = { version = "1.0.0" }
tokio-util = { version = "0.6.0", features = ["codec"] }
tokio-util = { version = "0.7.0", features = ["codec"] }
log = "0.4"
nohash-hasher = "0.2"

Expand Down
32 changes: 17 additions & 15 deletions yamux/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const TIMEOUT: Duration = Duration::from_secs(30);
/// So, I implemented a global time dependent on `futures-timer`,
/// Because in the browser environment, it is always single-threaded, so feel free to be unsafe
#[cfg(target_arch = "wasm32")]
static mut TIME: Instant = Instant::from_f64(0.0);
static mut TIME: Instant = Instant::from_u64(0);

/// The session
pub struct Session<T> {
Expand Down Expand Up @@ -276,7 +276,7 @@ where
if self
.pings
.iter()
.any(|(_id, time)| time.elapsed() > TIMEOUT)
.any(|(_id, time)| Instant::now().saturating_duration_since(*time) > TIMEOUT)
{
return Err(io::ErrorKind::TimedOut.into());
}
Expand Down Expand Up @@ -794,7 +794,7 @@ mod timer {
#[derive(Debug, Copy, Clone)]
pub struct Instant {
/// mock
inner: f64,
inner: u64,
}

impl PartialEq for Instant {
Expand All @@ -820,7 +820,7 @@ mod timer {
}

impl Instant {
pub const fn from_f64(val: f64) -> Self {
pub const fn from_u64(val: u64) -> Self {
Instant { inner: val }
}

Expand All @@ -832,6 +832,10 @@ mod timer {
*self - earlier
}

pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
*self - earlier
}

pub fn elapsed(&self) -> Duration {
Instant::now() - *self
}
Expand All @@ -841,31 +845,29 @@ mod timer {
type Output = Instant;

fn add(self, other: Duration) -> Instant {
let new_val = self.inner + other.as_millis() as f64;
Instant {
inner: new_val as f64,
}
let new_val = self.inner + other.as_millis() as u64;
Instant { inner: new_val }
}
}

impl Sub<Duration> for Instant {
type Output = Instant;

fn sub(self, other: Duration) -> Instant {
let new_val = self.inner - other.as_millis() as f64;
Instant {
inner: new_val as f64,
}
let new_val = self
.inner
.checked_sub(other.as_millis() as u64)
.unwrap_or_default();
Instant { inner: new_val }
}
}

impl Sub<Instant> for Instant {
type Output = Duration;

fn sub(self, other: Instant) -> Duration {
let ms = self.inner - other.inner;
assert!(ms >= 0.0);
Duration::from_millis(ms as u64)
let ms = self.inner.checked_sub(other.inner).unwrap_or_default();
Duration::from_millis(ms)
}
}

Expand Down