diff mbox

[v3,1/1] io/channel-websock: handle continuous reads without any data

Message ID 20180110153924.17391-1-edgar.kaziakhmedov@virtuozzo.com (mailing list archive)
State New, archived
Headers show

Commit Message

Edgar Kaziakhmedov Jan. 10, 2018, 3:39 p.m. UTC
According to the current implementation of websocket protocol in QEMU,
qio_channel_websock_handshake_io tries to read handshake from the
channel to start communication over socket. But this approach
doesn't cover scenario when socket was closed while handshaking.
Therefore, if G_IO_IN is caught and qio_channel_read returns zero,
error has to be set and connection has to be done.

Such behaviour causes 100% CPU load in main QEMU loop, because main loop
poll continues to receive and handle G_IO_IN events from websocket.

Step to reproduce 100% CPU load:
1) start qemu with the simplest configuration
$ qemu -vnc [::1]:1,websocket=7500
2) open any vnc listener (which doesn't follow websocket
protocol)
$ vncviewer :7500
3) kill listener
4) qemu main thread eats 100% CPU

Signed-off-by: Edgar Kaziakhmedov <edgar.kaziakhmedov@virtuozzo.com>
---
 io/channel-websock.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

--
2.11.0

Comments

Daniel P. Berrangé Jan. 10, 2018, 3:44 p.m. UTC | #1
On Wed, Jan 10, 2018 at 06:39:24PM +0300, Edgar Kaziakhmedov wrote:
> According to the current implementation of websocket protocol in QEMU,
> qio_channel_websock_handshake_io tries to read handshake from the
> channel to start communication over socket. But this approach
> doesn't cover scenario when socket was closed while handshaking.
> Therefore, if G_IO_IN is caught and qio_channel_read returns zero,
> error has to be set and connection has to be done.
> 
> Such behaviour causes 100% CPU load in main QEMU loop, because main loop
> poll continues to receive and handle G_IO_IN events from websocket.
> 
> Step to reproduce 100% CPU load:
> 1) start qemu with the simplest configuration
> $ qemu -vnc [::1]:1,websocket=7500
> 2) open any vnc listener (which doesn't follow websocket
> protocol)
> $ vncviewer :7500
> 3) kill listener
> 4) qemu main thread eats 100% CPU
> 
> Signed-off-by: Edgar Kaziakhmedov <edgar.kaziakhmedov@virtuozzo.com>
> ---
>  io/channel-websock.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)

Thanks, I have queued this patch.


Regards,
Daniel
diff mbox

Patch

diff --git a/io/channel-websock.c b/io/channel-websock.c
index 87ebdebfc0..1152715535 100644
--- a/io/channel-websock.c
+++ b/io/channel-websock.c
@@ -502,9 +502,12 @@  static int qio_channel_websock_handshake_read(QIOChannelWebsock *ioc,
             error_setg(errp,
                        "End of headers not found in first 4096 bytes");
             return 1;
-        } else {
-            return 0;
+        } else if (ret == 0) {
+            error_setg(errp,
+                       "End of headers not found before connection closed");
+            return -1;
         }
+        return 0;
     }
     *handshake_end = '\0';