[nginx] Mail: added missing event handling after reading data.

Maxim Dounin mdounin at mdounin.ru
Fri Mar 5 15:31:41 UTC 2021


details:   https://hg.nginx.org/nginx/rev/da0a85e91587
branches:  
changeset: 7790:da0a85e91587
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Fri Mar 05 17:16:17 2021 +0300
description:
Mail: added missing event handling after reading data.

If we need to be notified about further events, ngx_handle_read_event()
needs to be called after a read event is processed.  Without this,
an event can be removed from the kernel and won't be reported again,
notably when using oneshot event methods, such as eventport on Solaris.

For consistency, existing ngx_handle_read_event() call removed from
ngx_mail_read_command(), as this call only covers one of the code paths
where ngx_mail_read_command() returns NGX_AGAIN.  Instead, appropriate
processing added to the callers, covering all code paths where NGX_AGAIN
is returned.

diffstat:

 src/mail/ngx_mail_handler.c      |   5 -----
 src/mail/ngx_mail_imap_handler.c |  16 +++++++++++++++-
 src/mail/ngx_mail_pop3_handler.c |  16 +++++++++++++++-
 src/mail/ngx_mail_proxy_module.c |  30 ++++++++++++++++++++++++++++++
 src/mail/ngx_mail_smtp_handler.c |  16 +++++++++++++++-
 5 files changed, 75 insertions(+), 8 deletions(-)

diffs (189 lines):

diff -r ab6257dac2a8 -r da0a85e91587 src/mail/ngx_mail_handler.c
--- a/src/mail/ngx_mail_handler.c	Fri Mar 05 17:16:16 2021 +0300
+++ b/src/mail/ngx_mail_handler.c	Fri Mar 05 17:16:17 2021 +0300
@@ -722,11 +722,6 @@ ngx_mail_read_command(ngx_mail_session_t
     }
 
     if (n == NGX_AGAIN) {
-        if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
-            ngx_mail_session_internal_server_error(s);
-            return NGX_ERROR;
-        }
-
         if (s->buffer->pos == s->buffer->last) {
             return NGX_AGAIN;
         }
diff -r ab6257dac2a8 -r da0a85e91587 src/mail/ngx_mail_imap_handler.c
--- a/src/mail/ngx_mail_imap_handler.c	Fri Mar 05 17:16:16 2021 +0300
+++ b/src/mail/ngx_mail_imap_handler.c	Fri Mar 05 17:16:17 2021 +0300
@@ -136,7 +136,16 @@ ngx_mail_imap_auth_state(ngx_event_t *re
 
     rc = ngx_mail_read_command(s, c);
 
-    if (rc == NGX_AGAIN || rc == NGX_ERROR) {
+    if (rc == NGX_AGAIN) {
+        if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+            ngx_mail_session_internal_server_error(s);
+            return;
+        }
+
+        return;
+    }
+
+    if (rc == NGX_ERROR) {
         return;
     }
 
@@ -299,6 +308,11 @@ ngx_mail_imap_auth_state(ngx_event_t *re
         }
     }
 
+    if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+        ngx_mail_session_internal_server_error(s);
+        return;
+    }
+
     ngx_mail_send(c->write);
 }
 
diff -r ab6257dac2a8 -r da0a85e91587 src/mail/ngx_mail_pop3_handler.c
--- a/src/mail/ngx_mail_pop3_handler.c	Fri Mar 05 17:16:16 2021 +0300
+++ b/src/mail/ngx_mail_pop3_handler.c	Fri Mar 05 17:16:17 2021 +0300
@@ -151,7 +151,16 @@ ngx_mail_pop3_auth_state(ngx_event_t *re
 
     rc = ngx_mail_read_command(s, c);
 
-    if (rc == NGX_AGAIN || rc == NGX_ERROR) {
+    if (rc == NGX_AGAIN) {
+        if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+            ngx_mail_session_internal_server_error(s);
+            return;
+        }
+
+        return;
+    }
+
+    if (rc == NGX_ERROR) {
         return;
     }
 
@@ -281,6 +290,11 @@ ngx_mail_pop3_auth_state(ngx_event_t *re
             s->arg_start = s->buffer->start;
         }
 
+        if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+            ngx_mail_session_internal_server_error(s);
+            return;
+        }
+
         ngx_mail_send(c->write);
     }
 }
