Blame SOURCES/0044-Ticket-48184-close-connections-at-shutdown-cleanly.patch

058656
From 0c1fbfaf77d6f7b2a6628deaf309bbe1c3e7a8e8 Mon Sep 17 00:00:00 2001
058656
From: William Brown <firstyear@redhat.com>
058656
Date: Tue, 28 Nov 2017 13:39:19 +0100
058656
Subject: [PATCH] Ticket 48184 - close connections at shutdown cleanly.
058656
058656
Bug Description:  During shutdown we would not close connections.
058656
In the past this may have just been an annoyance, but now with the way
058656
nunc-stans works, io events can still trigger on open xeisting connectinos
058656
during shutdown.
058656
058656
Fix Description:  Close connections during shutdown rather than
058656
leaving them alive.
058656
058656
https://pagure.io/389-ds-base/issue/48184
058656
058656
Author: wibrown
058656
058656
Review by: lkrispen, vashirov (Thank you!)
058656
---
058656
 ldap/servers/slapd/conntable.c | 13 +++++++
058656
 ldap/servers/slapd/daemon.c    | 77 ++++++++++++++++++++++++++----------------
058656
 ldap/servers/slapd/fe.h        |  1 +
058656
 ldap/servers/slapd/slap.h      |  1 +
058656
 4 files changed, 63 insertions(+), 29 deletions(-)
058656
058656
diff --git a/ldap/servers/slapd/conntable.c b/ldap/servers/slapd/conntable.c
058656
index 7c57b47cd..f2f763dfa 100644
058656
--- a/ldap/servers/slapd/conntable.c
058656
+++ b/ldap/servers/slapd/conntable.c
058656
@@ -91,6 +91,19 @@ connection_table_abandon_all_operations(Connection_Table *ct)
058656
     }
058656
 }
058656
 
058656
+void
058656
+connection_table_disconnect_all(Connection_Table *ct)
058656
+{
058656
+    for (size_t i = 0; i < ct->size; i++) {
058656
+        if (ct->c[i].c_mutex) {
058656
+            Connection *c = &(ct->c[i]);
058656
+            PR_EnterMonitor(c->c_mutex);
058656
+            disconnect_server_nomutex(c, c->c_connid, -1, SLAPD_DISCONNECT_ABORT, ECANCELED);
058656
+            PR_ExitMonitor(c->c_mutex);
058656
+        }
058656
+    }
058656
+}
058656
+
058656
 /* Given a file descriptor for a socket, this function will return
058656
  * a slot in the connection table to use.
058656
  *
058656
diff --git a/ldap/servers/slapd/daemon.c b/ldap/servers/slapd/daemon.c
058656
index 4e0466ab3..c245a4d4e 100644
058656
--- a/ldap/servers/slapd/daemon.c
058656
+++ b/ldap/servers/slapd/daemon.c
058656
@@ -1176,6 +1176,30 @@ slapd_daemon(daemon_ports_t *ports, ns_thrpool_t *tp)
058656
     housekeeping_stop(); /* Run this after op_thread_cleanup() logged sth */
058656
     disk_monitoring_stop();
058656
 
058656
+    /*
058656
+     * Now that they are abandonded, we need to mark them as done.
058656
+     * In NS while it's safe to allow excess jobs to be cleaned by
058656
+     * by the walk and ns_job_done of remaining queued events, the
058656
+     * issue is that if we allow something to live past this point
058656
+     * the CT is freed from underneath, and bad things happen (tm).
058656
+     *
058656
+     * NOTE: We do this after we stop psearch, because there could
058656
+     * be a race between flagging the psearch done, and users still
058656
+     * try to send on the connection. Similar with op_threads.
058656
+     */
058656
+    connection_table_disconnect_all(the_connection_table);
058656
+
058656
+    /*
058656
+     * WARNING: Normally we should close the tp in main
058656
+     * but because of issues in the current connection design
058656
+     * we need to close it here to guarantee events won't fire!
058656
+     *
058656
+     * All the connection close jobs "should" complete before
058656
+     * shutdown at least.
058656
+     */
058656
+    ns_thrpool_shutdown(tp);
058656
+    ns_thrpool_wait(tp);
058656
+
058656
     threads = g_get_active_threadcnt();
