Skip to content

Commit b4aef44

Browse files
committed
fix(spaces): address potential race when setting up the room updates listener
1 parent 7942759 commit b4aef44

File tree

1 file changed

+7
-4
lines changed
  • crates/matrix-sdk-ui/src/spaces

1 file changed

+7
-4
lines changed

crates/matrix-sdk-ui/src/spaces/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use ruma::{
4343
space::{child::SpaceChildEventContent, parent::SpaceParentEventContent},
4444
},
4545
};
46+
use tokio::sync::Mutex as AsyncMutex;
4647
use tracing::error;
4748

4849
use crate::spaces::graph::SpaceGraph;
@@ -95,7 +96,7 @@ pub struct SpaceService {
9596

9697
joined_spaces: Arc<Mutex<ObservableVector<SpaceRoom>>>,
9798

98-
room_update_handle: Mutex<Option<AbortOnDrop<()>>>,
99+
room_update_handle: AsyncMutex<Option<AbortOnDrop<()>>>,
99100
}
100101

101102
impl SpaceService {
@@ -104,7 +105,7 @@ impl SpaceService {
104105
Self {
105106
client,
106107
joined_spaces: Arc::new(Mutex::new(ObservableVector::new())),
107-
room_update_handle: Mutex::new(None),
108+
room_update_handle: AsyncMutex::new(None),
108109
}
109110
}
110111

@@ -113,12 +114,14 @@ impl SpaceService {
113114
pub async fn subscribe_to_joined_spaces(
114115
&self,
115116
) -> (Vector<SpaceRoom>, VectorSubscriberBatchedStream<SpaceRoom>) {
116-
if self.room_update_handle.lock().is_none() {
117+
let mut room_update_handle = self.room_update_handle.lock().await;
118+
119+
if room_update_handle.is_none() {
117120
let client = self.client.clone();
118121
let joined_spaces = Arc::clone(&self.joined_spaces);
119122
let all_room_updates_receiver = self.client.subscribe_to_all_room_updates();
120123

121-
*self.room_update_handle.lock() = Some(AbortOnDrop::new(spawn(async move {
124+
*room_update_handle = Some(AbortOnDrop::new(spawn(async move {
122125
pin_mut!(all_room_updates_receiver);
123126

124127
loop {

0 commit comments

Comments
 (0)