diff --git a/crates/maestro-daemon/src/sidecar.rs b/crates/maestro-daemon/src/sidecar.rs index 0b52ab3..8e2762d 100644 --- a/crates/maestro-daemon/src/sidecar.rs +++ b/crates/maestro-daemon/src/sidecar.rs @@ -454,10 +454,13 @@ impl SharedBuf { #[derive(Default)] pub(crate) struct FaultyLogSink { pub(crate) buf: SharedBuf, - /// Accept this many bytes on the next `write` call, then fail the call - /// after — a short write followed by an error, exactly what `write_all` - /// sees from a real fd that dies mid-line. + /// Accept this many bytes on the next `write` call, then fail the + /// *following* call — a short write followed by an error, exactly what + /// `write_all` sees from a real fd that dies mid-line. `Write::write` + /// must not report an error on a call that made partial progress, so + /// the fault is delivered one call later, via `pending_write_error`. pub(crate) fail_write_after: Option, + pub(crate) pending_write_error: bool, pub(crate) fail_flush_once: bool, pub(crate) fail_truncate: bool, } @@ -465,14 +468,24 @@ pub(crate) struct FaultyLogSink { #[cfg(test)] impl Write for FaultyLogSink { fn write(&mut self, data: &[u8]) -> std::io::Result { + if self.pending_write_error { + self.pending_write_error = false; + return Err(std::io::Error::other("fault: write failed mid-line")); + } if let Some(allowed) = self.fail_write_after.take() { let allowed = allowed.min(data.len()); + if allowed == 0 { + // No progress was made, so it's fine to report the error + // from this same call. + return Err(std::io::Error::other("fault: write failed mid-line")); + } self.buf .0 .lock() .unwrap() .extend_from_slice(&data[..allowed]); - return Err(std::io::Error::other("fault: write failed mid-line")); + self.pending_write_error = true; + return Ok(allowed); } self.buf.0.lock().unwrap().extend_from_slice(data); Ok(data.len())