]> git.kaiwu.me - nginx.git/commitdiff
Mail: blocked client writes while connecting to upstream
authorVadim Zhestikov <v.zhestikov@f5.com>
Wed, 4 Nov 2026 17:54:11 +0000 (09:54 -0800)
committerRoman Arutyunyan <arutyunyan.roman@gmail.com>
Wed, 11 Nov 2026 12:17:01 +0000 (16:17 +0400)
While connecting to the upstream and reading its greeting, the client
read handler is ngx_mail_proxy_block_read(), but the client write
handler was left as it was.  If ngx_handle_write_event() failed in it,
the client connection was closed without the upstream one, and the next
upstream event accessed the freed session.

The client write handler is now ngx_mail_proxy_block_write(), which
closes both connections on errors the same way
ngx_mail_proxy_block_read() does.

src/mail/ngx_mail_proxy_module.c

index 4ecc95ea061a8f52e9ece6c9e74433e1b7329ee1..edcb91139dd03d47bafc06ef57823e3a4d806eb4 100644 (file)
@@ -24,6 +24,7 @@ typedef struct {
 
 
 static void ngx_mail_proxy_block_read(ngx_event_t *rev);
+static void ngx_mail_proxy_block_write(ngx_event_t *wev);
 static void ngx_mail_proxy_pop3_handler(ngx_event_t *rev);
 static void ngx_mail_proxy_imap_handler(ngx_event_t *rev);
 static void ngx_mail_proxy_smtp_handler(ngx_event_t *rev);
@@ -173,6 +174,7 @@ ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_addr_t *peer)
     p->upstream.connection->pool = s->connection->pool;
 
     s->connection->read->handler = ngx_mail_proxy_block_read;
+    s->connection->write->handler = ngx_mail_proxy_block_write;
     p->upstream.connection->write->handler = ngx_mail_proxy_write_handler;
 
     pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
@@ -231,6 +233,23 @@ ngx_mail_proxy_block_read(ngx_event_t *rev)
 }
 
 
+static void
+ngx_mail_proxy_block_write(ngx_event_t *wev)
+{
+    ngx_connection_t    *c;
+    ngx_mail_session_t  *s;
+
+    ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, "mail proxy block write");
+
+    if (ngx_handle_write_event(wev, 0) != NGX_OK) {
+        c = wev->data;
+        s = c->data;
+
+        ngx_mail_proxy_close_session(s);
+    }
+}
+
+
 static void
 ngx_mail_proxy_pop3_handler(ngx_event_t *rev)
 {