wezterm icon indicating copy to clipboard operation
wezterm copied to clipboard

portable-pty example not working properly on Windows

Open CyriacBr opened this issue 2 years ago • 2 comments

(I omittedthe bug report form as this issue concerns portable-pty)

I noticed that portable-pty behaves very differently on Windows. When running the whoami example, no outpud is read, and the child exits with an error code:

use portable_pty::{CommandBuilder, NativePtySystem, PtySize, PtySystem};

fn main() {
    let pty_system = NativePtySystem::default();

    let pair = pty_system
        .openpty(PtySize {
            rows: 24,
            cols: 80,
            pixel_width: 0,
            pixel_height: 0,
        })
        .unwrap();

    let cmd = CommandBuilder::new("whoami");
    let mut child = pair.slave.spawn_command(cmd).unwrap();
    drop(pair.slave);

    let mut reader = pair.master.try_clone_reader().unwrap();
    drop(pair.master);

    // Consume the output from the child
    let mut s = String::new();
    reader.read_to_string(&mut s).unwrap();

    print!("output: ");
    for c in s.escape_debug() {
        print!("{}", c);
    }

    println!("child status: {:?}", child.wait().unwrap());
}

This is the output:

output: child status: ExitStatus { successful: false }

I found that it only works when waiting for the child before reading:


    let cmd = CommandBuilder::new("whoami");
    let mut child = pair.slave.spawn_command(cmd).unwrap();
    child.wait().unwrap();
    drop(pair.slave);

But this is problematic because this makes it directly impossible to handle very long-running commands (e.g a python or node script that infinitely runs something on interval). It's easier to handle such a case through the reader directly with either a timeout or a limit of characters/lines to read.

So first of all it seems either there's an issue with portable_pty or with the windows computer I have. Hopefully you have an idea of what's happening, thanks :)

CyriacBr avatar Dec 13 '21 13:12 CyriacBr

I think I have the same problem on Windows server 2019.

bbigras avatar Aug 06 '22 18:08 bbigras

I've made a couple of changes in master and updated the example. I haven't published this version to crates.io yet.

https://github.com/wez/wezterm/blob/main/pty/examples/whoami.rs

wez avatar Aug 17 '22 14:08 wez