Blame SOURCES/0087-Ticket-48184-clean-up-and-delete-connections-at-shut.patch

b663b9
From 240cfa58c62571b92640a385cfcce6d858cb00dc Mon Sep 17 00:00:00 2001
b663b9
From: Thierry Bordaz <tbordaz@redhat.com>
b663b9
Date: Wed, 30 May 2018 15:48:11 +0200
b663b9
Subject: [PATCH] Ticket 48184 - clean up and delete connections at shutdown
b663b9
 (3rd)
b663b9
b663b9
Bug description:
b663b9
        During shutdown we would not close connections.
b663b9
        In the past this may have just been an annoyance, but now with the way
b663b9
        nunc-stans works, io events can still trigger on open xeisting connectinos
b663b9
        during shutdown.
b663b9
b663b9
Fix Description:
b663b9
        Because of NS dynamic it can happen that several jobs wants to work on the
b663b9
        same connection. In such case (a job is already set in c_job) we delay the
b663b9
        new job that will retry.
b663b9
        In addition:
b663b9
            - some call needed c_mutex
b663b9
            - test uninitialized nunc-stans in case of shutdown while startup is not completed
b663b9
b663b9
	If it is not possible to schedule immediately a job it is sometime useless to wait:
b663b9
		- if the connection is already freed, just cancel the scheduled job
b663b9
		  and do not register a new one
b663b9
		- If we are in middle of a shutdown we do not know if the
b663b9
		  scheduled job is ns_handle_closure, so cancel the scheduled
b663b9
		  job and schedule ns_handle_closure.
b663b9
b663b9
https://pagure.io/389-ds-base/issue/48184
b663b9
b663b9
Reviewed by:
b663b9
            Original fix reviewed by Ludwig and Viktor
b663b9
            Second   fix reviewed by Mark
b663b9
	    Third    fix reviewed by Mark
b663b9
b663b9
Platforms tested: F26
b663b9
b663b9
Flag Day: no
b663b9
b663b9
Doc impact: no
b663b9
---
b663b9
 ldap/servers/slapd/connection.c | 10 +++--
b663b9
 ldap/servers/slapd/conntable.c  |  2 +-
b663b9
 ldap/servers/slapd/daemon.c     | 67 +++++++++++++++++++++++++--------
b663b9
 ldap/servers/slapd/proto-slap.h |  2 +-
b663b9
 4 files changed, 60 insertions(+), 21 deletions(-)
b663b9
b663b9
diff --git a/ldap/servers/slapd/connection.c b/ldap/servers/slapd/connection.c
b663b9
index 76e83112b..c54e7c26c 100644
b663b9
--- a/ldap/servers/slapd/connection.c
b663b9
+++ b/ldap/servers/slapd/connection.c
b663b9
@@ -741,14 +741,18 @@ connection_acquire_nolock(Connection *conn)
b663b9
 
b663b9
 /* returns non-0 if connection can be reused and 0 otherwise */
b663b9
 int
b663b9
-connection_is_free(Connection *conn)
b663b9
+connection_is_free(Connection *conn, int use_lock)
b663b9
 {
b663b9
     int rc;
b663b9
 
b663b9
-    PR_EnterMonitor(conn->c_mutex);
b663b9
+    if (use_lock) {
b663b9
+        PR_EnterMonitor(conn->c_mutex);
b663b9
+    }
b663b9
     rc = conn->c_sd == SLAPD_INVALID_SOCKET && conn->c_refcnt == 0 &&
b663b9
          !(conn->c_flags & CONN_FLAG_CLOSING);
b663b9
-    PR_ExitMonitor(conn->c_mutex);
b663b9
+    if (use_lock) {
b663b9
+        PR_ExitMonitor(conn->c_mutex);
b663b9
+    }
b663b9
 
b663b9
     return rc;
b663b9
 }
b663b9
diff --git a/ldap/servers/slapd/conntable.c b/ldap/servers/slapd/conntable.c
b663b9
index f2f763dfa..114871d17 100644
b663b9
--- a/ldap/servers/slapd/conntable.c
b663b9
+++ b/ldap/servers/slapd/conntable.c
b663b9
@@ -129,7 +129,7 @@ connection_table_get_connection(Connection_Table *ct, int sd)
b663b9
             break;
b663b9
         }
b663b9
 
b663b9
-        if (connection_is_free(&(ct->c[index]))) {
b663b9
+        if (connection_is_free(&(ct->c[index]), 1 /*use lock */)) {
b663b9
             break;
b663b9
         }
b663b9
     }
b663b9
diff --git a/ldap/servers/slapd/daemon.c b/ldap/servers/slapd/daemon.c
b663b9
index 50e67474e..35cfe7de0 100644
b663b9
--- a/ldap/servers/slapd/daemon.c
b663b9
+++ b/ldap/servers/slapd/daemon.c
b663b9
@@ -1699,7 +1699,8 @@ ns_connection_post_io_or_closing_try(Connection *conn)
b663b9
     }
b663b9
 
