1- use crate :: interface:: connection_addr:: ConnectionInfo ;
1+ use crate :: { interface:: connection_addr:: ConnectionInfo } ;
22use crate :: protocol:: raw:: Raw ;
33use std:: {
44 error:: Error ,
@@ -20,7 +20,7 @@ use crate::{InstrumentError, Interface};
2020use 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" ) ]
2626use visa_rs:: {
@@ -180,12 +180,30 @@ impl Read for Protocol {
180180
181181impl 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