Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle EINTR return code from tcdrain to effectively flush #225

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ project adheres to [Semantic Versioning](https://semver.org/).
[#238](https://github.com/serialport/serialport-rs/pull/238)

### Fixed

* Retry flushing data on `EINTR` up to the ports read/write timeout.
[#225](https://github.com/serialport/serialport-rs/pull/225)

### Removed


Expand Down
23 changes: 20 additions & 3 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,8 +441,25 @@ impl io::Write for TTYPort {
}

fn flush(&mut self) -> io::Result<()> {
nix::sys::termios::tcdrain(self.fd)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "flush failed"))
let timeout = Instant::now() + self.timeout;
loop {
return match nix::sys::termios::tcdrain(self.fd) {
Ok(_) => Ok(()),
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