Skip to content

parse_length_delimited_from_reader doesn't work for TcpStream unless other side calls shutdown(Shutdown::Write) #157

@luser

Description

@luser

I tried to use parse_length_delimited_from_reader for parsing a protobuf Message from a TcpStream, but I think it calls read too many times on the underlying TcpStream and winds up blocking unless the other side calls shutdown(Shutdown::Write). I put together a fairly simple example client/server that demonstrates the problem:
https://github.com/luser/rust-protobuf-network-example/blob/master/src/bin/client.rs
https://github.com/luser/rust-protobuf-network-example/blob/master/src/bin/server.rs
https://github.com/luser/rust-protobuf-network-example/blob/master/proto.proto

With the calls to stream.shutdown() commented out, the client hangs waiting for a response from the server, and the server hangs waiting for more data from the client. With those calls uncommented, everything works.

For this simple case, this workaround would be sufficient, but if I want to have the client and server exchange multiple messages this will not work. I wound up having to manually parse the length, call read_exact, and then call parse_from_bytes, which is quite a bit more code:
https://github.com/luser/sccache2/blob/d9e5315b9da649112ad08c5b15016b13c9a4f5ba/src/client.rs#L51

It seems like in the parse_length_delimited case, rust-protobuf ought to be able to do just this and not attempt to read more bytes than it needs, which would make this scenario much simpler to write.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions