diff --git a/CHANGELOG.md b/CHANGELOG.md index 87e87161..139f4f2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- [Windows] Fixed a crash in monitor when espflash is connected via USB Serial/JTAG, and the user is typing into the monitor but the device is not reading serial input. (#943) + ### Removed ## [4.0.1] - 2025-07-07 diff --git a/espflash/src/cli/monitor/mod.rs b/espflash/src/cli/monitor/mod.rs index ccfe2767..13552b89 100644 --- a/espflash/src/cli/monitor/mod.rs +++ b/espflash/src/cli/monitor/mod.rs @@ -11,7 +11,7 @@ //! in our monitor the output is displayed immediately upon reading. use std::{ - io::{ErrorKind, Read, Write, stdout}, + io::{self, ErrorKind, Read, Write, stdout}, time::Duration, }; @@ -173,14 +173,31 @@ fn handle_user_input(serial: &mut Port, pid: u16, non_interactive: bool) -> Resu } if let Some(bytes) = handle_key_event(key) { - serial.write_all(&bytes).into_diagnostic()?; - serial.flush().into_diagnostic()?; + serial + .write_all(&bytes) + .ignore_timeout() + .into_diagnostic()?; + serial.flush().ignore_timeout().into_diagnostic()?; } } Ok(true) } +trait ErrorExt { + fn ignore_timeout(self) -> Self; +} + +impl ErrorExt for Result<(), io::Error> { + fn ignore_timeout(self) -> Self { + match self { + Ok(_) => Ok(()), + Err(e) if e.kind() == ErrorKind::TimedOut => Ok(()), + Err(e) => Err(e), + } + } +} + fn key_event() -> std::io::Result> { if !poll(Duration::ZERO)? { return Ok(None);