@@ -15,14 +15,17 @@ impl NonBlockStdIo {
15
15
let ( tx_in, rx_in) = tokio:: sync:: mpsc:: channel ( 100 ) ;
16
16
let ( tx_out, mut rx_out) = tokio:: sync:: mpsc:: channel ( 100 ) ;
17
17
18
+ let stdin_cancel_token = cancellation_token. clone ( ) ;
19
+ let stdout_cancel_token = cancellation_token. clone ( ) ;
20
+
18
21
std:: thread:: spawn ( move || {
19
22
for line_result in std:: io:: stdin ( ) . lines ( ) {
20
23
let line = match line_result {
21
24
Ok ( line) => line,
22
25
Err ( e) => {
23
26
error ! ( "[Proxy] Failed to read from stdin: {e:?}" ) ;
24
- cancellation_token . cancel ( ) ;
25
- "" . to_string ( )
27
+ stdin_cancel_token . cancel ( ) ;
28
+ break ;
26
29
}
27
30
} ;
28
31
@@ -32,15 +35,17 @@ impl NonBlockStdIo {
32
35
Ok ( data) => data,
33
36
Err ( e) => {
34
37
error ! ( "[Proxy] Failed to deserialize json: {e:?}" ) ;
35
- cancellation_token . cancel ( ) ;
36
- continue ;
38
+ stdin_cancel_token . cancel ( ) ;
39
+ break ;
37
40
}
38
41
} ;
39
42
40
43
match tx_in. blocking_send ( data) {
41
44
Ok ( _) => { }
42
45
Err ( e) => {
43
46
error ! ( "[Proxy] Failed to send data: {e:?}" ) ;
47
+ stdin_cancel_token. cancel ( ) ;
48
+ break ;
44
49
}
45
50
}
46
51
}
@@ -49,24 +54,27 @@ impl NonBlockStdIo {
49
54
std:: thread:: spawn ( move || {
50
55
loop {
51
56
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| {
53
58
error ! ( "[Proxy] Couldn't serialize data: {e:?}" ) ;
59
+ stdout_cancel_token. cancel ( ) ;
54
60
"" . to_string ( )
55
- } ) ;
56
-
57
- data. push ( '\n' ) ;
61
+ } ) + "\n " ;
58
62
59
63
match std:: io:: stdout ( ) . write_all ( data. as_bytes ( ) ) {
60
64
Ok ( _) => { }
61
65
Err ( e) => {
62
66
error ! ( "[Proxy] Failed to write data to stdout: {e:?}" ) ;
67
+ stdout_cancel_token. cancel ( ) ;
68
+ break ;
63
69
}
64
70
}
65
71
66
72
match std:: io:: stdout ( ) . flush ( ) {
67
73
Ok ( _) => { }
68
74
Err ( e) => {
69
75
error ! ( "[Proxy] Failed to flush stdout: {e:?}" ) ;
76
+ stdout_cancel_token. cancel ( ) ;
77
+ break ;
70
78
}
71
79
}
72
80
}
0 commit comments