Skip to content

Commit cfde2e1

Browse files
committed
refactor: store shared PTY wait errors via Arc
1 parent d3c938e commit cfde2e1

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

crates/pty_terminal/src/terminal.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use portable_pty::{ChildKiller, ExitStatus, MasterPty};
1010

1111
use crate::geo::ScreenSize;
1212

13+
type ChildWaitResult = Result<ExitStatus, Arc<std::io::Error>>;
14+
1315
/// The read half of a PTY connection. Implements [`Read`].
1416
///
1517
/// Reading feeds data through an internal vt100 parser (shared with [`PtyWriter`]),
@@ -32,7 +34,7 @@ pub struct PtyWriter {
3234
/// A cloneable handle to a child process spawned in a PTY.
3335
pub struct ChildHandle {
3436
child_killer: Box<dyn ChildKiller + Send + Sync>,
35-
exit_status: Arc<OnceLock<std::io::Result<ExitStatus>>>,
37+
exit_status: Arc<OnceLock<ChildWaitResult>>,
3638
}
3739

3840
impl Clone for ChildHandle {
@@ -226,10 +228,10 @@ impl ChildHandle {
226228
///
227229
/// Returns an error if waiting for the child process exit status fails.
228230
#[must_use]
229-
pub fn wait(&self) -> std::io::Result<ExitStatus> {
231+
pub fn wait(&self) -> anyhow::Result<ExitStatus> {
230232
match self.exit_status.wait() {
231233
Ok(status) => Ok(status.clone()),
232-
Err(error) => Err(std::io::Error::new(error.kind(), error.to_string())),
234+
Err(error) => Err(anyhow::Error::new(Arc::clone(error))),
233235
}
234236
}
235237

@@ -270,14 +272,14 @@ impl Terminal {
270272
let child_killer = child.clone_killer();
271273
drop(pty_pair.slave); // Critical: drop slave so EOF is signaled when child exits
272274
let master = pty_pair.master;
273-
let exit_status: Arc<OnceLock<std::io::Result<ExitStatus>>> = Arc::new(OnceLock::new());
275+
let exit_status: Arc<OnceLock<ChildWaitResult>> = Arc::new(OnceLock::new());
274276

275277
// Background thread: wait for child to exit, set exit status, then close writer to trigger EOF
276278
thread::spawn({
277279
let writer = Arc::clone(&writer);
278280
let exit_status = Arc::clone(&exit_status);
279281
move || {
280-
let _ = exit_status.set(child.wait());
282+
let _ = exit_status.set(child.wait().map_err(Arc::new));
281283
// Close writer to signal EOF to the reader
282284
*writer.lock().unwrap() = None;
283285
}

crates/pty_terminal_test/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl Reader {
9898
/// # Panics
9999
///
100100
/// Panics if reading from the PTY fails.
101-
pub fn wait_for_exit(&mut self) -> std::io::Result<ExitStatus> {
101+
pub fn wait_for_exit(&mut self) -> anyhow::Result<ExitStatus> {
102102
let mut discard = Vec::new();
103103
self.pty.read_to_end(&mut discard).expect("PTY read_to_end failed");
104104
self.child_handle.wait()

0 commit comments

Comments
 (0)