From 8d078ed4e22f8198a1aa63bcb5c73b15592a27d6 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 1 Mar 2022 13:08:25 +0100 Subject: [PATCH] Don't block when emitting fs events Blocking could happen while processing events, which would cause the background scanner to never make any further progress. --- crates/project/src/fs.rs | 19 ++++++++++--------- crates/project/src/worktree.rs | 2 +- crates/server/src/rpc.rs | 4 +++- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/crates/project/src/fs.rs b/crates/project/src/fs.rs index 9660312d5b..ec7925685d 100644 --- a/crates/project/src/fs.rs +++ b/crates/project/src/fs.rs @@ -225,7 +225,7 @@ struct FakeFsEntry { struct FakeFsState { entries: std::collections::BTreeMap, next_inode: u64, - events_tx: postage::broadcast::Sender>, + event_txs: Vec>>, } #[cfg(any(test, feature = "test-support"))] @@ -248,8 +248,6 @@ impl FakeFsState { I: IntoIterator, T: Into, { - use postage::prelude::Sink as _; - let events = paths .into_iter() .map(|path| fsevent::Event { @@ -257,9 +255,12 @@ impl FakeFsState { flags: fsevent::StreamFlags::empty(), path: path.into(), }) - .collect(); + .collect::>(); - let _ = self.events_tx.send(events).await; + self.event_txs.retain(|tx| { + let _ = tx.try_send(events.clone()); + !tx.is_closed() + }); } } @@ -273,7 +274,6 @@ pub struct FakeFs { #[cfg(any(test, feature = "test-support"))] impl FakeFs { pub fn new(executor: std::sync::Arc) -> std::sync::Arc { - let (events_tx, _) = postage::broadcast::channel(2); let mut entries = std::collections::BTreeMap::new(); entries.insert( Path::new("/").to_path_buf(), @@ -292,7 +292,7 @@ impl FakeFs { state: futures::lock::Mutex::new(FakeFsState { entries, next_inode: 1, - events_tx, + event_txs: Default::default(), }), }) } @@ -642,9 +642,10 @@ impl Fs for FakeFs { path: &Path, _: Duration, ) -> Pin>>> { - let state = self.state.lock().await; + let mut state = self.state.lock().await; self.simulate_random_delay().await; - let rx = state.events_tx.subscribe(); + let (tx, rx) = smol::channel::unbounded(); + state.event_txs.push(tx); let path = path.to_path_buf(); Box::pin(futures::StreamExt::filter(rx, move |events| { let result = events.iter().any(|event| event.path.starts_with(&path)); diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index fe9958b9cd..3e2ad5ce20 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -386,7 +386,7 @@ impl Worktree { if worktree.poll_task.is_none() { worktree.poll_task = Some(cx.spawn(|this, mut cx| async move { if is_fake_fs { - smol::future::yield_now().await; + cx.background().simulate_random_delay().await; } else { smol::Timer::after(Duration::from_millis(100)).await; } diff --git a/crates/server/src/rpc.rs b/crates/server/src/rpc.rs index db324a30fc..500e99ef7c 100644 --- a/crates/server/src/rpc.rs +++ b/crates/server/src/rpc.rs @@ -4185,7 +4185,9 @@ mod tests { while operations.get() < max_operations { cx.background().simulate_random_delay().await; - if clients.len() < max_peers && rng.lock().gen_bool(0.05) { + if clients.len() >= max_peers { + break; + } else if rng.lock().gen_bool(0.05) { operations.set(operations.get() + 1); let guest_id = clients.len();