Skip to content

Commit

Permalink
keep a copy of channel 0 when initiating connetion closure
Browse files Browse the repository at this point in the history
The channel 0 will get removed once the connection has finished closing (as it is still used during the process).
This avoids hitting an error when removing it while we already did.

Fixes #382

Signed-off-by: Marc-Antoine Perennou <Marc-Antoine@Perennou.com>
  • Loading branch information
Keruspe committed Jul 14, 2023
1 parent acd7edc commit 37dbba4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Channels {

pub(crate) fn set_connection_closed(&self, error: Error) {
self.connection_status.set_state(ConnectionState::Closed);
for (id, channel) in self.inner.lock().channels.drain() {
for (id, channel) in self.inner.lock().take_channels().drain() {
self.frames.clear_expected_replies(id, error.clone());
channel.set_closed(error.clone());
}
Expand All @@ -140,7 +140,7 @@ impl Channels {
self.connection_status.set_state(ConnectionState::Error);
self.frames.drop_pending(error.clone());
self.error_handler.on_error(error.clone());
for (id, channel) in self.inner.lock().channels.drain() {
for (id, channel) in self.inner.lock().take_channels().drain() {
self.frames.clear_expected_replies(id, error.clone());
channel.set_connection_error(error.clone());
}
Expand Down Expand Up @@ -369,4 +369,13 @@ impl Inner {
}
Err(Error::ChannelsLimitReached)
}

fn take_channels(&mut self) -> HashMap<ChannelId, Channel> {
let channel0 = self.channels.get(&0).cloned();
let channels = std::mem::take(&mut self.channels);
if let Some(channel0) = channel0 {
self.channels.insert(0, channel0);
}
channels
}
}

0 comments on commit 37dbba4

Please sign in to comment.