diff -r ab6257dac2a8 -r da0a85e91587 src/mail/ngx_mail_proxy_module.c
--- a/src/mail/ngx_mail_proxy_module.c	Fri Mar 05 17:16:16 2021 +0300
+++ b/src/mail/ngx_mail_proxy_module.c	Fri Mar 05 17:16:17 2021 +0300
@@ -233,6 +233,11 @@ ngx_mail_proxy_pop3_handler(ngx_event_t 
     rc = ngx_mail_proxy_read_response(s, 0);
 
     if (rc == NGX_AGAIN) {
+        if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+            ngx_mail_proxy_internal_server_error(s);
+            return;
+        }
+
         return;
     }
 
@@ -314,6 +319,11 @@ ngx_mail_proxy_pop3_handler(ngx_event_t 
         return;
     }
 
+    if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+        ngx_mail_proxy_internal_server_error(s);
+        return;
+    }
+
     s->proxy->buffer->pos = s->proxy->buffer->start;
     s->proxy->buffer->last = s->proxy->buffer->start;
 }
@@ -346,6 +356,11 @@ ngx_mail_proxy_imap_handler(ngx_event_t 
     rc = ngx_mail_proxy_read_response(s, s->mail_state);
 
     if (rc == NGX_AGAIN) {
+        if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+            ngx_mail_proxy_internal_server_error(s);
+            return;
+        }
+
         return;
     }
 
@@ -448,6 +463,11 @@ ngx_mail_proxy_imap_handler(ngx_event_t 
         return;
     }
 
+    if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+        ngx_mail_proxy_internal_server_error(s);
+        return;
+    }
+
     s->proxy->buffer->pos = s->proxy->buffer->start;
     s->proxy->buffer->last = s->proxy->buffer->start;
 }
@@ -482,6 +502,11 @@ ngx_mail_proxy_smtp_handler(ngx_event_t 
     rc = ngx_mail_proxy_read_response(s, s->mail_state);
 
     if (rc == NGX_AGAIN) {
+        if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+            ngx_mail_proxy_internal_server_error(s);
+            return;
+        }
+
         return;
     }
 
@@ -763,6 +788,11 @@ ngx_mail_proxy_smtp_handler(ngx_event_t 
         return;
     }
 
+    if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+        ngx_mail_proxy_internal_server_error(s);
+        return;
+    }
+
     s->proxy->buffer->pos = s->proxy->buffer->start;
     s->proxy->buffer->last = s->proxy->buffer->start;
 }
diff -r ab6257dac2a8 -r da0a85e91587 src/mail/ngx_mail_smtp_handler.c
--- a/src/mail/ngx_mail_smtp_handler.c	Fri Mar 05 17:16:16 2021 +0300
+++ b/src/mail/ngx_mail_smtp_handler.c	Fri Mar 05 17:16:17 2021 +0300
@@ -462,7 +462,16 @@ ngx_mail_smtp_auth_state(ngx_event_t *re
 
     rc = ngx_mail_read_command(s, c);
 
-    if (rc == NGX_AGAIN || rc == NGX_ERROR) {
+    if (rc == NGX_AGAIN) {
+        if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+            ngx_mail_session_internal_server_error(s);
+            return;
+        }
+
+        return;
+    }
+
+    if (rc == NGX_ERROR) {
         return;
     }
 
@@ -574,6 +583,11 @@ ngx_mail_smtp_auth_state(ngx_event_t *re
             s->arg_start = s->buffer->pos;
         }
 
+        if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
+            ngx_mail_session_internal_server_error(s);
+            return;
+        }
+
         ngx_mail_send(c->write);
     }
 }


More information about the nginx-devel mailing list