Skip to content

Commit 7d06c9b

Browse files
thesapanssddOnTopautofix-ci[bot]
authored
refactor: streamline stdout and stderr handling in process managers (#18)
* refactor: streamline stdout and stderr handling in process managers * refactor: replace tokio::join! with tokio::try_join! for improved error handling in process managers * [autofix.ci] apply automated fixes --------- Co-authored-by: Sandipsinh Rathod <zotbysandip@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent e0f2182 commit 7d06c9b

File tree

3 files changed

+14
-36
lines changed

3 files changed

+14
-36
lines changed

crates/gpmcp-layer-core/src/process_manager_trait.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,14 @@ impl Decoder for Utf8Codec {
198198
}
199199
}
200200
pub async fn stream<A: AsyncReadExt + Unpin + 'static>(
201-
io: &mut A,
201+
io: Option<A>,
202202
out: impl Into<LayerStdio>,
203203
) -> tokio::io::Result<()> {
204-
let mut frames = FramedRead::with_capacity(io, Utf8Codec, 1024);
205-
stream_frames(&mut frames, out.into()).await
204+
if let Some(io) = io {
205+
let mut frames = FramedRead::with_capacity(io, Utf8Codec, 1024);
206+
return stream_frames(&mut frames, out.into()).await;
207+
}
208+
Ok(())
206209
}
207210

208211
async fn stream_frames<R: AsyncReadExt + Unpin>(

crates/gpmcp-layer-unix/src/unix_process_manager.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -216,23 +216,11 @@ impl ProcessManager for UnixProcessManager {
216216
);
217217
}
218218

219-
// Capture and stream stdout - the stream function will detect it's stdout and route accordingly
220-
if let Some(mut stdout) = child.stdout.take() {
221-
tokio::spawn(async move {
222-
if let Err(e) = stream(&mut stdout, out).await {
223-
warn!("Error streaming stdout: {}", e);
224-
}
225-
});
226-
}
219+
let (stdout, stderr) = (child.stdout.take(), child.stderr.take());
227220

228-
// Capture and stream stderr - the stream function will detect it's stderr and route accordingly
229-
if let Some(mut stderr) = child.stderr.take() {
230-
tokio::spawn(async move {
231-
if let Err(e) = stream(&mut stderr, err).await {
232-
warn!("Error streaming stderr: {}", e);
233-
}
234-
});
235-
}
221+
tokio::spawn(
222+
async move { tokio::try_join!(stream(stdout, out), stream(stderr, err)).ok() },
223+
);
236224
Ok(UnixProcessHandle::new(child, command.to_string()))
237225
}
238226
}

crates/gpmcp-layer-windows/src/windows_process_manager.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -275,24 +275,11 @@ impl ProcessManager for WindowsProcessManager {
275275
);
276276
}
277277

278-
// Capture and stream stdout - the stream function will detect it's stdout and route accordingly
279-
if let Some(mut stdout) = child.stdout.take() {
280-
tokio::spawn(async move {
281-
if let Err(e) = stream(&mut stdout, out).await {
282-
warn!("Error streaming stdout: {}", e);
283-
}
284-
});
285-
}
286-
287-
// Capture and stream stderr - the stream function will detect it's stderr and route accordingly
288-
if let Some(mut stderr) = child.stderr.take() {
289-
tokio::spawn(async move {
290-
if let Err(e) = stream(&mut stderr, err).await {
291-
warn!("Error streaming stderr: {}", e);
292-
}
293-
});
294-
}
278+
let (stdout, stderr) = (child.stdout.take(), child.stderr.take());
295279

280+
tokio::spawn(
281+
async move { tokio::try_join!(stream(stdout, out), stream(stderr, err)).ok() },
282+
);
296283
Ok(WindowsProcessHandle::new(child, command.to_string()))
297284
}
298285
}

0 commit comments

Comments
 (0)