[nginx] Events: improved error event handling for UDP sockets.

Dmitry Volyntsev xeioex at nginx.com
Thu Dec 1 12:12:21 UTC 2016


details:   http://hg.nginx.org/nginx/rev/75dbab4ea930
branches:  
changeset: 6806:75dbab4ea930
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Nov 21 16:03:42 2016 +0300
description:
Events: improved error event handling for UDP sockets.

Normally, the epoll module calls the read and write handlers depending
on whether EPOLLIN and EPOLLOUT are reported by epoll_wait().  No error
processing is done in the module, the handlers are expected to get an
error when doing I/O.

If an error event is reported without EPOLLIN and EPOLLOUT, the module
set both EPOLLIN and EPOLLOUT to ensure the error event is handled at
least in one active handler.

This works well unless the error is delivered along with only one of
EPOLLIN or EPOLLOUT, and the corresponding handler does not do any I/O.
For example, it happened when getting EPOLLERR|EPOLLOUT from
epoll_wait() upon receiving "ICMP port unreachable" while proxying UDP.
As the write handler had nothing to send it was not able to detect and
log an error, and did not switch to the next upstream.

The fix is to unconditionally set EPOLLIN and EPOLLOUT in case of an
error event.  In the aforementioned case, this causes the read handler
to be called which does recv() and detects an error.

In addition to the epoll module, analogous changes were made in
devpoll/eventport/poll.

diffstat:

 src/event/modules/ngx_devpoll_module.c   |  10 ++++------
 src/event/modules/ngx_epoll_module.c     |  19 +++++++------------
 src/event/modules/ngx_eventport_module.c |  10 ++++------
 src/event/modules/ngx_poll_module.c      |  10 ++++------
 4 files changed, 19 insertions(+), 30 deletions(-)

diffs (99 lines):

diff -r 52bd8cc17f34 -r 75dbab4ea930 src/event/modules/ngx_devpoll_module.c
--- a/src/event/modules/ngx_devpoll_module.c	Mon Nov 28 19:19:21 2016 +0300
+++ b/src/event/modules/ngx_devpoll_module.c	Mon Nov 21 16:03:42 2016 +0300
@@ -481,13 +481,11 @@ ngx_devpoll_process_events(ngx_cycle_t *
                           fd, event_list[i].events, revents);
         }
 
-        if ((revents & (POLLERR|POLLHUP|POLLNVAL))
-             && (revents & (POLLIN|POLLOUT)) == 0)
-        {
+        if (revents & (POLLERR|POLLHUP|POLLNVAL)) {
+
             /*
-             * if the error events were returned without POLLIN or POLLOUT,
-             * then add these flags to handle the events at least in one
-             * active handler
+             * if the error events were returned, add POLLIN and POLLOUT
+             * to handle the events at least in one active handler
              */
 
             revents |= POLLIN|POLLOUT;
diff -r 52bd8cc17f34 -r 75dbab4ea930 src/event/modules/ngx_epoll_module.c
--- a/src/event/modules/ngx_epoll_module.c	Mon Nov 28 19:19:21 2016 +0300
+++ b/src/event/modules/ngx_epoll_module.c	Mon Nov 21 16:03:42 2016 +0300
@@ -863,6 +863,13 @@ ngx_epoll_process_events(ngx_cycle_t *cy
             ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                            "epoll_wait() error on fd:%d ev:%04XD",
                            c->fd, revents);
+
+            /*
+             * if the error events were returned, add EPOLLIN and EPOLLOUT
+             * to handle the events at least in one active handler
+             */
+
+            revents |= EPOLLIN|EPOLLOUT;
         }
 
 #if 0
@@ -873,18 +880,6 @@ ngx_epoll_process_events(ngx_cycle_t *cy
         }
 #endif
 
-        if ((revents & (EPOLLERR|EPOLLHUP))
-             && (revents & (EPOLLIN|EPOLLOUT)) == 0)
-        {
-            /*
-             * if the error events were returned without EPOLLIN or EPOLLOUT,
-             * then add these flags to handle the events at least in one
-             * active handler
-             */
-
-            revents |= EPOLLIN|EPOLLOUT;
-        }
-
         if ((revents & EPOLLIN) && rev->active) {
 
 #if (NGX_HAVE_EPOLLRDHUP)
diff -r 52bd8cc17f34 -r 75dbab4ea930 src/event/modules/ngx_eventport_module.c
--- a/src/event/modules/ngx_eventport_module.c	Mon Nov 28 19:19:21 2016 +0300
+++ b/src/event/modules/ngx_eventport_module.c	Mon Nov 21 16:03:42 2016 +0300
@@ -540,13 +540,11 @@ ngx_eventport_process_events(ngx_cycle_t
                               (int) event_list[i].portev_object, revents);
             }
 
-            if ((revents & (POLLERR|POLLHUP|POLLNVAL))
-                 && (revents & (POLLIN|POLLOUT)) == 0)
-            {
+            if (revents & (POLLERR|POLLHUP|POLLNVAL)) {
+
                 /*
-                 * if the error events were returned without POLLIN or POLLOUT,
-                 * then add these flags to handle the events at least in one
-                 * active handler
+                 * if the error events were returned, add POLLIN and POLLOUT
+                 * to handle the events at least in one active handler
                  */
 
                 revents |= POLLIN|POLLOUT;
diff -r 52bd8cc17f34 -r 75dbab4ea930 src/event/modules/ngx_poll_module.c
--- a/src/event/modules/ngx_poll_module.c	Mon Nov 28 19:19:21 2016 +0300
+++ b/src/event/modules/ngx_poll_module.c	Mon Nov 21 16:03:42 2016 +0300
@@ -353,13 +353,11 @@ ngx_poll_process_events(ngx_cycle_t *cyc
             continue;
         }
 
-        if ((revents & (POLLERR|POLLHUP|POLLNVAL))
-             && (revents & (POLLIN|POLLOUT)) == 0)
-        {
+        if (revents & (POLLERR|POLLHUP|POLLNVAL)) {
+
             /*
-             * if the error events were returned without POLLIN or POLLOUT,
-             * then add these flags to handle the events at least in one
-             * active handler
+             * if the error events were returned, add POLLIN and POLLOUT
+             * to handle the events at least in one active handler
              */
 
             revents |= POLLIN|POLLOUT;



More information about the nginx-devel mailing list