Skip to content

Commit 39f03d6

Browse files
authored
Merge pull request #349 from nervosnetwork/instant-panic
fix: instant panic
2 parents 0ef6f61 + 4819328 commit 39f03d6

File tree

9 files changed

+38
-29
lines changed

9 files changed

+38
-29
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.4.0-beta.4
2+
3+
### Bug Fix
4+
- Fix `Instant` may panic on `duration_since, elapsed, sub`(#349, also patch to 0.4.0-alpha.3)
5+
16
## 0.4.0-beta.3
27

38
### Features

bench/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ p2p = { path = "../tentacle", package = "tentacle" }
1616
rand = "0.8"
1717
futures = { version = "0.3.0" }
1818
tokio = { version = "1.0.0", features = ["time", "io-util", "net", "rt-multi-thread"] }
19-
tokio-util = { version = "0.6.0", features = ["codec"] }
19+
tokio-util = { version = "0.7.0", features = ["codec"] }
2020
crossbeam-channel = "0.5"
2121
env_logger = "0.6.0"
2222
bytes = "1.0.0"

fuzz/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tentacle-secio = { path = "../secio" }
1414
tokio-yamux = { path = "../yamux" }
1515
rand = "0.8"
1616
bytes = "1.0.0"
17-
tokio-util = { version = "0.6.0", features = ["codec"] }
17+
tokio-util = { version = "0.7.0", features = ["codec"] }
1818

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

3131
[[bin]]
3232
name = "yamux_frame_codec"
33-
path = "fuzz_targets/yamux/frame_codec.rs"
33+
path = "fuzz_targets/yamux/frame_codec.rs"

secio/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tentacle-secio"
3-
version = "0.5.2"
3+
version = "0.5.3"
44
license = "MIT"
55
description = "Secio encryption protocol for p2p"
66
authors = ["piaoliu <driftluo@foxmail.com>", "Nervos Core Dev <dev@nervos.org>"]
@@ -19,7 +19,7 @@ no-default-features = true
1919
bytes = "1.0"
2020
futures = { version = "0.3.0" }
2121
tokio = { version = "1.0", features = ["io-util"] }
22-
tokio-util = { version = "0.6.0", features = ["codec"] }
22+
tokio-util = { version = "0.7.0", features = ["codec"] }
2323
log = "0.4.1"
2424

2525
molecule = "0.7.0"

tentacle/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tentacle"
3-
version = "0.4.0-beta.3"
3+
version = "0.4.0-beta.4"
44
license = "MIT"
55
description = "Minimal implementation for a multiplexed p2p network framework."
66
authors = ["piaoliu <driftluo@foxmail.com>", "Nervos Core Dev <dev@nervos.org>"]
@@ -22,15 +22,15 @@ secio = { path = "../secio", version = "0.5.0", package = "tentacle-secio" }
2222

2323
futures = { version = "0.3.0" }
2424
tokio = { version = "1.0.0", features = ["macros"] }
25-
tokio-util = { version = "0.6.0", features = ["codec"] }
25+
tokio-util = { version = "0.7.0", features = ["codec"] }
2626
async-trait = "0.1"
2727
log = "0.4"
2828
bytes = "1.0.0"
2929
thiserror = "1.0"
3030
once_cell = "1.0"
3131
nohash-hasher = "0.2"
3232

33-
parking_lot = { version = "0.11", optional = true }
33+
parking_lot = { version = "0.12", optional = true }
3434
tokio-tungstenite = { version = "0.16", optional = true }
3535
futures-timer = { version = "3.0.2", optional = true }
3636
async-std = { version = "1", features = ["unstable"], optional = true }
@@ -66,7 +66,7 @@ env_logger = "0.6.0"
6666
crossbeam-channel = "0.5"
6767
systemstat = "0.1.3"
6868
futures-test = "0.3.5"
69-
rustls-pemfile = "0.2"
69+
rustls-pemfile = "0.3"
7070

7171
[target.'cfg(unix)'.dev-dependencies]
7272
nix = "0.23.0"

tentacle/src/runtime/async_runtime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ mod os {
4545
/// this will force users to ensure that they are used in an async environment
4646
recv: Receiver<io::Result<(AsyncStream, SocketAddr)>>,
4747
local_addr: SocketAddr,
48-
close_sender: Sender<()>,
48+
_close_sender: Sender<()>,
4949
}
5050

5151
impl TcpListener {
@@ -63,7 +63,7 @@ mod os {
6363
TcpListener {
6464
recv: rx,
6565
local_addr,
66-
close_sender: tx_c,
66+
_close_sender: tx_c,
6767
}
6868
}
6969

tentacle/src/upnp/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ impl IgdClient {
142142
pub fn process_only_leases_support(&mut self) {
143143
for (addr, interval) in self.leases.iter_mut() {
144144
let register = interval
145-
.map(|inner| inner.elapsed() > Duration::from_secs(40))
145+
.map(|inner| {
146+
Instant::now().saturating_duration_since(inner) > Duration::from_secs(40)
147+
})
146148
.unwrap_or(true);
147149

148150
if register {

yamux/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tokio-yamux"
3-
version = "0.3.4"
3+
version = "0.3.5"
44
license = "MIT"
55
repository = "https://github.com/nervosnetwork/tentacle"
66
description = "Rust implementation of Yamux"
@@ -11,7 +11,7 @@ edition = "2021"
1111
bytes = "1.0.0"
1212
futures = { version = "0.3.0" }
1313
tokio = { version = "1.0.0" }
14-
tokio-util = { version = "0.6.0", features = ["codec"] }
14+
tokio-util = { version = "0.7.0", features = ["codec"] }
1515
log = "0.4"
1616
nohash-hasher = "0.2"
1717

yamux/src/session.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const TIMEOUT: Duration = Duration::from_secs(30);
4040
/// So, I implemented a global time dependent on `futures-timer`,
4141
/// Because in the browser environment, it is always single-threaded, so feel free to be unsafe
4242
#[cfg(target_arch = "wasm32")]
43-
static mut TIME: Instant = Instant::from_f64(0.0);
43+
static mut TIME: Instant = Instant::from_u64(0);
4444

4545
/// The session
4646
pub struct Session<T> {
@@ -276,7 +276,7 @@ where
276276
if self
277277
.pings
278278
.iter()
279-
.any(|(_id, time)| time.elapsed() > TIMEOUT)
279+
.any(|(_id, time)| Instant::now().saturating_duration_since(*time) > TIMEOUT)
280280
{
281281
return Err(io::ErrorKind::TimedOut.into());
282282
}
@@ -794,7 +794,7 @@ mod timer {
794794
#[derive(Debug, Copy, Clone)]
795795
pub struct Instant {
796796
/// mock
797-
inner: f64,
797+
inner: u64,
798798
}
799799

800800
impl PartialEq for Instant {
@@ -820,7 +820,7 @@ mod timer {
820820
}
821821

822822
impl Instant {
823-
pub const fn from_f64(val: f64) -> Self {
823+
pub const fn from_u64(val: u64) -> Self {
824824
Instant { inner: val }
825825
}
826826

@@ -832,6 +832,10 @@ mod timer {
832832
*self - earlier
833833
}
834834

835+
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
836+
*self - earlier
837+
}
838+
835839
pub fn elapsed(&self) -> Duration {
836840
Instant::now() - *self
837841
}
@@ -841,31 +845,29 @@ mod timer {
841845
type Output = Instant;
842846

843847
fn add(self, other: Duration) -> Instant {
844-
let new_val = self.inner + other.as_millis() as f64;
845-
Instant {
846-
inner: new_val as f64,
847-
}
848+
let new_val = self.inner + other.as_millis() as u64;
849+
Instant { inner: new_val }
848850
}
849851
}
850852

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

854856
fn sub(self, other: Duration) -> Instant {
855-
let new_val = self.inner - other.as_millis() as f64;
856-
Instant {
857-
inner: new_val as f64,
858-
}
857+
let new_val = self
858+
.inner
859+
.checked_sub(other.as_millis() as u64)
860+
.unwrap_or_default();
861+
Instant { inner: new_val }
859862
}
860863
}
861864

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

865868
fn sub(self, other: Instant) -> Duration {
866-
let ms = self.inner - other.inner;
867-
assert!(ms >= 0.0);
868-
Duration::from_millis(ms as u64)
869+
let ms = self.inner.checked_sub(other.inner).unwrap_or_default();
870+
Duration::from_millis(ms)
869871
}
870872
}
871873

0 commit comments

Comments
 (0)