Skip to content

Commit 40e0f08

Browse files
committed
chore: adjusting error handling in non blocking stdio
1 parent be34d92 commit 40e0f08

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

crates/apollo-mcp-proxy/src/non_blocking_stdio.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ impl NonBlockStdIo {
1515
let (tx_in, rx_in) = tokio::sync::mpsc::channel(100);
1616
let (tx_out, mut rx_out) = tokio::sync::mpsc::channel(100);
1717

18+
let stdin_cancel_token = cancellation_token.clone();
19+
let stdout_cancel_token = cancellation_token.clone();
20+
1821
std::thread::spawn(move || {
1922
for line_result in std::io::stdin().lines() {
2023
let line = match line_result {
2124
Ok(line) => line,
2225
Err(e) => {
2326
error!("[Proxy] Failed to read from stdin: {e:?}");
24-
cancellation_token.cancel();
25-
"".to_string()
27+
stdin_cancel_token.cancel();
28+
break;
2629
}
2730
};
2831

@@ -32,15 +35,17 @@ impl NonBlockStdIo {
3235
Ok(data) => data,
3336
Err(e) => {
3437
error!("[Proxy] Failed to deserialize json: {e:?}");
35-
cancellation_token.cancel();
36-
continue;
38+
stdin_cancel_token.cancel();
39+
break;
3740
}
3841
};
3942

4043
match tx_in.blocking_send(data) {
4144
Ok(_) => {}
4245
Err(e) => {
4346
error!("[Proxy] Failed to send data: {e:?}");
47+
stdin_cancel_token.cancel();
48+
break;
4449
}
4550
}
4651
}
@@ -49,24 +54,27 @@ impl NonBlockStdIo {
4954
std::thread::spawn(move || {
5055
loop {
5156
if let Some(data) = rx_out.blocking_recv() {
52-
let mut data = serde_json::to_string(&data).unwrap_or_else(|e| {
57+
let data = serde_json::to_string(&data).unwrap_or_else(|e| {
5358
error!("[Proxy] Couldn't serialize data: {e:?}");
59+
stdout_cancel_token.cancel();
5460
"".to_string()
55-
});
56-
57-
data.push('\n');
61+
}) + "\n";
5862

5963
match std::io::stdout().write_all(data.as_bytes()) {
6064
Ok(_) => {}
6165
Err(e) => {
6266
error!("[Proxy] Failed to write data to stdout: {e:?}");
67+
stdout_cancel_token.cancel();
68+
break;
6369
}
6470
}
6571

6672
match std::io::stdout().flush() {
6773
Ok(_) => {}
6874
Err(e) => {
6975
error!("[Proxy] Failed to flush stdout: {e:?}");
76+
stdout_cancel_token.cancel();
77+
break;
7078
}
7179
}
7280
}

0 commit comments

Comments
 (0)