Skip to content

Commit

Permalink
fix(ping): Fix panic in WASM caused by retrying on dial upgrade errors
Browse files Browse the repository at this point in the history
This PR workarounds async-rs/futures-timer#74 issue.

Fixes libp2p#5442

Pull-Request: libp2p#5447.
  • Loading branch information
oblique authored Jun 10, 2024
1 parent 75483e8 commit 60e32c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions protocols/ping/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
- Use `web-time` instead of `instant`.
See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347).

- Fix panic in WASM caused by retrying on dial upgrade errors.
See [PR 5447](https://github.com/libp2p/rust-libp2p/pull/5447).

## 0.44.1

- Impose `Sync` on `ping::Failure::Other`.
Expand Down
12 changes: 12 additions & 0 deletions protocols/ping/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ impl Handler {
) {
self.outbound = None; // Request a new substream on the next `poll`.

// Timer is already polled and expired before substream request is initiated
// and will be polled again later on in our `poll` because we reset `self.outbound`.
//
// `futures-timer` allows an expired timer to be polled again and returns
// immediately `Poll::Ready`. However in its WASM implementation there is
// a bug that causes the expired timer to panic.
// This is a workaround until a proper fix is merged and released.
// See libp2p/rust-libp2p#5447 for more info.
//
// TODO: remove when async-rs/futures-timer#74 gets merged.
self.interval.reset(Duration::new(0, 0));

let error = match error {
StreamUpgradeError::NegotiationFailed => {
debug_assert_eq!(self.state, State::Active);
Expand Down

0 comments on commit 60e32c9

Please sign in to comment.