File tree 1 file changed +9
-5
lines changed
1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -81,7 +81,14 @@ impl Iroh {
81
81
msg_id : MsgId ,
82
82
) -> Result < Option < JoinTopicFut > > {
83
83
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) {
85
92
if channel_state. subscribe_loop . is_some ( ) {
86
93
return Ok ( None ) ;
87
94
}
@@ -115,10 +122,7 @@ impl Iroh {
115
122
}
116
123
} ) ;
117
124
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) ) ;
122
126
123
127
Ok ( Some ( connect_future) )
124
128
}
You can’t perform that action at this time.
0 commit comments