Skip to content

Commit

Permalink
fix(async): handle close of packet stream to avoid panic
Browse files Browse the repository at this point in the history
When the packet stream closes, e.g. because the server terminated the connection,
the stream returns None. Then, the next() function shouldn't be called again on
the stream, as it is already closed.
Previously, we wouldn't handle this, which lead to a panic.

Before the fix which implemented the as_stream() function for the socket, the program
wouldn't panic, but instead run in an endless loop which isn't ideal either.

Now, the loop which fetches the packets from the stream properly terminates.
However, the program which uses the socket.io client currently isn't notified of the
unexpected closure.
  • Loading branch information
sirkrypt0 authored and 1c3t3a committed Jul 8, 2023
1 parent ca200e4 commit a9a81e9
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions socketio/src/asynchronous/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,10 @@ impl ClientBuilder {
// Use thread to consume items in iterator in order to call callbacks
tokio::runtime::Handle::current().spawn(async move {
let mut stream = socket_clone.as_stream();
loop {
// tries to restart a poll cycle whenever a 'normal' error occurs,
// it just logs on network errors, in case the poll cycle returned
// `Result::Ok`, the server receives a close frame so it's safe to
// terminate
if let Some(e @ Err(Error::IncompleteResponseFromEngineIo(_))) = stream.next().await
{
trace!("Network error occured: {}", e.unwrap_err());
// Consume the stream until it returns None and the stream is closed.
while let Some(item) = stream.next().await {
if let e @ Err(Error::IncompleteResponseFromEngineIo(_)) = item {
trace!("Network error occurred: {}", e.unwrap_err());
}
}
});
Expand Down

0 comments on commit a9a81e9

Please sign in to comment.