b663b9
     /*
b663b9
-     * Cancel any existing ns jobs we have registered.
b663b9
+     * A job was already scheduled.
b663b9
+     * Let it be dispatched first
b663b9
      */
b663b9
     if (conn->c_job != NULL) {
b663b9
         return 1;
b663b9
@@ -1780,25 +1781,59 @@ ns_connection_post_io_or_closing_try(Connection *conn)
b663b9
     }
b663b9
     return 0;
b663b9
 }
b663b9
+
b663b9
+/*
b663b9
+ * Tries to schedule I/O for this connection
b663b9
+ * If the connection is already busy with a scheduled I/O
b663b9
+ * it can wait until scheduled I/O is dispatched
b663b9
+ *
b663b9
+ * caller must hold c_mutex
b663b9
+ */
b663b9
 void
b663b9
 ns_connection_post_io_or_closing(Connection *conn)
b663b9
 {
b663b9
     while (ns_connection_post_io_or_closing_try(conn)) {
b663b9
-	/* we should retry later */
b663b9
-	
b663b9
-	/* We are not suppose to work immediately on the connection that is taken by
b663b9
-	 * another job
b663b9
-	 * release the lock and give some time
b663b9
-	 */
b663b9
-	
b663b9
-	if (CONN_NEEDS_CLOSING(conn) && conn->c_ns_close_jobs) {
b663b9
-	    return;
b663b9
-	} else {
b663b9
-	    PR_ExitMonitor(conn->c_mutex);
b663b9
-	    DS_Sleep(PR_MillisecondsToInterval(100));
b663b9
-
b663b9
-	    PR_EnterMonitor(conn->c_mutex);
b663b9
-	}
b663b9
+        /* Here a job is currently scheduled (c->job is set) and not yet dispatched
b663b9
+         * Job can be either:
b663b9
+         *  - ns_handle_closure
b663b9
+         *  - ns_handle_pr_read_ready
b663b9
+         */
b663b9
+
b663b9
+        if (connection_is_free(conn, 0)) {
b663b9
+            PRStatus shutdown_status;
b663b9
+
b663b9
+            /* The connection being freed,
b663b9
+             * It means that ns_handle_closure already completed and the connection
b663b9
+             * is no longer on the active list.
b663b9
+             * The scheduled job is useless and scheduling a new one as well
b663b9
+             */
b663b9
+            shutdown_status = ns_job_done(conn->c_job);
b663b9
+            if (shutdown_status != PR_SUCCESS) {
b663b9
+                slapi_log_err(SLAPI_LOG_CRIT, "ns_connection_post_io_or_closing", "Failed cancel a job on a freed connection %d !\n", conn->c_sd);
b663b9
+            }
b663b9
+            conn->c_job = NULL;
b663b9
+            return;
b663b9
+        }
b663b9
+        if (g_get_shutdown() && CONN_NEEDS_CLOSING(conn)) {
b663b9
+            PRStatus shutdown_status;
b663b9
+
b663b9
+            /* This is shutting down cancel any scheduled job to register ns_handle_closure
b663b9
+             */
b663b9
+            shutdown_status = ns_job_done(conn->c_job);
b663b9
+            if (shutdown_status != PR_SUCCESS) {
b663b9
+                slapi_log_err(SLAPI_LOG_CRIT, "ns_connection_post_io_or_closing", "Failed to cancel a job during shutdown %d !\n", conn->c_sd);
b663b9
+            }
b663b9
+            conn->c_job = NULL;
b663b9
+            continue;
b663b9
+        }
b663b9
+
b663b9
+        /* We are not suppose to work immediately on the connection that is taken by
b663b9
+         * another job
b663b9
+         * release the lock and give some time
b663b9
+         */
b663b9
+        PR_ExitMonitor(conn->c_mutex);
b663b9
+        DS_Sleep(PR_MillisecondsToInterval(100));
b663b9
+        PR_EnterMonitor(conn->c_mutex);
b663b9
     }
b663b9
 }
b663b9
 
b663b9
diff --git a/ldap/servers/slapd/proto-slap.h b/ldap/servers/slapd/proto-slap.h
b663b9
index b13334ad1..f54bc6bc5 100644
b663b9
--- a/ldap/servers/slapd/proto-slap.h
b663b9
+++ b/ldap/servers/slapd/proto-slap.h
b663b9
@@ -1431,7 +1431,7 @@ int connection_acquire_nolock(Connection *conn);
b663b9
 int connection_acquire_nolock_ext(Connection *conn, int allow_when_closing);
b663b9
 int connection_release_nolock(Connection *conn);
b663b9
 int connection_release_nolock_ext(Connection *conn, int release_only);
b663b9
-int connection_is_free(Connection *conn);
b663b9
+int connection_is_free(Connection *conn, int user_lock);
b663b9
 int connection_is_active_nolock(Connection *conn);
b663b9
 #if defined(USE_OPENLDAP)
b663b9
 ber_slen_t openldap_read_function(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len);
b663b9
-- 
b663b9
2.17.0
b663b9