[nginx] Fixed small inconsistency in handling EOF among receive functions.

Valentin Bartenev vbart at nginx.com
Fri Apr 8 13:45:42 UTC 2016


details:   http://hg.nginx.org/nginx/rev/dc1ae0056a1e
branches:  
changeset: 6506:dc1ae0056a1e
user:      Valentin Bartenev <vbart at nginx.com>
date:      Fri Apr 08 16:39:49 2016 +0300
description:
Fixed small inconsistency in handling EOF among receive functions.

Now all functions always drop the ready flag in this case.

diffstat:

 src/os/unix/ngx_readv_chain.c |  48 ++++++++++++++++++++----------------------
 src/os/unix/ngx_recv.c        |  35 ++++++++++++++++---------------
 2 files changed, 41 insertions(+), 42 deletions(-)

diffs (139 lines):

diff -r 5ad379eab6fa -r dc1ae0056a1e src/os/unix/ngx_readv_chain.c
--- a/src/os/unix/ngx_readv_chain.c	Fri Apr 08 16:38:42 2016 +0300
+++ b/src/os/unix/ngx_readv_chain.c	Fri Apr 08 16:39:49 2016 +0300
@@ -106,7 +106,27 @@ ngx_readv_chain(ngx_connection_t *c, ngx
     do {
         n = readv(c->fd, (struct iovec *) vec.elts, vec.nelts);
 
-        if (n >= 0) {
+        if (n == 0) {
+            rev->ready = 0;
+            rev->eof = 1;
+
+#if (NGX_HAVE_KQUEUE)
+
+            /*
+             * on FreeBSD readv() may return 0 on closed socket
+             * even if kqueue reported about available data
+             */
+
+            if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
+                rev->available = 0;
+            }
+
+#endif
+
+            return 0;
+        }
+
+        if (n > 0) {
 
 #if (NGX_HAVE_KQUEUE)
 
@@ -115,7 +135,7 @@ ngx_readv_chain(ngx_connection_t *c, ngx
 
                 /*
                  * rev->available may be negative here because some additional
-                 * bytes may be received between kevent() and recv()
+                 * bytes may be received between kevent() and readv()
                  */
 
                 if (rev->available <= 0) {
@@ -128,37 +148,15 @@ ngx_readv_chain(ngx_connection_t *c, ngx
                     }
                 }
 
-                if (n == 0) {
-
-                    /*
-                     * on FreeBSD recv() may return 0 on closed socket
-                     * even if kqueue reported about available data
-                     */
-
-#if 0
-                    ngx_log_error(NGX_LOG_ALERT, c->log, 0,
-                                  "readv() returned 0 while kevent() reported "
-                                  "%d available bytes", rev->available);
-#endif
-
-                    rev->ready = 0;
-                    rev->eof = 1;
-                    rev->available = 0;
-                }
-
                 return n;
             }
 
-#endif /* NGX_HAVE_KQUEUE */
+#endif
 
             if (n < size && !(ngx_event_flags & NGX_USE_GREEDY_EVENT)) {
                 rev->ready = 0;
             }
 
-            if (n == 0) {
-                rev->eof = 1;
-            }
-
             return n;
         }
 
diff -r 5ad379eab6fa -r dc1ae0056a1e src/os/unix/ngx_recv.c
--- a/src/os/unix/ngx_recv.c	Fri Apr 08 16:38:42 2016 +0300
+++ b/src/os/unix/ngx_recv.c	Fri Apr 08 16:39:49 2016 +0300
@@ -54,7 +54,24 @@ ngx_unix_recv(ngx_connection_t *c, u_cha
         ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
                        "recv: fd:%d %z of %uz", c->fd, n, size);
 
-        if (n >= 0) {
+        if (n == 0) {
+            rev->ready = 0;
+            rev->eof = 1;
+
+            /*
+             * on FreeBSD recv() may return 0 on closed socket
+             * even if kqueue reported about available data
+             */
+
+            if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
+                rev->available = 0;
+            }
+
+            return 0;
+        }
+
+        if (n > 0) {
+
             if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
                 rev->available -= n;
 
@@ -73,18 +90,6 @@ ngx_unix_recv(ngx_connection_t *c, u_cha
                     }
                 }
 
-                if (n == 0) {
-
-                    /*
-                     * on FreeBSD recv() may return 0 on closed socket
-                     * even if kqueue reported about available data
-                     */
-
-                    rev->ready = 0;
-                    rev->eof = 1;
-                    rev->available = 0;
-                }
-
                 return n;
             }
 
@@ -94,10 +99,6 @@ ngx_unix_recv(ngx_connection_t *c, u_cha
                 rev->ready = 0;
             }
 
-            if (n == 0) {
-                rev->eof = 1;
-            }
-
             return n;
         }
 



More information about the nginx-devel mailing list