Fixed a bug where buffer saved clocks would desynchronize in rare execution paths

co-authored-by: Max <max@zed.dev>
This commit is contained in:
Mikayla Maki 2023-06-07 16:10:23 -07:00
parent cd63ec2c7f
commit 2c5e83bf72
No known key found for this signature in database
4 changed files with 21 additions and 20 deletions

View File

@ -66,6 +66,7 @@ impl<'a> AddAssign<&'a Local> for Local {
} }
} }
/// A vector clock
#[derive(Clone, Default, Hash, Eq, PartialEq)] #[derive(Clone, Default, Hash, Eq, PartialEq)]
pub struct Global(SmallVec<[u32; 8]>); pub struct Global(SmallVec<[u32; 8]>);

View File

@ -628,12 +628,13 @@ async fn apply_client_operation(
ensure_project_shared(&project, client, cx).await; ensure_project_shared(&project, client, cx).await;
let requested_version = buffer.read_with(cx, |buffer, _| buffer.version()); let requested_version = buffer.read_with(cx, |buffer, _| buffer.version());
let save = project.update(cx, |project, cx| project.save_buffer(buffer, cx)); let save = project.update(cx, |project, cx| project.save_buffer(buffer.clone(), cx));
let save = cx.background().spawn(async move { let save = cx.spawn(|cx| async move {
let (saved_version, _, _) = save save.await
.await
.map_err(|err| anyhow!("save request failed: {:?}", err))?; .map_err(|err| anyhow!("save request failed: {:?}", err))?;
assert!(saved_version.observed_all(&requested_version)); assert!(buffer
.read_with(&cx, |buffer, _| { buffer.saved_version().to_owned() })
.observed_all(&requested_version));
anyhow::Ok(()) anyhow::Ok(())
}); });
if detach { if detach {

View File

@ -37,8 +37,8 @@ use language::{
range_from_lsp, range_to_lsp, Anchor, Bias, Buffer, CachedLspAdapter, CodeAction, CodeLabel, range_from_lsp, range_to_lsp, Anchor, Bias, Buffer, CachedLspAdapter, CodeAction, CodeLabel,
Completion, Diagnostic, DiagnosticEntry, DiagnosticSet, Diff, Event as BufferEvent, File as _, Completion, Diagnostic, DiagnosticEntry, DiagnosticSet, Diff, Event as BufferEvent, File as _,
Language, LanguageRegistry, LanguageServerName, LocalFile, OffsetRangeExt, Operation, Patch, Language, LanguageRegistry, LanguageServerName, LocalFile, OffsetRangeExt, Operation, Patch,
PendingLanguageServer, PointUtf16, RopeFingerprint, TextBufferSnapshot, ToOffset, ToPointUtf16, PendingLanguageServer, PointUtf16, TextBufferSnapshot, ToOffset, ToPointUtf16, Transaction,
Transaction, Unclipped, Unclipped,
}; };
use log::error; use log::error;
use lsp::{ use lsp::{
@ -69,7 +69,7 @@ use std::{
atomic::{AtomicUsize, Ordering::SeqCst}, atomic::{AtomicUsize, Ordering::SeqCst},
Arc, Arc,
}, },
time::{Duration, Instant, SystemTime}, time::{Duration, Instant},
}; };
use terminals::Terminals; use terminals::Terminals;
use util::{ use util::{
@ -1617,7 +1617,7 @@ impl Project {
&self, &self,
buffer: ModelHandle<Buffer>, buffer: ModelHandle<Buffer>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> { ) -> Task<Result<()>> {
let Some(file) = File::from_dyn(buffer.read(cx).file()) else { let Some(file) = File::from_dyn(buffer.read(cx).file()) else {
return Task::ready(Err(anyhow!("buffer doesn't have a file"))); return Task::ready(Err(anyhow!("buffer doesn't have a file")));
}; };
@ -5985,16 +5985,15 @@ impl Project {
.await?; .await?;
let buffer_id = buffer.read_with(&cx, |buffer, _| buffer.remote_id()); let buffer_id = buffer.read_with(&cx, |buffer, _| buffer.remote_id());
let (saved_version, fingerprint, mtime) = this this.update(&mut cx, |this, cx| this.save_buffer(buffer.clone(), cx))
.update(&mut cx, |this, cx| this.save_buffer(buffer, cx))
.await?; .await?;
Ok(proto::BufferSaved { Ok(buffer.read_with(&cx, |buffer, _| proto::BufferSaved {
project_id, project_id,
buffer_id, buffer_id,
version: serialize_version(&saved_version), version: serialize_version(buffer.saved_version()),
mtime: Some(mtime.into()), mtime: Some(buffer.saved_mtime().into()),
fingerprint: language::proto::serialize_fingerprint(fingerprint), fingerprint: language::proto::serialize_fingerprint(buffer.saved_version_fingerprint()),
}) }))
} }
async fn handle_reload_buffers( async fn handle_reload_buffers(

View File

@ -923,7 +923,7 @@ impl LocalWorktree {
path: Arc<Path>, path: Arc<Path>,
has_changed_file: bool, has_changed_file: bool,
cx: &mut ModelContext<Worktree>, cx: &mut ModelContext<Worktree>,
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> { ) -> Task<Result<()>> {
let handle = cx.handle(); let handle = cx.handle();
let buffer = buffer_handle.read(cx); let buffer = buffer_handle.read(cx);
@ -979,7 +979,7 @@ impl LocalWorktree {
buffer.did_save(version.clone(), fingerprint, entry.mtime, cx); buffer.did_save(version.clone(), fingerprint, entry.mtime, cx);
}); });
Ok((version, fingerprint, entry.mtime)) Ok(())
}) })
} }
@ -1290,7 +1290,7 @@ impl RemoteWorktree {
&self, &self,
buffer_handle: ModelHandle<Buffer>, buffer_handle: ModelHandle<Buffer>,
cx: &mut ModelContext<Worktree>, cx: &mut ModelContext<Worktree>,
) -> Task<Result<(clock::Global, RopeFingerprint, SystemTime)>> { ) -> Task<Result<()>> {
let buffer = buffer_handle.read(cx); let buffer = buffer_handle.read(cx);
let buffer_id = buffer.remote_id(); let buffer_id = buffer.remote_id();
let version = buffer.version(); let version = buffer.version();
@ -1315,7 +1315,7 @@ impl RemoteWorktree {
buffer.did_save(version.clone(), fingerprint, mtime, cx); buffer.did_save(version.clone(), fingerprint, mtime, cx);
}); });
Ok((version, fingerprint, mtime)) Ok(())
}) })
} }