[PATCH 4 of 5] Mail: improved error reporting while connecting to upstream

Maxim Dounin mdounin at mdounin.ru
Wed Mar 18 12:47:59 UTC 2026


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1773836280 -10800
#      Wed Mar 18 15:18:00 2026 +0300
# Node ID 54cdb3f960974e519bfd951c8d225676bba95ac1
# Parent  363a706d8c137aa1b3745143889ab8e618852e67
Mail: improved error reporting while connecting to upstream.

Previously, when c->send() failed to fully send a command after connecting
to the upstream server, no error was logged, yet the connection was closed
with an internal error.

diff --git a/src/mail/ngx_mail_proxy_module.c b/src/mail/ngx_mail_proxy_module.c
--- a/src/mail/ngx_mail_proxy_module.c
+++ b/src/mail/ngx_mail_proxy_module.c
@@ -227,6 +227,7 @@ static void
 ngx_mail_proxy_pop3_handler(ngx_event_t *rev)
 {
     u_char                 *p;
+    ssize_t                 n;
     ngx_int_t               rc;
     ngx_str_t               line;
     ngx_connection_t       *c;
@@ -344,11 +345,20 @@ ngx_mail_proxy_pop3_handler(ngx_event_t 
         break;
     }
 
-    if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
+    n = c->send(c, line.data, line.len);
+
+    if (n == NGX_ERROR) {
+        ngx_mail_proxy_internal_server_error(s);
+        return;
+    }
+
+    if (n != (ssize_t) line.len) {
         /*
          * we treat the incomplete sending as NGX_ERROR
          * because it is very strange here
          */
+        ngx_log_error(NGX_LOG_ERR, c->log, 0,
+                      "sent only %z of %uz", n, line.len);
         ngx_mail_proxy_internal_server_error(s);
         return;
     }
@@ -367,6 +377,7 @@ static void
 ngx_mail_proxy_imap_handler(ngx_event_t *rev)
 {
     u_char                 *p;
+    ssize_t                 n;
     ngx_int_t               rc;
     ngx_str_t               line;
     ngx_connection_t       *c;
@@ -505,11 +516,20 @@ ngx_mail_proxy_imap_handler(ngx_event_t 
         break;
     }
 
-    if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
+    n = c->send(c, line.data, line.len);
+
+    if (n == NGX_ERROR) {
+        ngx_mail_proxy_internal_server_error(s);
+        return;
+    }
+
+    if (n != (ssize_t) line.len) {
         /*
          * we treat the incomplete sending as NGX_ERROR
          * because it is very strange here
          */
+        ngx_log_error(NGX_LOG_ERR, c->log, 0,
+                      "sent only %z of %uz", n, line.len);
         ngx_mail_proxy_internal_server_error(s);
         return;
     }
@@ -528,6 +548,7 @@ static void
 ngx_mail_proxy_smtp_handler(ngx_event_t *rev)
 {
     u_char                    *p;
+    ssize_t                    n;
     ngx_int_t                  rc;
     ngx_str_t                  line, auth, encoded;
     ngx_buf_t                 *b;
@@ -842,11 +863,20 @@ ngx_mail_proxy_smtp_handler(ngx_event_t 
         break;
     }
 
-    if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
+    n = c->send(c, line.data, line.len);
+
+    if (n == NGX_ERROR) {
+        ngx_mail_proxy_internal_server_error(s);
+        return;
+    }
+
+    if (n != (ssize_t) line.len) {
         /*
          * we treat the incomplete sending as NGX_ERROR
          * because it is very strange here
          */
+        ngx_log_error(NGX_LOG_ERR, c->log, 0,
+                      "sent only %z of %uz", n, line.len);
         ngx_mail_proxy_internal_server_error(s);
         return;
     }



More information about the nginx-devel mailing list