quinn-rs / quinn

Async-friendly QUIC implementation in Rust
Apache License 2.0
3.85k stars 394 forks source link

Read event on already finalized stream #1587

Closed serzhiio closed 1 year ago

serzhiio commented 1 year ago

I can't get it, why there is read event and unknown stream Err on .recv_stream(stream_id) on a stream already finalized after previous read. (I use one stream per message)

[2023-06-08T10:57:24.975748Z TRACE thread::eventloop::iouring::uni::quic::quinn::instance] Server evt: StreamEvent!
[2023-06-08T10:57:24.975838Z TRACE thread::eventloop::iouring::uni::quic::quinn::instance] - now opened unidirectional | client unidirectional stream 3
[2023-06-08T10:57:24.976467Z TRACE thread::eventloop::iouring::uni::quic::quinn::instance] - read client unidirectional stream 3,None
[2023-06-08T10:57:24.976504Z TRACE thread::eventloop::iouring::uni::quic::quinn::instance] - - read loop iter (next chunk)
[2023-06-08T10:57:24.976520Z TRACE thread::eventloop::iouring::uni::quic::quinn::instance] - - read loop iter (next chunk)
[2023-06-08T10:57:24.976535Z TRACE thread::eventloop::iouring::uni::quic::quinn::instance] - - read loop iter (next chunk)
[2023-06-08T10:57:24.976553Z DEBUG server::app] Worker:0 CH:None size:276bytes.
[2023-06-08T10:57:24.976685Z TRACE thread::eventloop::iouring::uni::quic::quinn::instance] - - should transmit
[2023-06-08T10:57:24.976706Z TRACE thread::eventloop::iouring::uni::quic::quinn::instance] Server evt: StreamEvent!
[2023-06-08T10:57:24.976720Z TRACE thread::eventloop::iouring::uni::quic::quinn::instance] - readable client unidirectional stream 3
[2023-06-08T10:57:24.976736Z TRACE thread::eventloop::iouring::uni::quic::quinn::instance] - read client unidirectional stream 3,None
[2023-06-08T10:57:24.976752Z ERROR thread::eventloop::iouring::uni::quic::quinn::instance] recv_stream(client unidirectional stream 3,None): unknown stream
serzhiio commented 1 year ago

It's worth to say i try to read stream immediately after it opened and not waiting for read event.

Ralith commented 1 year ago

This is currently expected behavior. We queue Readable events when processing a packet, and multiple packets may be processed in series. This can lead to an Opened event being immediately followed by Readable events, or even multiple Readable events. If you read the stream in full after the first Opened or Readable event, you will see UnknownStream on any future access to that stream, as might be prompted by the following already-queued events.

You could handle UnknownStream errors by skipping the event, or do what the quinn crate does and consume all stream events in one go in a way that combines duplicates (e.g. setting a flag in your application-layer stream state) before attempting to manipulate a stream in response.