diff --git a/crates/notebook/src/live/share/batch.rs b/crates/notebook/src/live/share/batch.rs index ae5d2228e18bed5bf836345732550277e7858d3e..2ebda7fc81f2ba7b64b199499759f351d195e73f 100644 --- a/crates/notebook/src/live/share/batch.rs +++ b/crates/notebook/src/live/share/batch.rs @@ -267,7 +267,15 @@ impl Served { .find_map(|(held, at, image)| { (held == path && *at == stamp).then(|| Arc::clone(image)) }) - .ok_or_else(|| retry("The batch's base is no longer held"))?; + .ok_or_else(|| { + Error::Remote(CommitError { + state: CommitState::NotCommitted, + error: io::Error::new( + io::ErrorKind::StaleNetworkFileHandle, + "The batch's base is no longer held", + ), + }) + })?; let old_arena = Arena::default(); let mut old = Section::open(&old_arena, (*base).clone())?; if old.root() != next.root() { diff --git a/crates/notebook/src/live/wire.rs b/crates/notebook/src/live/wire.rs index 8cbb9070a8773a71ab170e41977d5aa3cdaf4dcb..501dc3ca53cf65907327eca4f122a46576dae22a 100644 --- a/crates/notebook/src/live/wire.rs +++ b/crates/notebook/src/live/wire.rs @@ -403,7 +403,7 @@ pub struct WireEntry { } /// The `io::ErrorKind`s a failure names, by number; any other is `Other`. -const ERROR_KINDS: [io::ErrorKind; 20] = [ +const ERROR_KINDS: [io::ErrorKind; 21] = [ io::ErrorKind::Other, io::ErrorKind::NotFound, io::ErrorKind::PermissionDenied, @@ -424,6 +424,7 @@ const ERROR_KINDS: [io::ErrorKind; 20] = [ io::ErrorKind::UnexpectedEof, io::ErrorKind::Interrupted, io::ErrorKind::BrokenPipe, + io::ErrorKind::StaleNetworkFileHandle, ]; pub fn error_number(kind: io::ErrorKind) -> u16 { diff --git a/crates/notebook/src/sync.rs b/crates/notebook/src/sync.rs index 0ecc05109ca7d86f6d3a6daad75ceba040d5cec8..e1b22f0168d6f631e6ac7420b764a5d4e9e625ed 100644 --- a/crates/notebook/src/sync.rs +++ b/crates/notebook/src/sync.rs @@ -28,7 +28,8 @@ pub trait Remote { false } /// Where the remote accepts edits, a batch published again with the same revisions, - /// however its first attempt ended, lands at most once. + /// however its first attempt ended, lands at most once; one refused as + /// `StaleNetworkFileHandle` never can, as the remote no longer holds its base. fn publish_edits( &mut self, transaction: &Transaction, @@ -463,7 +464,7 @@ impl Replica { /// Publishes the attempted `batch` sealed as `transaction`, rebasing on the remote's /// image after where `batched`; `changed` holds the pages a rebase already changed. An /// attempt made `again` stays uncertain until the remote takes it, as the first may - /// still land. + /// still land. A batch whose base the remote refuses for good is rebased onto its image. async fn publish_sealed( &self, remote: &mut impl Remote, @@ -494,6 +495,18 @@ impl Replica { }; match published { Ok(()) => {} + Err(error) + if error.state == CommitState::NotCommitted + && error.error.kind() == io::ErrorKind::StaleNetworkFileHandle => + { + self.lock()? + .execute("UPDATE batches SET attempted=0 WHERE id=?1", [batch])?; + let image = awaited(remote, Remote::read) + .await + .map_err(Error::RemoteIo)?; + self.rebase(Some(image))?; + return Err(error.into()); + } Err(error) if error.state == CommitState::NotCommitted && !again => { self.lock()? .execute("UPDATE batches SET attempted=0 WHERE id=?1", [batch])?; diff --git a/crates/notebook/tests/live_batch.rs b/crates/notebook/tests/live_batch.rs index 6f5cd8723131b8aea0817f6ec2e8918d8434706e..4e558384bfe1baad9d2da080ecc3a8da823a2452 100644 --- a/crates/notebook/tests/live_batch.rs +++ b/crates/notebook/tests/live_batch.rs @@ -676,3 +676,87 @@ fn a_closed_section_s_uncertain_batch_lands_from_the_background() { Some(EditStatus::Published { .. }) )); } + +/// A batch lost on the way to a host that restarts, while the section moves on, is +/// rebased onto the host's section once the new host no longer holds its base, and lands +/// beside the host's own edit. +#[test] +fn a_lost_batch_whose_base_the_host_dropped_is_rebased() { + use notebook::{EditStatus, Remote, live::share::Guest}; + + let directory = tempfile::tempdir().unwrap(); + let folder = notebook(directory.path()); + let url = relay(Default::default()); + let sharing = Sharing::new("").unwrap(); + let host = host(&folder, &directory.path().join("host"), &sharing, &url); + let welcome = + notebook::live::share::join(hello("Grace"), &code(&host), "", None, Some(&url)).unwrap(); + let relay = PresenceRelay::new(&url, &welcome.room); + let guest = Guest::start( + hello("Grace"), + welcome.share, + welcome.secret, + None, + Some(&relay.url), + || {}, + ) + .unwrap(); + until("the host was never met", || guest.host().is_some()); + let file = folder.join("Garden.one"); + let original = std::fs::read(&file).unwrap(); + let (space, text, _) = server::text(&original); + let typed = |with: &str| Edit { + at: 134_000_000_000_000_000, + ops: vec![Op::Page { + space, + op: PageOp::Text { + text, + range: 0..0, + with: with.into(), + }, + }], + }; + let replica = Replica::open_or_create(directory.path().join("grace.sqlite"), None, || { + Ok(original.clone()) + }) + .unwrap(); + let remote = std::cell::RefCell::new(HostedRemote::new(&guest, "Garden.one")); + let first = replica.apply("Grace", typed("A")).unwrap(); + until("the first edit was never published", || { + let _ = replica.sync_once(&mut *remote.borrow_mut()); + matches!( + replica.status(first).unwrap(), + Some(EditStatus::Published { .. }) + ) + }); + until("the host's stamp was never known without asking", || { + let before = relay.state.lock().unwrap().access_bytes; + remote.borrow_mut().stamp().unwrap(); + relay.state.lock().unwrap().access_bytes == before + }); + relay.state.lock().unwrap().swallow = true; + let second = replica.apply("Grace", typed("B")).unwrap(); + assert!(replica.sync_once(&mut *remote.borrow_mut()).is_err()); + assert!(matches!( + replica.status(second).unwrap(), + Some(EditStatus::AwaitingConfirmation { .. }) + )); + + // The host quits, edits the section itself, and shares again. + let saved = host.sharing(); + drop(host); + let image = std::fs::read(&file).unwrap(); + std::fs::write(&file, server::typed(&image, space, text, 14..14, "!")).unwrap(); + let _host = self::host(&folder, &directory.path().join("host"), &saved, &url); + until("the uncertain batch never landed", || { + let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Garden.one")); + matches!( + replica.status(second).unwrap(), + Some(EditStatus::Published { .. }) + ) + }); + assert_eq!( + server::text(&std::fs::read(&file).unwrap()).2, + "BAOriginal text!" + ); +}