OK off the window thread: a job that has just started waits a frame (#2929) (#3609)

* claim: #2929

Co-Authored-By: Claude Code <noreply@anthropic.com>

* OK off the window thread: a job that has just started waits a frame (#2929)

The off-thread evaluation of a feature dialog was taken up in the same frame
it started, so a job whose worker had already finished was applied at once
and the dialog never said it is computing. Under load the window thread is
held up between starting the job and the check, and the feature appears
where the computing state belongs - which is what made
`apply_off_thread_tests::cancel_while_computing_applies_nothing` fail with
`assertion failed: h.app.applying.is_some()`.

A job that has just started now waits one frame: its result is taken up from
the next one, so the dialog says it is computing before the feature appears.

The app waits APPLY_AT_ONCE (150 ms) for the evaluation before it lets the
window go on, so a job only exists when the evaluation took longer than
that: the change shows the computing state for a frame in the 150-151 ms
window, and nothing else moves.

Co-Authored-By: Claude Code <noreply@anthropic.com>

---------

Co-authored-by: jeremymiribung-blip <jeremymiribung-blip@users.noreply.github.com>
Co-authored-by: Claude Code <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-10-20 23:02:34 -04:00
1 parent 08eff4f9dd
commit cbac4eab3f
2 files changed
+37 -4

No files matched your search

+19 -2
View File
@@ -73,6 +73,23 @@ fn cancel_while_computing_applies_nothing() {
assert_eq!(h.app.session.doc.features.len(), features);
}
/// #2929: the feature must not appear in the frame OK starts computing in. The window thread
/// can be held up between starting the evaluation and the check that takes it up, and the
/// feature then appears where the dialog says it is computing - which is what made
/// `cancel_while_computing_applies_nothing` fail under load.
#[test]
fn ok_computing_off_the_window_thread_does_not_apply_in_the_frame_it_starts() {
let mut h = square_on_a_block();
h.app.apply_off_thread = Some(Duration::ZERO);
let features = h.app.session.doc.features.len();
h.step(&json!({"key": "Enter"})).unwrap();
assert!(h.app.applying.is_some(), "the dialog is computing");
assert_eq!(h.app.session.doc.features.len(), features, "nothing applied in the frame it started");
// A frame later the evaluation is in and the feature is applied.
until_applied(&mut h);
assert_eq!(h.app.session.doc.features.len(), features + 1);
}
#[test]
fn the_dialog_holds_its_inputs_while_ok_computes() {
let mut h = square_on_a_block();
@@ -82,7 +99,7 @@ fn the_dialog_holds_its_inputs_while_ok_computes() {
// OK computing, until the test lets it finish.
let (finish, done) = std::sync::mpsc::channel();
let cmds = h.app.dialog.as_ref().and_then(|d| crate::dialogs::apply_commands(&h.app, d).ok()).unwrap();
h.app.applying = Some(crate::dialogs::Applying { done, started: crate::now_ms(), cmds });
h.app.applying = Some(crate::dialogs::Applying { done, started: crate::now_ms(), cmds, seen: true });
// A click on the square picked would take it out of the profiles.
h.step(&json!({"click": {"world": [10, 10, 10]}})).unwrap();
h.frames(2);
@@ -103,7 +120,7 @@ fn inputs_changed_while_ok_computes_are_computed_off_the_thread_too() {
let features = h.app.session.doc.features.len();
// OK computing other commands than the dialog's, until the test lets it finish.
let (finish, done) = std::sync::mpsc::channel();
h.app.applying = Some(crate::dialogs::Applying { done, started: crate::now_ms(), cmds: Vec::new() });
h.app.applying = Some(crate::dialogs::Applying { done, started: crate::now_ms(), cmds: Vec::new(), seen: true });
finish.send(()).unwrap();
h.frames(1);
assert!(h.app.applying.is_some(), "the dialog's own commands are computing");
+18 -2
View File
@@ -436,6 +436,9 @@ pub struct Applying {
pub(crate) started: f64,
/// The commands being computed.
pub(crate) cmds: Vec<(String, Value)>,
/// The job has been in a frame. A job that has just started is not taken up in the frame it
/// starts, so the dialog says it is computing before the feature appears.
pub(crate) seen: bool,
}
impl Applying {
@@ -443,6 +446,13 @@ impl Applying {
fn finished(&self) -> bool {
!matches!(self.done.try_recv(), Err(std::sync::mpsc::TryRecvError::Empty))
}
/// The evaluation is in and the job has been in a frame. A job that has just started waits
/// one frame: the window thread may be held up between starting it and the check, and the
/// feature must not appear where the dialog says it is computing.
fn ready(&self) -> bool {
self.seen && self.finished()
}
}
/// How long the desktop app's OK waits for its evaluation before it lets the window go on and
@@ -477,7 +487,7 @@ fn begin_applying(app: &SolveApp, d: &Dialog) -> Option<Applying> {
})
.ok()?;
match rx.recv_timeout(wait) {
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => Some(Applying { done: rx, started: crate::now_ms(), cmds }),
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => Some(Applying { done: rx, started: crate::now_ms(), cmds, seen: false }),
_ => None,
}
}
@@ -2416,7 +2426,13 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
// OK's evaluation is in: run it, the feature taken from the cache. Inputs changed meanwhile
// (nothing should change them) are computed in turn, off the window thread too: run here,
// they would hold the window for the whole boolean.
if app.applying.as_ref().is_some_and(Applying::finished) && !cancel {
// A job that has just started is not in yet: it waits a frame, so the dialog says it is
// computing before the feature appears.
let ready = app.applying.as_ref().is_some_and(Applying::ready);
if let Some(job) = &mut app.applying {
job.seen = true;
}
if ready && !cancel {
let computed = app.applying.take().map(|job| job.cmds);
let current = apply_commands(app, &d).ok();
let again = if current.is_some() && current != computed { begin_applying(app, &d) } else { None };