Detect streaming client disconnects
@wllenyj is already working on this.
Since Aug 4, 2023.
Assessment
This issue has not been assessed yet.
Description
Description of problem
The async server does not detect if a client disconnects ungracefully from a streaming API. For example, let's say a client wishes to subscribe to updates streamed from the server. The connection is established, and the server starts streaming updates to the client. If the client dies, the server does not immediately realize this and continues to send events.
For example, adapting the async-stream-server file to only read the first message from the client, then echo back an increasing sequence number forever:
async fn echo_stream(
&self,
_ctx: &::ttrpc::r#async::TtrpcContext,
mut s: ::ttrpc::r#async::ServerStream<streaming::EchoPayload, streaming::EchoPayload>,
) -> ::ttrpc::Result<()> {
let mut e = s.recv().await?.unwrap();
loop {
e.seq += 1;
println!("Sleep 1 second to simulate waiting for an event");
sleep(std::time::Duration::from_secs(1)).await;
println!("sending: {}", e.seq);
let result = s.send(&e).await;
if result.is_err() {
println!("error sending item!");
}
}
}
With this client implementation:
async fn echo_stream(cli: streaming_ttrpc::StreamingClient) {
let mut stream = cli.echo_stream(default_ctx()).await.unwrap();
let echo = streaming::EchoPayload {
seq: 0,
msg: "Echo in a stream".to_string(),
..Default::default()
};
stream.send(&echo).await.unwrap();
while let Ok(resp) = stream.recv().await {
println!("{}", resp);
}
}
Expected result
I'm not sure what would be best; some options are:
- Cancel the async future the service method is executing in (like non-streaming async does if the client dies)
- Provide some kind of token that can be selected on (like #180)
- Propagate the write error back to the service method (so errors can be detected when calling
s.send(&e).await;). While I think this makes sense to do regardless, it's a lot more convenient if the client disconnect can be detected before trying to send something to it.
Actual result
Client output:
seq: 5 msg: "Echo in a stream"
seq: 6 msg: "Echo in a stream"
^C <-- client killed
Server output:
... snip ....
Sleep 1 second to simulate waiting for an event
sending: 6
Sleep 1 second to simulate waiting for an event
sending: 7
Sleep 1 second to simulate waiting for an event
[00:00:19.969] (7ffb7073b640) ERROR write_message got error: Socket("Broken pipe (os error 32)")
sending: 8
Sleep 1 second to simulate waiting for an event
[00:00:20.970] (7ffb7073b640) ERROR write_message got error: Socket("Broken pipe (os error 32)")
- Dominant language
- Rust
- Stars
- 374
- Forks
- 64
- Avg merge
- 3d 5h
- Merged PRs (30d)
- 6
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from containerd/ttrpc-rust
-
Async server reorders and drops client-stream DATA framesPossibly taken @shvbsle claimed this 19 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 15/100
containerd/ttrpc-rust#340 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
containerd/ttrpc-rust#338 ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
containerd/ttrpc-rust#317 · 1 comment ·
-
Add single connection support to ServerPossibly taken @KarstenB claimed this 204 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 42/100
containerd/ttrpc-rust#293 ·
-
Can the asynchronous client connect operation support asynchronous mode to avoid connect blocking?Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
containerd/ttrpc-rust#265 · 1 comment ·
All issues in containerd/ttrpc-rust
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
longbridge/nsh#4201 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
moq-dev/moq#10463 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day