Skip to content

Commit bb2fb43

Browse files
authored
fix(desktop): a download fails only when it stalls, not when it takes longer than 30 seconds (#830)
deployment::get read the body with bytes(), which the blocking client bounds by its 30-second default timeout and reports as 'error decoding response body' when that runs out. Any download taking longer than 30 seconds failed however steadily it arrived: Bun's 30 MB on a connection under about 1 MB/s, the Compose and Podman downloads, and the release lookup. Present since get() was written (#392). The body is now read as a stream, where the timeout bounds each read, so a download is given up only after 30 seconds with nothing arriving. A slow-server test fails with the old code's exact message and passes now.
1 parent 16014be commit bb2fb43

1 file changed

Lines changed: 69 additions & 4 deletions

File tree

‎desktop/src-tauri/src/deployment.rs‎

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,25 @@ fn fetch_images(root: &Path, version: &str) -> Result<(), String> {
332332
/// Shared with `install.rs`, which fetches the engine's installers through it and then checks their
333333
/// digests. One client, one user agent, one set of TLS defaults.
334334
pub fn get(url: &str) -> Result<Vec<u8>, String> {
335-
let response = reqwest::blocking::Client::builder()
335+
get_waiting(url, IDLE_TIMEOUT)
336+
}
337+
338+
/// How long a download may go without receiving anything before it is given up.
339+
const IDLE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
340+
341+
/**
342+
`get`, giving up only when nothing arrives for `idle`.
343+
344+
READ AS A STREAM, NOT WITH `bytes()`. The blocking client's timeout (30 s by default) bounds each
345+
read on a stream, but `bytes()` waits for the whole body within it, and reports running out of time
346+
as "error decoding response body". So any download that took longer than 30 seconds failed, however
347+
steadily it was arriving: Bun's 30 MB on a connection under about 1 MB/s, every time.
348+
*/
349+
fn get_waiting(url: &str, idle: std::time::Duration) -> Result<Vec<u8>, String> {
350+
use std::io::Read;
351+
let mut response = reqwest::blocking::Client::builder()
336352
.user_agent("openbot-desktop")
353+
.timeout(idle)
337354
.build()
338355
.map_err(|error| format!("could not prepare the download: {error}"))?
339356
.get(url)
@@ -342,16 +359,64 @@ pub fn get(url: &str) -> Result<Vec<u8>, String> {
342359
if !response.status().is_success() {
343360
return Err(format!("{url} answered {}", response.status()));
344361
}
362+
let mut body = Vec::new();
345363
response
346-
.bytes()
347-
.map(|body| body.to_vec())
348-
.map_err(|error| format!("the download did not finish: {error}"))
364+
.read_to_end(&mut body)
365+
.map_err(|error| format!("the download did not finish: {error}"))?;
366+
Ok(body)
349367
}
350368

351369
#[cfg(test)]
352370
mod tests {
353371
use super::*;
354372

373+
/// A server that sends `chunks` pieces of `body`, `gap` apart, then closes.
374+
fn dripping(chunks: usize, gap: std::time::Duration) -> (String, std::thread::JoinHandle<()>) {
375+
use std::io::{Read, Write};
376+
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
377+
let url = format!("http://{}/file", listener.local_addr().unwrap());
378+
let served = std::thread::spawn(move || {
379+
let (mut stream, _) = listener.accept().unwrap();
380+
let mut request = [0u8; 1024];
381+
let _ = stream.read(&mut request);
382+
let length = chunks * 4;
383+
stream
384+
.write_all(
385+
format!(
386+
"HTTP/1.1 200 OK\r\ncontent-length: {length}\r\nconnection: close\r\n\r\n"
387+
)
388+
.as_bytes(),
389+
)
390+
.unwrap();
391+
for _ in 0..chunks {
392+
std::thread::sleep(gap);
393+
if stream.write_all(b"data").is_err() {
394+
return;
395+
}
396+
let _ = stream.flush();
397+
}
398+
});
399+
(url, served)
400+
}
401+
402+
#[test]
403+
fn a_download_that_keeps_arriving_finishes_however_long_it_takes() {
404+
// Six pieces 300 ms apart take 1.8 s in all, longer than the 1 s allowed between pieces.
405+
// `bytes()` gave up here with "error decoding response body"; a stream does not.
406+
let (url, served) = dripping(6, std::time::Duration::from_millis(300));
407+
let body = get_waiting(&url, std::time::Duration::from_secs(1)).unwrap();
408+
assert_eq!(body, b"data".repeat(6));
409+
served.join().unwrap();
410+
}
411+
412+
#[test]
413+
fn a_download_that_stalls_is_given_up() {
414+
let (url, served) = dripping(2, std::time::Duration::from_millis(1500));
415+
let error = get_waiting(&url, std::time::Duration::from_millis(500)).unwrap_err();
416+
assert!(error.starts_with("the download did not finish"), "{error}");
417+
served.join().unwrap();
418+
}
419+
355420
fn manifest(names: &[&str]) -> Images {
356421
Images {
357422
version: "v0.0.7".into(),

0 commit comments

Comments
 (0)