Skip to content

Commit a9a81e9

Browse files
sirkrypt01c3t3a
authored andcommitted
fix(async): handle close of packet stream to avoid panic
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.
1 parent ca200e4 commit a9a81e9

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

socketio/src/asynchronous/client/builder.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,10 @@ impl ClientBuilder {
343343
// Use thread to consume items in iterator in order to call callbacks
344344
tokio::runtime::Handle::current().spawn(async move {
345345
let mut stream = socket_clone.as_stream();
346-
loop {
347-
// tries to restart a poll cycle whenever a 'normal' error occurs,
348-
// it just logs on network errors, in case the poll cycle returned
349-
// `Result::Ok`, the server receives a close frame so it's safe to
350-
// terminate
351-
if let Some(e @ Err(Error::IncompleteResponseFromEngineIo(_))) = stream.next().await
352-
{
353-
trace!("Network error occured: {}", e.unwrap_err());
346+
// Consume the stream until it returns None and the stream is closed.
347+
while let Some(item) = stream.next().await {
348+
if let e @ Err(Error::IncompleteResponseFromEngineIo(_)) = item {
349+
trace!("Network error occurred: {}", e.unwrap_err());
354350
}
355351
}
356352
});

0 commit comments

Comments
 (0)