File tree Expand file tree Collapse file tree 1 file changed +9
-5
lines changed
Expand file tree Collapse file tree 1 file changed +9
-5
lines changed Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments