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 {...@@ -417,7 +417,8 @@ enum Replay {
417 /// Waits for nothing to be on its way, then writes the window's accessibility tree as417 /// Waits for nothing to be on its way, then writes the window's accessibility tree as
418 /// text where given, and answers.418 /// text where given, and answers.
419 Settle(Option<PathBuf>, std::sync::mpsc::Sender<()>),419 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.
421 Tick,422 Tick,
422 /// What the window sent itself when it found nothing on its way, at that instant: once it423 /// What the window sent itself when it found nothing on its way, at that instant: once it
423 /// arrives, so has every event sent before.424 /// arrives, so has every event sent before.
...@@ -6645,6 +6646,9 @@ fn page_text(editor: &CanvasEditor) -> String {...@@ -6645,6 +6646,9 @@ fn page_text(editor: &CanvasEditor) -> String {
6645/// Work [`spawn`] started that has not ended.6646/// Work [`spawn`] started that has not ended.
6646static RUNNING: AtomicUsize = AtomicUsize::new(0);6647static 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
6648fn spawn(work: impl FnOnce() + Send + 'static) {6652fn spawn(work: impl FnOnce() + Send + 'static) {
6649 RUNNING.fetch_add(1, Ordering::Relaxed);6653 RUNNING.fetch_add(1, Ordering::Relaxed);
6650 let work = move || {6654 let work = move || {
...@@ -6801,7 +6805,8 @@ impl State {...@@ -6801,7 +6805,8 @@ impl State {
6801 Replay::Settle(path, settled) => {6805 Replay::Settle(path, settled) => {
6802 self.replay_settle = Some((path, settled, None));6806 self.replay_settle = Some((path, settled, None));
6803 }6807 }
6804 Replay::Tick | Replay::Mark(_) | Replay::Quit => {}6808 Replay::Tick => TICKING.store(false, Ordering::Release),
6809 Replay::Mark(_) | Replay::Quit => {}
6805 Replay::Appearance(appearance) => {6810 Replay::Appearance(appearance) => {
6806 self.window.set_theme(Some(appearance));6811 self.window.set_theme(Some(appearance));
6807 self.set_appearance(appearance);6812 self.set_appearance(appearance);
...@@ -6972,6 +6977,7 @@ impl ApplicationHandler<UserEvent> for App {...@@ -6972,6 +6977,7 @@ impl ApplicationHandler<UserEvent> for App {
6972 UserEvent::Quit => self.close(event_loop),6977 UserEvent::Quit => self.close(event_loop),
6973 // A cold launch's documents arrive before the window opens.6978 // A cold launch's documents arrive before the window opens.
6974 UserEvent::Open(paths) => self.opening.extend(paths),6979 UserEvent::Open(paths) => self.opening.extend(paths),
6980 UserEvent::Replay(Replay::Tick) => TICKING.store(false, Ordering::Release),
6975 _ => {}6981 _ => {}
6976 }6982 }
6977 return;6983 return;
...@@ -7244,6 +7250,16 @@ fn write_png(path: &Path, size: [u32; 2], pixels: &[u8]) -> Result<(), Box<dyn E...@@ -7244,6 +7250,16 @@ fn write_png(path: &Path, size: [u32; 2], pixels: &[u8]) -> Result<(), Box<dyn E
7244 Ok(())7250 Ok(())
7245}7251}
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
7247/// Feeds a development script to the window from another thread, one command per line7263/// Feeds a development script to the window from another thread, one command per line
7248/// in logical pixels: `move X Y`, `press [right]`, `release [right]`, `wheel DX DY`,7264/// in logical pixels: `move X Y`, `press [right]`, `release [right]`, `wheel DX DY`,
7249/// `pressure LEVEL|none`, `pinch FACTOR`, `key NAME`, `type TEXT`, `modifiers [shift]7265/// `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...@@ -7388,15 +7404,20 @@ fn replay(script: String, proxy: EventLoopProxy<UserEvent>) -> Result<(), Box<dy
7388 }7404 }
7389 let settling = matches!(replay, Replay::Settle(..));7405 let settling = matches!(replay, Replay::Settle(..));
7390 let _ = proxy.send_event(UserEvent::Replay(replay));7406 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.
7392 let start = Instant::now();7410 let start = Instant::now();
7393 while settling7411 while settling
7394 && start.elapsed().as_secs() < 60
7395 && settled7412 && settled
7396 .recv_timeout(std::time::Duration::from_micros(16_667))7413 .recv_timeout(std::time::Duration::from_micros(16_667))
7397 .is_err()7414 .is_err()
7398 {7415 {
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);
7400 }7421 }
7401 }7422 }
7402 Err(duration) => {7423 Err(duration) => {
...@@ -7409,7 +7430,7 @@ fn replay(script: String, proxy: EventLoopProxy<UserEvent>) -> Result<(), Box<dy...@@ -7409,7 +7430,7 @@ fn replay(script: String, proxy: EventLoopProxy<UserEvent>) -> Result<(), Box<dy
7409 tick += period;7430 tick += period;
7410 }7431 }
7411 std::thread::sleep(tick - Instant::now());7432 std::thread::sleep(tick - Instant::now());
7412 let _ = proxy.send_event(UserEvent::Replay(Replay::Tick));7433 self::tick(&proxy);
7413 }7434 }
7414 }7435 }
7415 }7436 }