Skip to content

Commit fca804c

Browse files
committed
Attempt to write up to 30 times when we get WOULD_BLOCK
1 parent d75494b commit fca804c

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

kic-lib/src/protocol/mod.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::interface::connection_addr::ConnectionInfo;
1+
use crate::{interface::connection_addr::ConnectionInfo};
22
use crate::protocol::raw::Raw;
33
use std::{
44
error::Error,
@@ -20,7 +20,7 @@ use crate::{InstrumentError, Interface};
2020
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
2121

2222
#[allow(unused_imports)] // warn is only used in 'visa' feature
23-
use tracing::{trace, warn};
23+
use tracing::{trace, debug, warn, error};
2424

2525
#[cfg(feature = "visa")]
2626
use visa_rs::{
@@ -180,12 +180,30 @@ impl Read for Protocol {
180180

181181
impl Write for Protocol {
182182
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
183+
const WRITE_ATTEMPT_LIMIT: u8 = 30;
183184
trace!("writing to instrument: '{}'", String::from_utf8_lossy(buf));
184-
match self {
185-
Self::Raw(r) => r.write(buf),
186185

187-
#[cfg(feature = "visa")]
188-
Self::Visa(v) => v.write(buf),
186+
let mut attempts = 0;
187+
loop {
188+
let write_res = match self {
189+
Self::Raw(r) => r.write(buf),
190+
191+
#[cfg(feature = "visa")]
192+
Self::Visa(v) => v.write(buf),
193+
};
194+
195+
attempts += 1;
196+
197+
match &write_res {
198+
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
199+
warn!("Encountered would-block (attempt {attempts})");
200+
if attempts >= WRITE_ATTEMPT_LIMIT {
201+
error!("Unable to write after {attempts} attempts, giving up");
202+
return write_res;
203+
}
204+
}
205+
_ => return write_res,
206+
}
189207
}
190208
}
191209

0 commit comments

Comments
 (0)