Skip to content

deadlock on UnixStream pair with ends in separate threads #185

Closed
@dwrensha

Description

@dwrensha

In the following program, I expect the counter ii to grow without bound, as the two threads ping-pong a single byte back and forth:

use futures::{AsyncReadExt, AsyncWriteExt};

fn main() {
    let (mut client_stream, mut server_stream) =
        async_std::os::unix::net::UnixStream::pair().expect("socket pair");

    std::thread::spawn(move || {
        let r: Result<(), Box<dyn std::error::Error>> = futures::executor::block_on(async move {
            loop {
                let mut buf = [0u8];
                server_stream.read_exact(&mut buf[..]).await?;
                server_stream.write_all(&buf).await?;
            }
        });
        r.expect("server");
    });


    let r: Result<(), Box<dyn std::error::Error>> = futures::executor::block_on(async move {
        let mut ii = 0;
        loop {
            dbg!(ii);
            let mut buf = [11u8];
            client_stream.write_all(&buf).await?;
            client_stream.read_exact(&mut buf[..]).await?;
            ii += 1;
        }
    });
    r.expect("client");
}

When I run the program, I find that, contrary to my expectations, it deadlocks after about a second, with ii only climbing to a few thousand.

I'm using the latest nightly on Linux:

$ rustc -vV
rustc 1.39.0-nightly (f0b58fcf0 2019-09-11)
binary: rustc
commit-hash: f0b58fcf03391a91f74224fe38a696d5a5b789d9
commit-date: 2019-09-11
host: x86_64-unknown-linux-gnu
release: 1.39.0-nightly
LLVM version: 9.0

The deadlock also occurs on MacOS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions