[PATCH] Worked around EINVAL from shutdown() on NetBSD

Maxim Dounin mdounin at mdounin.ru
Tue Jul 15 13:50:28 UTC 2025


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1752506823 -10800
#      Mon Jul 14 18:27:03 2025 +0300
# Node ID cf229ea45d3469b20449dcbdafd3e29ed2632805
# Parent  a66da54900004f1e8509a6a50786707ab192472c
Worked around EINVAL from shutdown() on NetBSD.

If a connection was reset by the client, shutdown() on NetBSD returns
EINVAL, leading to test failures, as seen in the ssl_conf_command.t
test.

Fix is to ignore EINVAL from shutdown() on NetBSD, similarly to how we
do with EINVAL from setsockopt() on Solaris.

diff --git a/src/core/ngx_connection.c b/src/core/ngx_connection.c
--- a/src/core/ngx_connection.c
+++ b/src/core/ngx_connection.c
@@ -1619,11 +1619,9 @@ ngx_connection_error(ngx_connection_t *c
         return 0;
     }
 
-#if (NGX_SOLARIS)
     if (err == NGX_EINVAL && c->log_error == NGX_ERROR_IGNORE_EINVAL) {
         return 0;
     }
-#endif
 
     if (err == NGX_EMSGSIZE && c->log_error == NGX_ERROR_IGNORE_EMSGSIZE) {
         return 0;
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -3579,6 +3579,10 @@ ngx_http_set_lingering_close(ngx_connect
     }
 
     if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
+#if (defined __NetBSD__)
+        /* NetBSD returns EINVAL if the connection was reset */
+        c->log_error = NGX_ERROR_IGNORE_EINVAL;
+#endif
         ngx_connection_error(c, ngx_socket_errno,
                              ngx_shutdown_socket_n " failed");
         ngx_http_close_request(r, 0);
diff --git a/src/http/v2/ngx_http_v2.c b/src/http/v2/ngx_http_v2.c
--- a/src/http/v2/ngx_http_v2.c
+++ b/src/http/v2/ngx_http_v2.c
@@ -805,6 +805,10 @@ ngx_http_v2_lingering_close(ngx_connecti
     }
 
     if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
+#if (defined __NetBSD__)
+        /* NetBSD returns EINVAL if the connection was reset */
+        c->log_error = NGX_ERROR_IGNORE_EINVAL;
+#endif 
         ngx_connection_error(c, ngx_socket_errno,
                              ngx_shutdown_socket_n " failed");
         ngx_http_close_connection(c);
diff --git a/src/mail/ngx_mail_handler.c b/src/mail/ngx_mail_handler.c
--- a/src/mail/ngx_mail_handler.c
+++ b/src/mail/ngx_mail_handler.c
@@ -1330,6 +1330,10 @@ ngx_mail_lingering_close(ngx_connection_
     }
 
     if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
+#if (defined __NetBSD__)
+        /* NetBSD returns EINVAL if the connection was reset */
+        c->log_error = NGX_ERROR_IGNORE_EINVAL;
+#endif 
         ngx_connection_error(c, ngx_socket_errno,
                              ngx_shutdown_socket_n " failed");
         ngx_mail_close_connection(c);



More information about the nginx-devel mailing list