Skip to content

Commit 65189ba

Browse files
committed
fix: avoid blocking writes in write_after_exit test
1 parent 0c7776d commit 65189ba

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

crates/pty_terminal/src/terminal.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ impl PtyReader {
151151
}
152152

153153
impl PtyWriter {
154+
/// Returns `true` if the child process write channel has been closed.
155+
///
156+
/// # Panics
157+
///
158+
/// Panics if the writer lock is poisoned.
159+
#[must_use]
160+
pub fn is_closed(&self) -> bool {
161+
self.writer.lock().unwrap().is_none()
162+
}
163+
154164
/// Writes `line` followed by a platform-appropriate line ending to the child process.
155165
///
156166
/// On Unix, appends `\n`. On Windows `ConPTY`, appends `\r\n` for proper line handling.

crates/pty_terminal/tests/terminal.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ fn write_after_exit() {
122122
let _ = child_handle.wait();
123123

124124
// Writer shutdown is done by a background thread after child wait returns.
125-
// Poll briefly to avoid a race where write occurs before shutdown is observed.
125+
// Poll briefly for the writer state to flip to closed before asserting write failure.
126126
let deadline = Instant::now() + Duration::from_millis(300);
127-
loop {
128-
if pty_writer.write_all(b"too late\n").is_err() {
129-
break;
130-
}
127+
while !pty_writer.is_closed() {
131128
assert!(Instant::now() <= deadline, "writer did not close after child exit");
132129
std::thread::yield_now();
133130
}
131+
132+
let result = pty_writer.write_all(b"too late\n");
133+
assert!(result.is_err());
134134
}
135135

136136
#[test]

0 commit comments

Comments
 (0)