From: Vadim Zhestikov Date: Wed, 4 Nov 2026 17:54:11 +0000 (-0800) Subject: Mail: blocked client writes while connecting to upstream X-Git-Tag: release-1.31.9~5 X-Git-Url: https://git.kaiwu.me/http/%7B@url%7D?a=commitdiff_plain;h=013ce187965496fd1a3f7d51ad5477a5573c09b7;p=nginx.git Mail: blocked client writes while connecting to upstream 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. --- diff --git a/src/mail/ngx_mail_proxy_module.c b/src/mail/ngx_mail_proxy_module.c index 4ecc95ea0..edcb91139 100644 --- a/src/mail/ngx_mail_proxy_module.c +++ b/src/mail/ngx_mail_proxy_module.c @@ -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) {