Skip to content

Commit 9e14e69

Browse files
committed
fix: acquire write lock on iroh_channels before checking for subscribe_loop
1 parent 9922769 commit 9e14e69

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/peer_channels.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ impl Iroh {
8181
msg_id: MsgId,
8282
) -> Result<Option<JoinTopicFut>> {
8383
let topic = get_iroh_topic_for_msg(ctx, msg_id).await?;
84-
let seq = if let Some(channel_state) = self.iroh_channels.read().await.get(&topic) {
84+
85+
// Take exclusive lock to make sure
86+
// no other thread can create a second gossip subscription
87+
// after we check that it does not exist and before we create a new one.
88+
// Otherwise we would receive every message twice or more times.
89+
let mut iroh_channels = self.iroh_channels.write().await;
90+
91+
let seq = if let Some(channel_state) = iroh_channels.get(&topic) {
8592
if channel_state.subscribe_loop.is_some() {
8693
return Ok(None);
8794
}
@@ -115,10 +122,7 @@ impl Iroh {
115122
}
116123
});
117124

118-
self.iroh_channels
119-
.write()
120-
.await
121-
.insert(topic, ChannelState::new(seq, subscribe_loop));
125+
iroh_channels.insert(topic, ChannelState::new(seq, subscribe_loop));
122126

123127
Ok(Some(connect_future))
124128
}

0 commit comments

Comments
 (0)