058656
     if (threads > 0) {
058656
         slapi_log_err(SLAPI_LOG_INFO, "slapd_daemon",
058656
@@ -1628,23 +1652,18 @@ ns_handle_closure(struct ns_job_t *job)
058656
     Connection *c = (Connection *)ns_job_get_data(job);
058656
     int do_yield = 0;
058656
 
058656
-/* this function must be called from the event loop thread */
058656
-#ifdef DEBUG
058656
-    PR_ASSERT(0 == NS_JOB_IS_THREAD(ns_job_get_type(job)));
058656
-#else
058656
-    /* This doesn't actually confirm it's in the event loop thread, but it's a start */
058656
-    if (NS_JOB_IS_THREAD(ns_job_get_type(job)) != 0) {
058656
-        slapi_log_err(SLAPI_LOG_ERR, "ns_handle_closure", "Attempt to close outside of event loop thread %" PRIu64 " for fd=%d\n",
058656
-                      c->c_connid, c->c_sd);
058656
-        return;
058656
-    }
058656
-#endif
058656
     PR_EnterMonitor(c->c_mutex);
058656
+    /* Assert we really have the right job state. */
058656
+    PR_ASSERT(job == c->c_job);
058656
+
058656
     connection_release_nolock_ext(c, 1); /* release ref acquired for event framework */
058656
     PR_ASSERT(c->c_ns_close_jobs == 1);  /* should be exactly 1 active close job - this one */
058656
     c->c_ns_close_jobs--;                /* this job is processing closure */
058656
+    /* Because handle closure will add a new job, we need to detach our current one. */
058656
+    c->c_job = NULL;
058656
     do_yield = ns_handle_closure_nomutex(c);
058656
     PR_ExitMonitor(c->c_mutex);
058656
+    /* Remove this task now. */
058656
     ns_job_done(job);
058656
     if (do_yield) {
058656
         /* closure not done - another reference still outstanding */
058656
@@ -1667,6 +1686,14 @@ ns_connection_post_io_or_closing(Connection *conn)
058656
         return;
058656
     }
058656
 
058656
+    /*
058656
+     * Cancel any existing ns jobs we have registered.
058656
+     */
058656
+    if (conn->c_job != NULL) {
058656
+        ns_job_done(conn->c_job);
058656
+        conn->c_job = NULL;
058656
+    }
058656
+
058656
     if (CONN_NEEDS_CLOSING(conn)) {
058656
         /* there should only ever be 0 or 1 active closure jobs */
058656
         PR_ASSERT((conn->c_ns_close_jobs == 0) || (conn->c_ns_close_jobs == 1));
058656
@@ -1676,13 +1703,10 @@ ns_connection_post_io_or_closing(Connection *conn)
058656
                           conn->c_connid, conn->c_sd);
058656
             return;
058656
         } else {
058656
-            /* just make sure we schedule the event to be closed in a timely manner */
058656
-            tv.tv_sec = 0;
058656
-            tv.tv_usec = slapd_wakeup_timer * 1000;
058656
             conn->c_ns_close_jobs++;                                                      /* now 1 active closure job */
058656
             connection_acquire_nolock_ext(conn, 1 /* allow acquire even when closing */); /* event framework now has a reference */
058656
-            ns_result_t job_result = ns_add_timeout_job(conn->c_tp, &tv, NS_JOB_TIMER,
058656
-                                                        ns_handle_closure, conn, NULL);
058656
+            /* Close the job asynchronously. Why? */
058656
+            ns_result_t job_result = ns_add_job(conn->c_tp, NS_JOB_TIMER, ns_handle_closure, conn, &(conn->c_job));
058656
             if (job_result != NS_SUCCESS) {
058656
                 if (job_result == NS_SHUTDOWN) {
058656
                     slapi_log_err(SLAPI_LOG_INFO, "ns_connection_post_io_or_closing", "post closure job "
058656
@@ -1726,7 +1750,7 @@ ns_connection_post_io_or_closing(Connection *conn)
058656
 #endif
058656
         ns_result_t job_result = ns_add_io_timeout_job(conn->c_tp, conn->c_prfd, &tv,
058656
                                                        NS_JOB_READ | NS_JOB_PRESERVE_FD,
058656
-                                                       ns_handle_pr_read_ready, conn, NULL);
058656
+                                                       ns_handle_pr_read_ready, conn, &(conn->c_job));
058656
         if (job_result != NS_SUCCESS) {
058656
             if (job_result == NS_SHUTDOWN) {
058656
                 slapi_log_err(SLAPI_LOG_INFO, "ns_connection_post_io_or_closing", "post I/O job for "
058656
@@ -1755,19 +1779,13 @@ ns_handle_pr_read_ready(struct ns_job_t *job)
058656
     int maxthreads = config_get_maxthreadsperconn();
058656
     Connection *c = (Connection *)ns_job_get_data(job);
058656
 
058656
-/* this function must be called from the event loop thread */
058656
-#ifdef DEBUG
058656
-    PR_ASSERT(0 == NS_JOB_IS_THREAD(ns_job_get_type(job)));
058656
-#else
058656
-    /* This doesn't actually confirm it's in the event loop thread, but it's a start */
058656
-    if (NS_JOB_IS_THREAD(ns_job_get_type(job)) != 0) {
058656
-        slapi_log_err(SLAPI_LOG_ERR, "ns_handle_pr_read_ready", "Attempt to handle read ready outside of event loop thread %" PRIu64 " for fd=%d\n",
058656
-                      c->c_connid, c->c_sd);
058656
-        return;
058656
-    }
058656
-#endif
058656
-
058656
     PR_EnterMonitor(c->c_mutex);
058656
+    /* Assert we really have the right job state. */
058656
+    PR_ASSERT(job == c->c_job);
058656
+
058656
+    /* On all code paths we remove the job, so set it null now */
058656
+    c->c_job = NULL;
058656
+
058656
     slapi_log_err(SLAPI_LOG_CONNS, "ns_handle_pr_read_ready", "activity on conn %" PRIu64 " for fd=%d\n",
058656
                   c->c_connid, c->c_sd);
058656
     /* if we were called due to some i/o event, see what the state of the socket is */
058656
@@ -1826,6 +1844,7 @@ ns_handle_pr_read_ready(struct ns_job_t *job)
058656
         slapi_log_err(SLAPI_LOG_CONNS, "ns_handle_pr_read_ready", "queued conn %" PRIu64 " for fd=%d\n",
058656
                       c->c_connid, c->c_sd);
058656
     }
058656
+    /* Since we call done on the job, we need to remove it here. */
058656
     PR_ExitMonitor(c->c_mutex);
058656
     ns_job_done(job);
058656
     return;
058656
diff --git a/ldap/servers/slapd/fe.h b/ldap/servers/slapd/fe.h
058656
index 4d25a9fb8..f47bb6145 100644
058656
--- a/ldap/servers/slapd/fe.h
058656
+++ b/ldap/servers/slapd/fe.h
058656
@@ -100,6 +100,7 @@ extern Connection_Table *the_connection_table; /* JCM - Exported from globals.c
058656
 Connection_Table *connection_table_new(int table_size);
058656
 void connection_table_free(Connection_Table *ct);
058656
 void connection_table_abandon_all_operations(Connection_Table *ct);
058656
+void connection_table_disconnect_all(Connection_Table *ct);
058656
 Connection *connection_table_get_connection(Connection_Table *ct, int sd);
058656
 int connection_table_move_connection_out_of_active_list(Connection_Table *ct, Connection *c);
058656
 void connection_table_move_connection_on_to_active_list(Connection_Table *ct, Connection *c);
058656
diff --git a/ldap/servers/slapd/slap.h b/ldap/servers/slapd/slap.h
058656
index 830944f72..08754d8fb 100644
058656
--- a/ldap/servers/slapd/slap.h
058656
+++ b/ldap/servers/slapd/slap.h
058656
@@ -1644,6 +1644,7 @@ typedef struct conn
058656
     void *c_io_layer_cb_data;            /* callback data */
058656
     struct connection_table *c_ct;       /* connection table that this connection belongs to */
058656
     ns_thrpool_t *c_tp;                  /* thread pool for this connection */
058656
+    struct ns_job_t *c_job;                     /* If it exists, the current ns_job_t */
058656
     int c_ns_close_jobs;                 /* number of current close jobs */
058656
     char *c_ipaddr;                      /* ip address str - used by monitor */
058656
 } Connection;
058656
-- 
058656
2.13.6
058656