fix(app): install cancels whole process trees and silent downloads, and finishes only with the settings written

- A cancelled command ends with every process it started (process group on
  Unix, taskkill /T on Windows).
- Waiting for a response or the next chunk gives way to a cancel, so a
  silent server cannot hold the install.
- Pointing the settings at the runtime is the last install step: a failure
  shows as failed and can be retried; done means written.
- Strata's installer gets its folder as the working folder, not inside the
  script text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 17:17:25 +09:00
parent a4436698c8
commit 1a6d6447f6
6 changed files with 196 additions and 28 deletions

View File

@@ -18,6 +18,22 @@ impl std::fmt::Display for DownloadError {
}
impl std::error::Error for DownloadError {}
/// Await `fut`, giving up when `cancel` is set (checked every 200 ms): a server that keeps the
/// connection open and sends nothing cannot hold a cancel.
async fn or_cancel<T>(fut: impl std::future::Future<Output = T>, cancel: &AtomicBool) -> Result<T, DownloadError> {
tokio::pin!(fut);
loop {
tokio::select! {
v = &mut fut => return Ok(v),
_ = tokio::time::sleep(std::time::Duration::from_millis(200)) => {
if cancel.load(Ordering::Relaxed) {
return Err(DownloadError("cancelled".into()));
}
}
}
}
}
fn e(what: &str) -> impl FnOnce(std::io::Error) -> DownloadError + '_ {
move |err| DownloadError(format!("{what}: {err}"))
}
@@ -49,7 +65,8 @@ pub async fn download(url: &str, dest: &Path, sha256: &str, progress: &(dyn Fn(u
if have > 0 {
req = req.header(reqwest::header::RANGE, format!("bytes={have}-"));
}
let resp = req.send().await.map_err(|x| DownloadError(format!("could not download {url}: {x}")))?;
let resp = or_cancel(req.send(), cancel).await?
.map_err(|x| DownloadError(format!("could not download {url}: {x}")))?;
let status = resp.status();
if status.as_u16() == 416 && have > 0 {
// The part file already holds every byte: check it below.
@@ -64,7 +81,15 @@ pub async fn download(url: &str, dest: &Path, sha256: &str, progress: &(dyn Fn(u
progress(done, total);
if status.as_u16() != 416 {
let mut stream = resp.bytes_stream();
while let Some(chunk) = stream.next().await {
loop {
let next = match or_cancel(stream.next(), cancel).await {
Ok(n) => n,
Err(c) => {
file.flush().await.ok(); // what arrived stays for the next try
return Err(c);
}
};
let Some(chunk) = next else { break };
if cancel.load(Ordering::Relaxed) {
file.flush().await.ok();
return Err(DownloadError("cancelled".into()));
@@ -168,6 +193,47 @@ mod tests {
assert!(!dest2.exists() && !PathBuf::from(format!("{}.part", dest2.display())).exists());
}
/// Accepts, optionally sends headers and the first `bytes`, then holds the connection silently.
async fn stall(headers: bool, bytes: usize) -> String {
let l = TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!("http://{}/f.bin", l.local_addr().unwrap());
tokio::spawn(async move {
let mut held = vec![];
while let Ok((mut s, _)) = l.accept().await {
let mut buf = vec![0u8; 4096];
let _ = s.read(&mut buf).await;
if headers {
let _ = s.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 1000000\r\n\r\n").await;
let _ = s.write_all(&vec![7u8; bytes]).await;
}
held.push(s);
}
});
url
}
#[tokio::test]
async fn a_silent_server_does_not_hold_a_cancel() {
for (headers, bytes) in [(false, 0), (true, 5000)] {
let url = stall(headers, bytes).await;
let dest = temp_dir("dlstall").join("f.bin");
let cancel = Arc::new(AtomicBool::new(false));
let c2 = cancel.clone();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(400)).await;
c2.store(true, Ordering::Relaxed);
});
let t = std::time::Instant::now();
let err = download(&url, &dest, &"0".repeat(64), &|_, _| {}, &cancel).await.unwrap_err();
assert_eq!(err.0, "cancelled", "headers={headers}");
assert!(t.elapsed() < std::time::Duration::from_secs(3), "{:?}", t.elapsed());
if headers {
let part = std::fs::metadata(format!("{}.part", dest.display())).unwrap().len();
assert_eq!(part, bytes as u64, "what arrived stays for the next try");
}
}
}
#[tokio::test]
async fn cancelling_keeps_the_part_for_the_next_try() {
let (body, sum) = data();