committed by
GitHub
parent
125b8d5387
commit
46aa3d5d47
@@ -180,15 +180,23 @@ impl FileLoad {
|
||||
pub fn features(&self) -> Option<(usize, usize)> {
|
||||
self.running.as_ref().map(|r| r.progress.features())
|
||||
}
|
||||
/// Nothing running or queued (a cancelled job may still be finishing in [`Self::busy`]'s eyes).
|
||||
/// No active or queued request; cancelled import workers may still be unwinding.
|
||||
pub fn idle(&self) -> bool {
|
||||
self.running.is_none() && self.queued.is_empty()
|
||||
}
|
||||
/// Stop the running operation at its next feature and drop the queued ones.
|
||||
/// Signal cancellation, release the import slot and drop queued requests.
|
||||
pub fn cancel(&mut self) {
|
||||
if let Some(r) = &self.running {
|
||||
r.progress.cancel();
|
||||
}
|
||||
// Imports only change a scratch session. Disconnect their result and preview channels
|
||||
// so a slow cancellation checkpoint cannot block the next file or apply a late result.
|
||||
// Exports publish a file: keep their reply until the worker reports whether it committed.
|
||||
if self.running.as_ref().is_some_and(|r| r.request.kind != Kind::ExportDxf)
|
||||
&& let Some(r) = self.running.take()
|
||||
{
|
||||
r.request.answer(Err("file operation cancelled".into()));
|
||||
}
|
||||
for q in self.queued.drain(..) {
|
||||
q.answer(Err("file operation cancelled".into()));
|
||||
}
|
||||
@@ -992,9 +1000,8 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
|
||||
}
|
||||
let cancel = closed || action == ui_kit::Action::Cancel;
|
||||
if cancel && !cancelled {
|
||||
// Keep the running job until it exits: cancelling must not allow another large
|
||||
// import to overlap it in memory. The rebuild stops at its next feature and the
|
||||
// current design stays as it was.
|
||||
// The worker stops at its next cancellation checkpoint; its scratch design and
|
||||
// result channels are isolated from any file operation started afterwards.
|
||||
app.file_load.cancel();
|
||||
app.set_status(if export { crate::i18n::tr_label("dxf.cancelled") } else { "file operation cancelled" }, false);
|
||||
}
|
||||
@@ -1171,34 +1178,58 @@ mod tests {
|
||||
assert!(app.status.as_ref().unwrap().0.contains("bad STEP"));
|
||||
}
|
||||
#[test]
|
||||
fn cancelling_discards_a_finished_result_and_keeps_the_worker_slot_until_completion() {
|
||||
let mut app = SolveApp::new(Session::default(), Default::default());
|
||||
let old = app.session.doc.clone();
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let progress = Arc::new(Progress::default());
|
||||
progress.cancel();
|
||||
app.file_load.running = Some(Running {
|
||||
request: Request::new("cancelled.step", Kind::Insert),
|
||||
base: old.clone(),
|
||||
base_revision: app.session.revision,
|
||||
before: Vec::new(),
|
||||
rx,
|
||||
phase: Arc::new(AtomicU8::new(0)),
|
||||
progress,
|
||||
preview_rx: None,
|
||||
preview: None,
|
||||
partial_rx: None,
|
||||
built: None,
|
||||
staged: None,
|
||||
});
|
||||
assert!(!app.file_load.busy());
|
||||
assert!(app.file_load.running.is_some());
|
||||
let mut computed = Session::default();
|
||||
computed.execute("solid.box", &serde_json::json!({"length":10,"width":10,"height":10})).unwrap();
|
||||
tx.send(Ok(Made { session: computed, result: Value::Null, thumbnail: false })).unwrap();
|
||||
tick(&mut app, &crate::fonts::test_context());
|
||||
assert!(app.file_load.running.is_none());
|
||||
assert!(Arc::ptr_eq(&old, &app.session.doc));
|
||||
fn cancelling_disconnects_late_results_and_answers_queued_callers() {
|
||||
for finished in [false, true] {
|
||||
let mut app = SolveApp::new(Session::default(), Default::default());
|
||||
let old = app.session.doc.clone();
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let (reply_tx, reply_rx) = std::sync::mpsc::channel();
|
||||
let mut request = Request::new("cancelled.step", Kind::Insert);
|
||||
request.reply = Some(reply_tx);
|
||||
let progress = Arc::new(Progress::default());
|
||||
app.file_load.running = Some(Running {
|
||||
request,
|
||||
base: old.clone(),
|
||||
base_revision: app.session.revision,
|
||||
before: Vec::new(),
|
||||
rx,
|
||||
phase: Arc::new(AtomicU8::new(0)),
|
||||
progress: progress.clone(),
|
||||
preview_rx: None,
|
||||
preview: None,
|
||||
partial_rx: None,
|
||||
built: None,
|
||||
staged: None,
|
||||
});
|
||||
let (queued_tx, queued_rx) = std::sync::mpsc::channel();
|
||||
let mut queued = Request::new("queued.step", Kind::Open);
|
||||
queued.reply = Some(queued_tx);
|
||||
app.file_load.queued.push_back(queued);
|
||||
let mut computed = Session::default();
|
||||
computed.execute("solid.box", &json!({"length":10,"width":10,"height":10})).unwrap();
|
||||
let made = Ok(Made { session: computed, result: Value::Null, thumbnail: false });
|
||||
// Both races: the result was queued before Cancel, or arrives afterwards.
|
||||
let late = if finished {
|
||||
tx.send(made).unwrap();
|
||||
None
|
||||
} else {
|
||||
Some(made)
|
||||
};
|
||||
app.file_load.cancel();
|
||||
assert!(progress.is_cancelled());
|
||||
assert!(app.file_load.idle());
|
||||
for reply in [&reply_rx, &queued_rx] {
|
||||
assert_eq!(reply.try_recv().unwrap(), json!({"ok":false,"error":"file operation cancelled"}));
|
||||
assert!(reply.try_recv().is_err(), "each caller is answered once");
|
||||
}
|
||||
app.session.execute("solid.box", &json!({"length":20,"width":20,"height":20})).unwrap();
|
||||
let newer = app.session.doc.clone();
|
||||
if let Some(late) = late {
|
||||
assert!(tx.send(late).is_err(), "the old worker cannot reach the next operation");
|
||||
}
|
||||
tick(&mut app, &crate::fonts::test_context());
|
||||
assert!(Arc::ptr_eq(&newer, &app.session.doc));
|
||||
}
|
||||
}
|
||||
|
||||
/// #1376: the web app reads a STEP file a piece per frame. The first frame only shows the
|
||||
|
||||
@@ -463,3 +463,55 @@ fn opening_a_large_design_never_stalls_the_window() {
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// #3171: the second STEP open completes even if the cancelled worker has not returned.
|
||||
#[test]
|
||||
fn cancelling_a_step_open_releases_the_slot_before_the_worker_returns() {
|
||||
let dir = scratch("step-cancel-reopen");
|
||||
let path = dir.join("box.step").to_string_lossy().to_string();
|
||||
let mut src = Session::default();
|
||||
src.execute("solid.box", &json!({"length": 10, "width": 20, "height": 30})).unwrap();
|
||||
src.execute("file.export", &json!({"path": path})).unwrap();
|
||||
let mut expected = Session::default();
|
||||
expected.execute("doc.open", &json!({"path": path})).unwrap();
|
||||
|
||||
let mut w = Win::new();
|
||||
let blank = w.app.session.doc.clone();
|
||||
let (started_tx, started_rx) = std::sync::mpsc::channel();
|
||||
let (release_tx, release_rx) = std::sync::mpsc::channel();
|
||||
w.app.file_load.worker_gate = Some((started_tx, release_rx));
|
||||
let (first, first_reply) = crate::control::ControlRequest::new("engine.execute", json!({"command":"doc.open", "params":{"path":path}}));
|
||||
assert!(crate::file_load::control(&mut w.app, first).is_none());
|
||||
started_rx.recv_timeout(std::time::Duration::from_secs(60)).unwrap();
|
||||
for _ in 0..3 {
|
||||
w.frame(vec![]);
|
||||
}
|
||||
let cancel = crate::scenario::handle_at("file_loading:cancel").unwrap();
|
||||
w.gesture(&[cancel]);
|
||||
assert!(w.app.file_load.idle(), "Cancel releases the slot while the first worker is still held");
|
||||
let cancelled = first_reply.try_recv().expect("the cancelled caller is answered immediately");
|
||||
assert_eq!(cancelled["ok"], false);
|
||||
assert!(cancelled["error"].as_str().unwrap().contains("cancelled"));
|
||||
assert!(std::sync::Arc::ptr_eq(&blank, &w.app.session.doc));
|
||||
|
||||
let (second, second_reply) = crate::control::ControlRequest::new("engine.execute", json!({"command":"doc.open", "params":{"path":path}}));
|
||||
assert!(crate::file_load::control(&mut w.app, second).is_none());
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
|
||||
let reply = loop {
|
||||
w.frame(vec![]);
|
||||
if let Ok(reply) = second_reply.try_recv() {
|
||||
break reply;
|
||||
}
|
||||
assert!(std::time::Instant::now() < deadline, "the second import completes before releasing the first");
|
||||
std::thread::yield_now();
|
||||
};
|
||||
assert_eq!(reply["ok"], true, "{reply}");
|
||||
assert_eq!(fingerprint(&w.app.session), fingerprint(&expected));
|
||||
assert_eq!(w.app.docs.count(), 1);
|
||||
release_tx.send(()).unwrap();
|
||||
for _ in 0..3 {
|
||||
w.frame(vec![]);
|
||||
}
|
||||
assert_eq!(fingerprint(&w.app.session), fingerprint(&expected));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user