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

Retry Interruptible read error #9

Merged
merged 1 commit into from
Sep 14, 2024
Merged
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
22 changes: 15 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod flat_map_err;

use crate::flat_map_err::FlatMapErr;
use core::panic;
use crossbeam::channel::{bounded, select, Receiver, TrySendError};
use eyre::{eyre, Result};
use std::{
Expand Down Expand Up @@ -31,7 +32,7 @@ fn io_streams<W: Write + Send + 'static>(
/// file or thread operations fail.
///
/// # Panics
/// Errors reading from stdin moving its data.
/// Errors reading from stdin other than `ErrorKind::Interrupted`.
pub fn run(
cmd: &str,
args: &[&str],
Expand All @@ -53,7 +54,7 @@ pub fn run(
match child.stdin.take() {
Some(child_in) => {
let in_file = File::create(stdin_log_path.unwrap())?;
let mut stdin_outputs: Vec<Box<dyn Write + Send>> =
let mut in_writers: Vec<Box<dyn Write + Send>> =
vec![Box::new(child_in), Box::new(in_file)];

// Create a channel to send data from stdin to the cancellable_tee thread.
Expand All @@ -65,11 +66,18 @@ pub fn run(
thread::spawn(move || {
let mut buffer = [0u8; 1024];
loop {
let n = stdin().read(&mut buffer).unwrap();
if n == 0 {
break;
match stdin().read(&mut buffer) {
Ok(n) => {
if n == 0 {
break;
}
t_in.send((buffer, n)).unwrap();
}
Err(e) => match e.kind() {
std::io::ErrorKind::Interrupted => continue,
_ => panic!("{e:?}"),
},
}
t_in.send((buffer, n)).unwrap();
}
});

Expand All @@ -80,7 +88,7 @@ pub fn run(
// after the child process has exited. This will prevent the process from exiting.
// So we have to cancel the tee thread when the child process exits and before
// joining on the tee thread.
s.spawn(move || cancellable_tee(&r_cancel, &r_in, &mut stdin_outputs[..]));
s.spawn(move || cancellable_tee(&r_cancel, &r_in, &mut in_writers[..]));

if let Some(child_out) = child.stdout.take() {
s.spawn(move || tee(child_out, &mut out_writers[..]));
Expand Down