From b77e64db05f9b062024344f4a5a1bed1cb8e7014 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 19 Oct 2026 21:47:27 -0700 Subject: [PATCH] fix(notebook): check again soon a closed section whose replica was held A background check that met a closed section's replica held by another holder (a session closing, an earlier background still finishing its own check) took the file's new stamp as seen and moved on: the change was not reported, and the section was not checked again until the next interval, an hour where a watch reports the folder. Such a check now leaves the stamp as it was and comes back in a second. This fits the two session.rs flakes in gate 20261019-174028, both waiting 20 s on a background check: the second reopens a notebook right after dropping a background, whose thread may still hold a replica. The pass-over predates #148 (xywznqsl); no loaded run here reproduced either flake, on main or before #148, so this is the race the code shows rather than one caught in the act. A test holds the replica across the check the touch asks for; it fails without the fix. Assisted-by: claude-opus-5.5 --- crates/notebook/src/background.rs | 18 ++++++++++-- crates/notebook/tests/session.rs | 48 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/crates/notebook/src/background.rs b/crates/notebook/src/background.rs index c8f780bc80176452387485de5abfda82ccb3117f..bbed5fbcd1d33e19faecba1a3c5c148cd937e072 100644 --- a/crates/notebook/src/background.rs +++ b/crates/notebook/src/background.rs @@ -28,6 +28,10 @@ const RETRY: Duration = Duration::from_secs(31); /// Between one section's first check and the next's, so opening a notebook reads its files /// one after another rather than in a burst. const STAGGER: Duration = Duration::from_millis(100); + +/// How soon a section whose replica another holder had is checked again. +const HELD: Duration = Duration::from_secs(1); + /// How long a reported change settles before its section is checked, so the writes of one /// commit cost one check. const SETTLE: Duration = Duration::from_secs(1); @@ -298,6 +302,17 @@ impl Background { } moved || before != summary(&watch.status) } + // Another holder has the replica for a moment, as a session closing or an + // earlier background finishing its own check does: the section is checked + // again soon, and the change it may have taken is not passed over. + Err(error) + if error.busy() + || matches!(&error, Error::Io(error) + if error.kind() == io::ErrorKind::WouldBlock) => + { + watch.due = watch.due.min(now + HELD); + false + } Err(error) => { watch.due = watch.due.min(now + RETRY); watch.fail(&error, queued) @@ -1020,12 +1035,11 @@ async fn step_async( Ok((base, 0)) if base == stamp && versions.is_empty() => { return (Some(0), Ok((stamp, moved))); } - Err(error) if error.busy() => return (None, Ok((stamp, false))), + Err(error) if error.busy() => return (None, Err(error)), _ => {} } let replica = match Replica::open(replica) { Ok(replica) => replica, - Err(error) if error.busy() => return (None, Ok((stamp, false))), Err(error) => return (None, Err(error)), }; let synced = (async { diff --git a/crates/notebook/tests/session.rs b/crates/notebook/tests/session.rs index 46da7c75f7d164db1e77c26badd724ff74e657aa..86d5ceeb5e2823f5a1bf4c9a9a2d31ada397bac5 100644 --- a/crates/notebook/tests/session.rs +++ b/crates/notebook/tests/session.rs @@ -1156,6 +1156,54 @@ fn a_remote_change_to_a_closed_section_is_noticed_and_rebases_its_replica() { assert_same(opened.unwrap().page(*space).unwrap(), native); } +/// A closed section's replica another holder has for a moment, as a session closing or an +/// earlier background still finishing its check does, is checked again once it is let go: the +/// change the file took is noticed and taken, not passed over until the next backstop. +#[test] +fn a_change_to_a_section_whose_replica_is_held_is_noticed_once_it_is_let_go() { + let directory = tempfile::tempdir().unwrap(); + let mut notebook = two_sections(directory.path()); + notebook + .section("Second.one", false, || {}) + .unwrap() + .close() + .unwrap(); + let background = notebook.background(true, false, || {}).unwrap(); + background.watch(notebook.replicas()); + until("both sections were reached", || { + background + .status() + .iter() + .all(|(_, status)| status.synced.is_some() && status.error.is_none()) + }); + let replica = notebook.replica_path("Second.one").unwrap(); + let held = notebook::Replica::open(&replica).unwrap(); + let file = directory.path().join("Shared/Second.one"); + let bytes = onestore::read_file(&file).unwrap(); + let space = notebook::session::stored_pages(&bytes).unwrap()[0].space; + let native = edited(&model_ops::page_of(&bytes, space), "Native "); + ops::save(&bytes, space, &native) + .unwrap() + .commit_file(&file) + .unwrap(); + background.touched(&["Second.one".to_owned()]); + // Long enough for the check the touch asks for to meet the held replica. + std::thread::sleep(Duration::from_secs(2)); + drop(held); + let mut noticed = Vec::new(); + until("the change was noticed", || { + noticed.extend(background.changed()); + noticed.iter().any(|path| path == "Second.one") + }); + drop(background); + let mut opened = None; + until("the background released the replica", || { + opened = notebook::Replica::open(&replica).ok(); + opened.is_some() + }); + assert_same(opened.unwrap().page(space).unwrap(), &native); +} + #[test] fn a_reported_change_wakes_its_section_without_polling() { let directory = tempfile::tempdir().unwrap(); -- 2.54.1