Skip to content

Commit

Permalink
fix: acquire write lock on iroh_channels before checking for subscrib…
Browse files Browse the repository at this point in the history
…e_loop
  • Loading branch information
link2xt committed May 25, 2024
1 parent 9922769 commit 9e14e69
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/peer_channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ impl Iroh {
msg_id: MsgId,
) -> Result<Option<JoinTopicFut>> {
let topic = get_iroh_topic_for_msg(ctx, msg_id).await?;
let seq = if let Some(channel_state) = self.iroh_channels.read().await.get(&topic) {

// Take exclusive lock to make sure
// no other thread can create a second gossip subscription
// after we check that it does not exist and before we create a new one.
// Otherwise we would receive every message twice or more times.
let mut iroh_channels = self.iroh_channels.write().await;

let seq = if let Some(channel_state) = iroh_channels.get(&topic) {
if channel_state.subscribe_loop.is_some() {
return Ok(None);
}
Expand Down Expand Up @@ -115,10 +122,7 @@ impl Iroh {
}
});

self.iroh_channels
.write()
.await
.insert(topic, ChannelState::new(seq, subscribe_loop));
iroh_channels.insert(topic, ChannelState::new(seq, subscribe_loop));

Ok(Some(connect_future))
}
Expand Down

0 comments on commit 9e14e69

Please sign in to comment.