]> 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)
committerSergey Kandaurov <pluknet@nginx.com>
Wed, 11 Nov 2026 18:08:26 +0000 (22:08 +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 aa3baeb7e3caa571abb7b60c7d48adffdcb9e5a3..b3560e21fa13c5d3676745eff07921a721402aaf 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);
@@ -165,6 +166,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);
@@ -223,6 +225,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)
 {