From 53dee1298ca95f9c89ecc8b2d68321326c562aad Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 28 Nov 2026 23:56:20 +0000 Subject: [PATCH] automation: a request left waiting at a pass end asks for the main window's next frame eframe keeps a repaint request only while the pass number it names is at most one pass old. The control server's wake from its own thread names the pass before the frame; when that frame runs twice (a discarded first pass), the wake is dropped. An app that takes requests once a frame, before its passes, then leaves a request that arrived after its drain queued until other input comes: DeckCraft's look runs timed out after 60 s on engine.execute. ControlChannel::poll_input, which runs at every pass end, now looks for a waiting request (held for next_request) and asks for the next frame from inside the pass, where the wake names the running pass. Co-Authored-By: Claude Opus 5.5 --- crates/automation/src/control.rs | 16 ++++++++++++--- crates/automation/tests/it/control.rs | 29 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/crates/automation/src/control.rs b/crates/automation/src/control.rs index bb7821e6af..86283027f9 100644 --- a/crates/automation/src/control.rs +++ b/crates/automation/src/control.rs @@ -171,6 +171,8 @@ pub fn result_reply(r: Result) -> ControlResponse { #[non_exhaustive] pub struct ControlChannel { rx: Receiver, + /// The request [`poll_input`](Self::poll_input) took from `rx` to see that one waits; it runs first. + held: Option, input_waiters: Vec>, job_waiters: Vec<(JobId, Sender)>, tree_waiters: Vec>, @@ -181,14 +183,14 @@ pub struct ControlChannel { impl ControlChannel { /// Attach a transport's receiver (TCP, stdin, tests). pub fn new(rx: Receiver) -> Self { - Self { rx, input_waiters: Vec::new(), job_waiters: Vec::new(), tree_waiters: Vec::new(), captures: Captures::default() } + Self { rx, held: None, input_waiters: Vec::new(), job_waiters: Vec::new(), tree_waiters: Vec::new(), captures: Captures::default() } } /// The next request to run, claimed through [`Dispatch::start`]. Expired or abandoned /// requests are replied with [`expired_error`] here and skipped. `None` when the queue is /// empty this frame. pub fn next_request(&mut self) -> Option { - while let Ok(req) = self.rx.try_recv() { + while let Some(req) = self.held.take().or_else(|| self.rx.try_recv().ok()) { if req.deadline.is_some_and(|deadline| motion::now() >= deadline) || !req.dispatch.start() { let _ = req.reply.send(json!({"ok": false, "error": expired_error()})); continue; @@ -244,9 +246,17 @@ impl ControlChannel { /// once nothing is queued or in flight. Call it once per pass, at the end of the pass (after its /// UI ran): the step that pass consumed counts as run, so the reply goes out with that pass /// ([`WindowHost::injected_idle_at_pass_end`]). Called between passes, it waits for the pass. It also has the host - /// keep its access trees, which `control` targets and `ui.find` read. + /// keep its access trees, which `control` targets and `ui.find` read, and asks for the main window's next frame + /// while a request waits: the transport's wake from its own thread names the pass it was sent in, and eframe drops + /// a wake more than one pass old, so a request sent before a frame that runs twice would wait for other input. pub fn poll_input(&mut self, host: &WindowHost) { host.keep_access_trees(); + if self.held.is_none() { + self.held = self.rx.try_recv().ok(); + } + if self.held.is_some() { + host.repaint_in(egui::ViewportId::ROOT, -1.0); + } let gone = host.take_injected_gone(); if host.take_injected_withdrawn() { self.input_cancelled(&gone.map_or_else(|| TARGET_CHANGED.to_owned(), |node| crate::synthetic::gone(&node))); diff --git a/crates/automation/tests/it/control.rs b/crates/automation/tests/it/control.rs index 6b3c9009fa..8dc612ff9f 100644 --- a/crates/automation/tests/it/control.rs +++ b/crates/automation/tests/it/control.rs @@ -65,6 +65,35 @@ fn a_request_without_a_deadline_is_dispatched_after_its_reply_receiver_is_droppe assert!(channel.next_request().is_none(), "the request runs once"); } +/// A request that arrives after the app took its requests for a frame asks for the next frame, also when the frame +/// runs twice: the transport's own wake, sent before the frame, is spent on its first pass. +#[trial] +fn a_request_left_waiting_at_a_frame_end_asks_for_the_next_frame() { + let ctx = Context::default(); + let (tx, rx) = channel(); + let mut channel = ControlChannel::new(rx); + let host = WindowHost::of(&ctx); + let wakes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new())); + let sink = wakes.clone(); + ctx.set_request_repaint_callback(move |info| sink.lock().expect("wakes").push(info.current_cumulative_pass_nr)); + let frame = |ui: &mut dyn FnMut(&mut egui::Ui)| ctx.run_ui(egui::RawInput::default(), ui).textures_delta.clear(); + frame(&mut |_| {}); + frame(&mut |_| {}); + let (request, _reply) = ControlRequest::new("engine.execute", json!({})); + tx.send(request).expect("sent"); + ctx.request_repaint(); + frame(&mut |ui| { + if ui.ctx().current_pass_index() == 0 { + ui.ctx().request_discard("a layout change"); + } + channel.poll_input(&host); + }); + // eframe keeps a wake only while the pass number it names is at most one pass old. + let now = ctx.cumulative_pass_nr(); + assert!(wakes.lock().expect("wakes").iter().any(|nr| now <= nr + 1), "the frame asks for the next one"); + assert_eq!(channel.next_request().map(|request| request.method), Some("engine.execute".to_owned()), "the request still runs"); +} + /// A job's reply goes to the requests waiting on that job only, as the app built it (an error with partial results /// keeps both); the other job's waiter keeps waiting. #[trial]