You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-commits@axis.apache.org by ro...@apache.org on 2026/01/13 17:18:17 UTC

(axis-axis2-c-core) 02/06: AXIS2C-1605: Fix SSL sockets left in CLOSE_WAIT state

This is an automated email from the ASF dual-hosted git repository.

robertlazarski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/axis-axis2-c-core.git

commit 6c46ee212de3190d645ea3cc9f08d7b508e40338
Author: Robert Lazarski <ro...@gmail.com>
AuthorDate: Tue Jan 13 04:21:00 2026 -1000

    AXIS2C-1605: Fix SSL sockets left in CLOSE_WAIT state
    
    Implement proper bidirectional SSL shutdown to prevent sockets from
    being left in CLOSE_WAIT state when using SSL/TLS connections.
    
    The issue was that SSL_shutdown() was only called once, which sends
    our close_notify but doesn't wait for the peer's close_notify. When
    the peer's close_notify arrives after we've freed the SSL object,
    the socket remains in CLOSE_WAIT state.
    
    Fix: Call SSL_shutdown() twice when the first call returns 0:
    - First call: sends our close_notify to the peer
    - Second call: waits to receive peer's close_notify
    
    This follows OpenSSL best practices for clean connection shutdown.
    Added debug logging for shutdown status to aid troubleshooting.
    
    Related issues:
    - AXIS2C-1237: SSL memory leak
    - AXIS2C-1258: TLS memory leaks
    - AXIS2C-1652: Further SSL socket issues
    
    Co-Authored-By: Claude Opus 4.5 <no...@anthropic.com>
---
 src/core/transport/http/sender/ssl/ssl_utils.c | 36 +++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/src/core/transport/http/sender/ssl/ssl_utils.c b/src/core/transport/http/sender/ssl/ssl_utils.c
index a0a2aaa45..e4d8c6f77 100644
--- a/src/core/transport/http/sender/ssl/ssl_utils.c
+++ b/src/core/transport/http/sender/ssl/ssl_utils.c
@@ -317,9 +317,39 @@ axis2_ssl_utils_cleanup_ssl(
 
     if (ssl)
     {
-        /* SSL_shutdown returns 0 if not yet complete, 1 if complete, <0 on error.
-         * We should always free the SSL object regardless of shutdown result. */
-        SSL_shutdown(ssl);
+        int shutdown_ret;
+
+        /* AXIS2C-1605 FIX: Proper bidirectional SSL shutdown
+         *
+         * SSL_shutdown() return values:
+         *   0  = Shutdown in progress (sent close_notify, awaiting peer's)
+         *   1  = Shutdown complete (bidirectional)
+         *   <0 = Error
+         *
+         * To prevent sockets from being left in CLOSE_WAIT state, we must
+         * call SSL_shutdown() twice when the first call returns 0:
+         * - First call: sends our close_notify to the peer
+         * - Second call: waits to receive peer's close_notify
+         *
+         * Without the second call, the peer's close_notify arrives after
+         * we've freed the SSL object, leaving the socket in CLOSE_WAIT.
+         */
+        shutdown_ret = SSL_shutdown(ssl);
+
+        if (shutdown_ret == 0)
+        {
+            /* Shutdown not yet complete - call again to receive peer's close_notify */
+            AXIS2_LOG_DEBUG(env->log, AXIS2_LOG_SI,
+                "[ssl] Performing bidirectional shutdown (waiting for peer close_notify)");
+            SSL_shutdown(ssl);
+        }
+        else if (shutdown_ret < 0)
+        {
+            /* Log error but continue with cleanup */
+            AXIS2_LOG_DEBUG(env->log, AXIS2_LOG_SI,
+                "[ssl] SSL_shutdown returned error: %d", shutdown_ret);
+        }
+
         SSL_free(ssl);
     }
     if (ctx)