[PATCH 5 of 5] Mail: fixed occasional mail proxy errors on Windows
Maxim Dounin
mdounin at mdounin.ru
Wed Mar 18 12:48:00 UTC 2026
# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1773836283 -10800
# Wed Mar 18 15:18:03 2026 +0300
# Node ID 966eeb7c1bea317f2144ada6411751646ee2787c
# Parent 54cdb3f960974e519bfd951c8d225676bba95ac1
Mail: fixed occasional mail proxy errors on Windows.
On Windows, in some cases a read event is reported by select() before a
write event signals that a connection to a backend server is established.
This results in c->write->ready not being set when the read handler is
called, and subsequent c->send() returns NGX_AGAIN, as ngx_wsasend()
does not do anything unless c->write->ready is set. And the connection
establishment code assumes that short initial commands can be sent
without blocking, further resulting in an internal error being reported
to the client.
It is not clear why this happens, though it was observed in practice when
running tests on Windows 10 (22H2).
The fix is to block read handlers from doing anything unless c->write->ready
is set, much like we do when sending the PROXY protocol header.
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
@@ -248,7 +248,7 @@ ngx_mail_proxy_pop3_handler(ngx_event_t
return;
}
- if (s->proxy->proxy_protocol) {
+ if (s->proxy->proxy_protocol || !c->write->ready) {
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "mail proxy pop3 busy");
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
@@ -398,7 +398,7 @@ ngx_mail_proxy_imap_handler(ngx_event_t
return;
}
- if (s->proxy->proxy_protocol) {
+ if (s->proxy->proxy_protocol || !c->write->ready) {
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "mail proxy imap busy");
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
@@ -571,7 +571,7 @@ ngx_mail_proxy_smtp_handler(ngx_event_t
return;
}
- if (s->proxy->proxy_protocol) {
+ if (s->proxy->proxy_protocol || !c->write->ready) {
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "mail proxy smtp busy");
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
More information about the nginx-devel
mailing list