From a9d9673f2a15a11493e95beb207f8d40f120a9c4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 10 May 2026 12:32:20 +0000 Subject: [PATCH] fix: break server reader threads on read error stdout.read(...).unwrap_or(!0) substituted usize::MAX on Err, then `if on > buffer.len() { continue; }` swallowed it and looped back into read(). On a sticky IO error (broken pipe, EBADF) the thread spun a CPU core forever and the accumulated logs were never flushed. Match Ok(0)/Ok(n)/Err(e) explicitly: break on EOF, break on error after logging, and treat any other Ok(n) as bytes read. Same change for stderr. Closes #51 --- src/stremio_app/stremio_server/server.rs | 32 ++++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/stremio_app/stremio_server/server.rs b/src/stremio_app/stremio_server/server.rs index c40a672..15ae361 100644 --- a/src/stremio_app/stremio_server/server.rs +++ b/src/stremio_app/stremio_server/server.rs @@ -85,10 +85,14 @@ impl StremioServer { let http_endpoint = String::new(); loop { let mut buffer = [0; SRV_BUFFER_SIZE]; - let on = stdout.read(&mut buffer[..]).unwrap_or(!0); - if on > buffer.len() { - continue; - } + let on = match stdout.read(&mut buffer[..]) { + Ok(0) => break, + Ok(n) => n, + Err(err) => { + eprintln!("server stdout read error: {err}"); + break; + } + }; std::io::stdout().write_all(&buffer).ok(); let string_data = String::from_utf8_lossy(&buffer[..on]); { @@ -116,10 +120,6 @@ impl StremioServer { .collect::>() .join("\n"); }; - if on == 0 { - // Server terminated - break; - } } }); @@ -128,10 +128,14 @@ impl StremioServer { let err_thread = thread::spawn(move || { let mut buffer = [0; SRV_BUFFER_SIZE]; loop { - let en = stderr.read(&mut buffer[..]).unwrap_or(!0); - if en > buffer.len() { - continue; - } + let en = match stderr.read(&mut buffer[..]) { + Ok(0) => break, + Ok(n) => n, + Err(err) => { + eprintln!("server stderr read error: {err}"); + break; + } + }; std::io::stderr().write_all(&buffer).ok(); let string_data = String::from_utf8_lossy(&buffer[..en]); // eprint!("{:?}", &buffer); @@ -148,10 +152,6 @@ impl StremioServer { .collect::>() .join("\n"); }; - if en == 0 { - // Server terminated - break; - } } }); out_thread.join().ok();