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