Skip to content

Commit e11015f

Browse files
fix: Handles non utf8 stderr and stdout (#272)
* Closes #270 Co-authored-by: Andrés Quintero <andres@ixpantia.com>
1 parent ad3cf92 commit e11015f

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/client/worker.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use tokio::{
2222
task::JoinHandle,
2323
};
2424
use tokio_stream::StreamExt;
25-
use tokio_util::codec::{FramedRead, LinesCodec};
25+
use tokio_util::codec::FramedRead;
2626

2727
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, serde::Deserialize)]
2828
pub enum WorkerType {
@@ -45,36 +45,49 @@ pub fn log_stdio(mut child: Child, target: &'static str) -> FaucetResult<Child>
4545
child.stdout.take().ok_or(FaucetError::Unknown(format!(
4646
"Unable to take stdout from PID {pid}"
4747
)))?,
48-
LinesCodec::new(),
48+
tokio_util::codec::AnyDelimiterCodec::new(vec![b'\n'], vec![]),
4949
);
5050

5151
let mut stderr = FramedRead::new(
5252
child.stderr.take().ok_or(FaucetError::Unknown(format!(
5353
"Unable to take stderr from PID {pid}"
5454
)))?,
55-
LinesCodec::new(),
55+
tokio_util::codec::AnyDelimiterCodec::new(vec![b'\n'], vec![]),
5656
);
5757

5858
tokio::spawn(async move {
5959
while let Some(line) = stderr.next().await {
60-
if let Ok(line) = line {
61-
match parse_faucet_event(&line) {
62-
FaucetEventResult::Output(line) => log::warn!(target: target, "{line}"),
63-
FaucetEventResult::Event(e) => {
64-
send_log_event(e);
65-
}
66-
FaucetEventResult::EventError(e) => {
67-
log::error!(target: target, "{e:?}")
60+
match line {
61+
Ok(line) => match std::str::from_utf8(&line) {
62+
Ok(line) => match parse_faucet_event(&line) {
63+
FaucetEventResult::Output(line) => log::warn!(target: target, "{line}"),
64+
FaucetEventResult::Event(e) => {
65+
send_log_event(e);
66+
}
67+
FaucetEventResult::EventError(e) => {
68+
log::error!(target: target, "{e:?}")
69+
}
70+
},
71+
Err(e) => {
72+
log::warn!(target: target, "Unable to parse non-utf8 stderr output: {e}")
6873
}
69-
}
74+
},
75+
Err(e) => log::error!(target: target, "{e}"),
7076
}
7177
}
7278
});
7379

7480
tokio::spawn(async move {
7581
while let Some(line) = stdout.next().await {
7682
if let Ok(line) = line {
77-
log::info!(target: target, "{line}");
83+
match std::str::from_utf8(&line) {
84+
Ok(line) => {
85+
log::info!(target: target, "{line}");
86+
}
87+
Err(e) => {
88+
log::warn!(target: target, "Unable to parse non-utf8 stdout output: {e}")
89+
}
90+
}
7891
}
7992
}
8093
});
@@ -238,6 +251,7 @@ fn spawn_shiny_worker(config: &WorkerConfig) -> FaucetResult<Child> {
238251
port = config.addr.port(),
239252
app_dir = config.app_dir.unwrap_or(".")
240253
);
254+
241255
let child = spawn_child_rscript_process(config, command)?;
242256

243257
log_stdio(child, config.target)

0 commit comments

Comments
 (0)