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

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