pty: fixup for macos
e6421d1b72 removed this bit from the
example:
```
-
- // Note that we're waiting until after we've read the output
- // to call `wait` on the process.
- // On macOS Catalina, waiting on the process seems to prevent
- // its output from making it into the pty.
- println!("child status: {:?}", child.wait().unwrap());
```
This commit revisits that and puts in place a differently horrible
solution for macos.
The issue is that if we don't put in a short sleep on macos, then
for a short lived process like `whoami` in this example, we end up
reading output like this:
```
; cargo run --example whoami
Compiling portable-pty v0.8.0 (/Users/wez/wez-personal/wezterm/pty)
Finished dev [unoptimized + debuginfo] target(s) in 0.60s
Running `/Users/wez/wez-personal/wezterm/target/debug/examples/whoami`
child status: ExitStatus { code: 0, signal: None }
output: \r\n^D\u{8}\u{8}wez\r\n%
```
where the EOT that we send on Drop somehow gets *prepended* to the output
that we read from the pty with a couple of BELs thrown in.
I'm not sure WTF is happening on macOS for that to occur; feels like
some kind of race wrt. process startup and initializing the pty in the
system.
The reader has to be started before we close it as well, otherwise
the same issue can occur.
This commit is contained in:
1 parent
e6421d1b72
commit
53febb14ff
2 files changed
+27
-14
No files matched your search
+26
-13
@@ -24,6 +24,19 @@ fn main() {
|
||||
// that we've spawned the child.
|
||||
drop(pair.slave);
|
||||
|
||||
// Read the output in another thread.
|
||||
// This is important because it is easy to encounter a situation
|
||||
// where read/write buffers fill and block either your process
|
||||
// or the spawned process.
|
||||
let (tx, rx) = channel();
|
||||
let mut reader = pair.master.try_clone_reader().unwrap();
|
||||
std::thread::spawn(move || {
|
||||
// Consume the output from the child
|
||||
let mut s = String::new();
|
||||
reader.read_to_string(&mut s).unwrap();
|
||||
tx.send(s).unwrap();
|
||||
});
|
||||
|
||||
{
|
||||
// Obtain the writer.
|
||||
// When the writer is dropped, EOF will be sent to
|
||||
@@ -33,6 +46,19 @@ fn main() {
|
||||
// generated, otherwise you risk deadlocking yourself.
|
||||
let mut writer = pair.master.take_writer().unwrap();
|
||||
|
||||
if cfg!(target_os = "macos") {
|
||||
// macOS quirk: the child and reader must be started and
|
||||
// allowed a brief grace period to run before we allow
|
||||
// the writer to drop. Otherwise, the data we send to
|
||||
// the kernel to trigger EOF is interleaved with the
|
||||
// data read by the reader! WTF!?
|
||||
// This appears to be a race condition for very short
|
||||
// lived processes on macOS.
|
||||
// I'd love to find a more deterministic solution to
|
||||
// this than sleeping.
|
||||
std::thread::sleep(std::time::Duration::from_millis(20));
|
||||
}
|
||||
|
||||
// This example doesn't need to write anything, but if you
|
||||
// want to send data to the child, you'd set `to_write` to
|
||||
// that data and do it like this:
|
||||
@@ -46,19 +72,6 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
// Read the output in another thread.
|
||||
// This is important because it is easy to encounter a situation
|
||||
// where read/write buffers fill and block either your process
|
||||
// or the spawned process.
|
||||
let (tx, rx) = channel();
|
||||
let mut reader = pair.master.try_clone_reader().unwrap();
|
||||
std::thread::spawn(move || {
|
||||
// Consume the output from the child
|
||||
let mut s = String::new();
|
||||
reader.read_to_string(&mut s).unwrap();
|
||||
tx.send(s).unwrap();
|
||||
});
|
||||
|
||||
// Wait for the child to complete
|
||||
println!("child status: {:?}", child.wait().unwrap());
|
||||
|
||||
|
||||
+1
-1
@@ -346,7 +346,7 @@ impl Drop for UnixMasterWriter {
|
||||
// we send a newline followed by EOF.
|
||||
let eot = t.c_cc[libc::VEOF];
|
||||
if eot != 0 {
|
||||
let _ = self.fd.0.write_all(&[b'\r', b'\n', eot]);
|
||||
let _ = self.fd.0.write_all(&[b'\n', eot]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user