authoravatarclover caruso<git@paperclover.net>2026-10-19 21:57:55-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:39:58-07:00
log3eb7e24ca6cee8017d52b3b5f79f902fbd1f1f8d
treedba40bf7fcffff591a55117ded1ce49ed70248bd
parent513df158d4d03ab227320f5d92fa75722135d7f5
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix(desktop): keep a replay's ticks from piling up behind a slow window

While a replay waits for the window to settle it asked for a frame every 16.7 ms whether or not the last was drawn, so a window drawing slower than that, as on a loaded machine, fell further behind with each one, and the mark that ends a settle waited behind them all: under load the first settle of switching_renderers_keeps_the_window_as_it_was took 35 s, 1809 ticks asked for and 1228 drawn. Past a minute the replay gave up on the step and went on, so the tree it was to write was never written, and the test failed reading it (gate 20261019-213712). A tick is now asked for only once the last has been drawn, and a replay that waits out its minute fails, saying so, rather than going on without what the step was to write. Assisted-by: claude-opus-5.5

1 files changed, 27 insertions(+), 6 deletions(-)

crates/snowbound/src/main.rs+27-6
......@@ -417,7 +417,8 @@ enum Replay {
417417 /// Waits for nothing to be on its way, then writes the window's accessibility tree as
418418 /// text where given, and answers.
419419 Settle(Option<PathBuf>, std::sync::mpsc::Sender<()>),
420 /// A frame during a wait, as a visible window's display would ask for.
420 /// A frame during a wait, as a visible window's display would ask for; one at a time
421 /// (`TICKING`), so a window drawing slower than the display falls no further behind.
421422 Tick,
422423 /// What the window sent itself when it found nothing on its way, at that instant: once it
423424 /// arrives, so has every event sent before.
......@@ -6645,6 +6646,9 @@ fn page_text(editor: &CanvasEditor) -> String {
66456646/// Work [`spawn`] started that has not ended.
66466647static RUNNING: AtomicUsize = AtomicUsize::new(0);
66476648
6649/// A replay's tick is on its way and not yet drawn.
6650static TICKING: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
6651
66486652fn spawn(work: impl FnOnce() + Send + 'static) {
66496653 RUNNING.fetch_add(1, Ordering::Relaxed);
66506654 let work = move || {
......@@ -6801,7 +6805,8 @@ impl State {
68016805 Replay::Settle(path, settled) => {
68026806 self.replay_settle = Some((path, settled, None));
68036807 }
6804 Replay::Tick | Replay::Mark(_) | Replay::Quit => {}
6808 Replay::Tick => TICKING.store(false, Ordering::Release),
6809 Replay::Mark(_) | Replay::Quit => {}
68056810 Replay::Appearance(appearance) => {
68066811 self.window.set_theme(Some(appearance));
68076812 self.set_appearance(appearance);
......@@ -6972,6 +6977,7 @@ impl ApplicationHandler<UserEvent> for App {
69726977 UserEvent::Quit => self.close(event_loop),
69736978 // A cold launch's documents arrive before the window opens.
69746979 UserEvent::Open(paths) => self.opening.extend(paths),
6980 UserEvent::Replay(Replay::Tick) => TICKING.store(false, Ordering::Release),
69756981 _ => {}
69766982 }
69776983 return;
......@@ -7244,6 +7250,16 @@ fn write_png(path: &Path, size: [u32; 2], pixels: &[u8]) -> Result<(), Box<dyn E
72447250 Ok(())
72457251}
72467252
7253#[cfg(not(target_arch = "wasm32"))]
7254/// Asks for a replay's frame, unless the last one asked for is not yet drawn.
7255fn tick(proxy: &EventLoopProxy<UserEvent>) {
7256 if !TICKING.swap(true, Ordering::AcqRel)
7257 && proxy.send_event(UserEvent::Replay(Replay::Tick)).is_err()
7258 {
7259 TICKING.store(false, Ordering::Release);
7260 }
7261}
7262
72477263/// Feeds a development script to the window from another thread, one command per line
72487264/// in logical pixels: `move X Y`, `press [right]`, `release [right]`, `wheel DX DY`,
72497265/// `pressure LEVEL|none`, `pinch FACTOR`, `key NAME`, `type TEXT`, `modifiers [shift]
......@@ -7388,15 +7404,20 @@ fn replay(script: String, proxy: EventLoopProxy<UserEvent>) -> Result<(), Box<dy
73887404 }
73897405 let settling = matches!(replay, Replay::Settle(..));
73907406 let _ = proxy.send_event(UserEvent::Replay(replay));
7391 // Frames tick until settled; a minute bounds a page that never lands.
7407 // Frames tick until settled; a minute bounds a page that never lands, and
7408 // a replay that waited out its minute fails rather than going on without
7409 // what the step was to write.
73927410 let start = Instant::now();
73937411 while settling
7394 && start.elapsed().as_secs() < 60
73957412 && settled
73967413 .recv_timeout(std::time::Duration::from_micros(16_667))
73977414 .is_err()
73987415 {
7399 let _ = proxy.send_event(UserEvent::Replay(Replay::Tick));
7416 if start.elapsed().as_secs() >= 60 {
7417 eprintln!("The replay never settled");
7418 std::process::exit(1);
7419 }
7420 tick(&proxy);
74007421 }
74017422 }
74027423 Err(duration) => {
......@@ -7409,7 +7430,7 @@ fn replay(script: String, proxy: EventLoopProxy<UserEvent>) -> Result<(), Box<dy
74097430 tick += period;
74107431 }
74117432 std::thread::sleep(tick - Instant::now());
7412 let _ = proxy.send_event(UserEvent::Replay(Replay::Tick));
7433 self::tick(&proxy);
74137434 }
74147435 }
74157436 }