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(tcp): remove correct listener inTransport::remove_listener #3387

Merged
merged 3 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions transports/tcp/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# 0.39.0 [unreleased]

- Update to `libp2p-core` `v0.39.0`.
- Fix a bug where we removed any other listener in `Transport::remove_listener` except for the one with the provided `ListenerId`. See [PR 3387].

[PR 3387]: https://github.com/libp2p/rust-libp2p/pull/3387

# 0.38.0

Expand Down
29 changes: 28 additions & 1 deletion transports/tcp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ where
}

fn remove_listener(&mut self, id: ListenerId) -> bool {
if let Some(index) = self.listeners.iter().position(|l| l.listener_id != id) {
if let Some(index) = self.listeners.iter().position(|l| l.listener_id == id) {
self.listeners.remove(index);
self.pending_events
.push_back(TransportEvent::ListenerClosed {
Expand Down Expand Up @@ -1335,4 +1335,31 @@ mod tests {
.address_translation(&quic_addr, &tcp_observed_addr)
.is_none());
}

#[test]
fn test_remove_listener() {
env_logger::try_init().ok();

async fn cycle_listeners<T: Provider>() -> bool {
let mut tcp = Transport::<T>::default().boxed();
let listener_id = tcp
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
.unwrap();
tcp.remove_listener(listener_id)
}

#[cfg(feature = "async-io")]
{
assert!(async_std::task::block_on(cycle_listeners::<async_io::Tcp>()));
}

#[cfg(feature = "tokio")]
{
let rt = ::tokio::runtime::Builder::new_current_thread()
.enable_io()
.build()
.unwrap();
assert!(rt.block_on(cycle_listeners::<tokio::Tcp>()));
}
}
}