Skip to content

Commit

Permalink
Limit retrying flush to port timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
sirhcel committed Jan 13, 2025
1 parent d86c5a7 commit a6cae0f
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/posix/tty.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::mem::MaybeUninit;
use std::os::unix::prelude::*;
use std::path::Path;
use std::time::Duration;
use std::time::{Duration, Instant};
use std::{io, mem};

use nix::fcntl::{fcntl, OFlag};
Expand Down Expand Up @@ -441,10 +441,22 @@ impl io::Write for TTYPort {
}

fn flush(&mut self) -> io::Result<()> {
let timeout = Instant::now() + self.timeout;
loop {
return match nix::sys::termios::tcdrain(self.fd) {
Ok(_) => Ok(()),
Err(nix::errno::Errno::EINTR) => continue,
Err(nix::errno::Errno::EINTR) => {
// Retry flushing. But only up to the ports timeout for not retrying
// indefinitely in case that it gets interrupted again.
if Instant::now() < timeout {
continue;
} else {
Err(io::Error::new(
io::ErrorKind::TimedOut,
"timeout for retrying flush reached",
))
}
}
Err(_) => Err(io::Error::new(io::ErrorKind::Other, "flush failed")),
};
}
Expand Down

0 comments on commit a6cae0f

Please sign in to comment.