vv9k / podman-api-rs

Rust interface to Podman (libpod).
MIT License
81 stars 11 forks source link

Reading part of `Container::attach` seems to be stuck in `next` #141

Closed marhkb closed 1 year ago

marhkb commented 1 year ago

I try to attach to a simple alpine container. For that, I just use the code from the documentation of the Container::attach API like

let container = podman.containers().get("{container}");
let tty_multiplexer = container.attach(&Default::default()).await.unwrap();
let (mut reader, _writer) = tty_multiplexer.split();

while let Some(tty_result) = reader.next().await {
    match tty_result {
        Ok(chunk) => println!("{:?}", chunk),
        Err(e) => eprintln!("Error: {}", e),
    }
}

Additionally, while executing this piece of code, I run podman container attach <container> in Terminal 1 and curl -X POST --unix-socket /run/user/1000/podman/podman.sock http://d/v4.0.0/libpod/containers/{container}/attach in Terminal 2.

Now, when I type some command in Terminal 1 I can see the input and output in Terminal 2, but the rust code is stuck in await.

Writing within in the rust code seems to work, though. When run the following code, I can see the input and output in both Terminal 1 and Terminal 2.

let (_, mut writer) = multiplexer.split();
let len = writer.write(b"date\n").await.unwrap();

Are you able to get the reading part working? I had a look into code, but unfortunately without success.

vv9k commented 1 year ago

The problem was with containers that had TTY allocated. As it turns out the standard Docker format stream is only used for containers without TTY. I added a check in attach to see if container has TTY allocated and if so it uses a different decode function for the incoming stream changing the raw bytes stream into a TtyChunk::Stdout so that the return type stays the same for both stream types.

marhkb commented 1 year ago

This works great. It would be cool if we could also change the return type of Exec::start to match Container::attach. I will open a separate report about that.