authoravatarclover caruso<git@paperclover.net>2026-10-19 21:47:27-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:39:58-07:00
logb77e64db05f9b062024344f4a5a1bed1cb8e7014
tree95c1ee0c0e5ddbaa85ba52075e8e04063a4740a8
parente4896e5afe119be520884ea2909139e167cbf6b9
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

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

2 files changed, 64 insertions(+), 2 deletions(-)

crates/notebook/src/background.rs+16-2
...@@ -28,6 +28,10 @@ const RETRY: Duration = Duration::from_secs(31);...@@ -28,6 +28,10 @@ const RETRY: Duration = Duration::from_secs(31);
28/// Between one section's first check and the next's, so opening a notebook reads its files28/// Between one section's first check and the next's, so opening a notebook reads its files
29/// one after another rather than in a burst.29/// one after another rather than in a burst.
30const STAGGER: Duration = Duration::from_millis(100);30const STAGGER: Duration = Duration::from_millis(100);
31
32/// How soon a section whose replica another holder had is checked again.
33const HELD: Duration = Duration::from_secs(1);
34
31/// How long a reported change settles before its section is checked, so the writes of one35/// How long a reported change settles before its section is checked, so the writes of one
32/// commit cost one check.36/// commit cost one check.
33const SETTLE: Duration = Duration::from_secs(1);37const SETTLE: Duration = Duration::from_secs(1);
...@@ -298,6 +302,17 @@ impl Background {...@@ -298,6 +302,17 @@ impl Background {
298 }302 }
299 moved || before != summary(&watch.status)303 moved || before != summary(&watch.status)
300 }304 }
305 // Another holder has the replica for a moment, as a session closing or an
306 // earlier background finishing its own check does: the section is checked
307 // again soon, and the change it may have taken is not passed over.
308 Err(error)
309 if error.busy()
310 || matches!(&error, Error::Io(error)
311 if error.kind() == io::ErrorKind::WouldBlock) =>
312 {
313 watch.due = watch.due.min(now + HELD);
314 false
315 }
301 Err(error) => {316 Err(error) => {
302 watch.due = watch.due.min(now + RETRY);317 watch.due = watch.due.min(now + RETRY);
303 watch.fail(&error, queued)318 watch.fail(&error, queued)
...@@ -1020,12 +1035,11 @@ async fn step_async<R: Remote>(...@@ -1020,12 +1035,11 @@ async fn step_async<R: Remote>(
1020 Ok((base, 0)) if base == stamp && versions.is_empty() => {1035 Ok((base, 0)) if base == stamp && versions.is_empty() => {
1021 return (Some(0), Ok((stamp, moved)));1036 return (Some(0), Ok((stamp, moved)));
1022 }1037 }
1023 Err(error) if error.busy() => return (None, Ok((stamp, false))),1038 Err(error) if error.busy() => return (None, Err(error)),
1024 _ => {}1039 _ => {}
1025 }1040 }
1026 let replica = match Replica::open(replica) {1041 let replica = match Replica::open(replica) {
1027 Ok(replica) => replica,1042 Ok(replica) => replica,
1028 Err(error) if error.busy() => return (None, Ok((stamp, false))),
1029 Err(error) => return (None, Err(error)),1043 Err(error) => return (None, Err(error)),
1030 };1044 };
1031 let synced = (async {1045 let synced = (async {
crates/notebook/tests/session.rs+48
...@@ -1156,6 +1156,54 @@ fn a_remote_change_to_a_closed_section_is_noticed_and_rebases_its_replica() {...@@ -1156,6 +1156,54 @@ fn a_remote_change_to_a_closed_section_is_noticed_and_rebases_its_replica() {
1156 assert_same(opened.unwrap().page(*space).unwrap(), native);1156 assert_same(opened.unwrap().page(*space).unwrap(), native);
1157}1157}
11581158
1159/// A closed section's replica another holder has for a moment, as a session closing or an
1160/// earlier background still finishing its check does, is checked again once it is let go: the
1161/// change the file took is noticed and taken, not passed over until the next backstop.
1162#[test]
1163fn a_change_to_a_section_whose_replica_is_held_is_noticed_once_it_is_let_go() {
1164 let directory = tempfile::tempdir().unwrap();
1165 let mut notebook = two_sections(directory.path());
1166 notebook
1167 .section("Second.one", false, || {})
1168 .unwrap()
1169 .close()
1170 .unwrap();
1171 let background = notebook.background(true, false, || {}).unwrap();
1172 background.watch(notebook.replicas());
1173 until("both sections were reached", || {
1174 background
1175 .status()
1176 .iter()
1177 .all(|(_, status)| status.synced.is_some() && status.error.is_none())
1178 });
1179 let replica = notebook.replica_path("Second.one").unwrap();
1180 let held = notebook::Replica::open(&replica).unwrap();
1181 let file = directory.path().join("Shared/Second.one");
1182 let bytes = onestore::read_file(&file).unwrap();
1183 let space = notebook::session::stored_pages(&bytes).unwrap()[0].space;
1184 let native = edited(&model_ops::page_of(&bytes, space), "Native ");
1185 ops::save(&bytes, space, &native)
1186 .unwrap()
1187 .commit_file(&file)
1188 .unwrap();
1189 background.touched(&["Second.one".to_owned()]);
1190 // Long enough for the check the touch asks for to meet the held replica.
1191 std::thread::sleep(Duration::from_secs(2));
1192 drop(held);
1193 let mut noticed = Vec::new();
1194 until("the change was noticed", || {
1195 noticed.extend(background.changed());
1196 noticed.iter().any(|path| path == "Second.one")
1197 });
1198 drop(background);
1199 let mut opened = None;
1200 until("the background released the replica", || {
1201 opened = notebook::Replica::open(&replica).ok();
1202 opened.is_some()
1203 });
1204 assert_same(opened.unwrap().page(space).unwrap(), &native);
1205}
1206
1159#[test]1207#[test]
1160fn a_reported_change_wakes_its_section_without_polling() {1208fn a_reported_change_wakes_its_section_without_polling() {
1161 let directory = tempfile::tempdir().unwrap();1209 let directory = tempfile::tempdir().unwrap();