Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

portable-pty example not working properly on Windows #1396

Open
CyriacBr opened this issue Dec 13, 2021 · 2 comments
Open

portable-pty example not working properly on Windows #1396

CyriacBr opened this issue Dec 13, 2021 · 2 comments

Comments

@CyriacBr
Copy link

CyriacBr commented Dec 13, 2021

(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 :)

@bbigras
Copy link

bbigras commented Aug 6, 2022

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

wez added a commit that referenced this issue Aug 12, 2022
This breaking API change allows us to explicitly generate EOF when the
taken writer is dropped.

The examples have been updated to show how to manage read, write
and waiting without deadlock for both linux and windows.
Need to confirm that this is still good on macOS, but my
confidence is high.

I've also removed ssh2 support from this crate as part of this
change. We haven't used it directly in wezterm in a long while
and removing it from here means that there is slightly less code
to keep compiling over and over.

refs: #2392
refs: #1396
@wez
Copy link
Owner

wez commented Aug 17, 2022

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 added a commit that referenced this issue Aug 20, 2022
Looks like we bonus-fixed an issue we didn't know we had as
part of the pty API adjustments in e6421d1

refs: #2434
refs: #1396
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants