From ad40037c807b2018a35471488475c7bad6a7afd3 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Thu, 5 May 2016 09:06:09 +0800 Subject: [PATCH] recv revert treating zero as hangup While checking with ab, I found commit 30cdb3ac8f24a4c289a3b4e8a41d6c5f2e6ec959 Author: Justin Chen Date: Thu Apr 14 21:40:53 2016 +0800 recv treat zero return as error https://github.com/warmcat/libwebsockets/issues/475 turned ab performance to crap, reverting it made everything fast again. recv manpage says there is three ways to get zero returned 1) When a stream socket peer has performed an orderly shutdown, the return value will be 0 (the traditional "end-of-file" return). 2) Datagram sockets in various domains (e.g., the UNIX and Internet domains) permit zero-length datagrams. When such a datagram is received, the return value is 0. 3) The value 0 may also be returned if the requested number of bytes to receive from a stream socket was 0. we can't just assume it means the peer shut down. If the peer shut down, then the event loop should get an event on the socket like POLLHUP and deal with it that way. So the patch mentioned above is simply reverted here. Signed-off-by: Andy Green --- lib/output.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/output.c b/lib/output.c index 50ec69ea..3dbe8bb1 100644 --- a/lib/output.c +++ b/lib/output.c @@ -636,7 +636,7 @@ lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len) int n; n = recv(wsi->sock, (char *)buf, len, 0); - if (n > 0) { + if (n >= 0) { if (wsi->vhost) wsi->vhost->rx += n; return n; @@ -647,7 +647,7 @@ lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len) LWS_ERRNO == LWS_EINTR) return LWS_SSL_CAPABLE_MORE_SERVICE; #endif - lwsl_debug("error on reading from skt\n"); + lwsl_notice("error on reading from skt : %d\n", LWS_ERRNO); return LWS_SSL_CAPABLE_ERROR; }