From 7425610c81ca1329d72a97f57770e5b052d08079 Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Mon, 9 May 2016 18:30:22 +0200 Subject: [PATCH] decrement thead_count *after* THD is destroyed because thread_count means just that: number of THDs and shutdown code looks at it to know when to free shared data structures that THD uses. This fixes random crashes in ~THD on shutdown --- sql/mysqld.cc | 3 ++- sql/scheduler.cc | 1 + sql/threadpool_common.cc | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/sql/mysqld.cc b/sql/mysqld.cc index 835074d6e5f..ad1c301a234 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -2972,7 +2972,6 @@ void unlink_thd(THD *thd) unlink_not_visible_thd(thd); thd->free_connection(); - dec_thread_count(); DBUG_VOID_RETURN; } @@ -3117,6 +3116,7 @@ bool one_thread_per_connection_end(THD *thd, bool put_in_cache) if (!wsrep_applier && put_in_cache && cache_thread(thd)) DBUG_RETURN(0); // Thread is reused delete thd; + dec_thread_count(); } DBUG_PRINT("info", ("killing thread")); @@ -6418,6 +6418,7 @@ void create_thread_to_handle_connection(CONNECT *connect) /* Get thread from cache */ thread_cache.push_back(connect); wake_thread++; + thread_safe_decrement32(&thread_count); mysql_cond_signal(&COND_thread_cache); mysql_mutex_unlock(&LOCK_thread_cache); DBUG_PRINT("info",("Thread created")); diff --git a/sql/scheduler.cc b/sql/scheduler.cc index de472ae2504..95702dbb4e1 100644 --- a/sql/scheduler.cc +++ b/sql/scheduler.cc @@ -39,6 +39,7 @@ static bool no_threads_end(THD *thd, bool put_in_cache) { unlink_thd(thd); delete thd; + dec_thread_count(); } return 1; // Abort handle_one_connection } diff --git a/sql/threadpool_common.cc b/sql/threadpool_common.cc index 520a16c467e..9de168c9fdf 100644 --- a/sql/threadpool_common.cc +++ b/sql/threadpool_common.cc @@ -201,7 +201,7 @@ void threadpool_remove_connection(THD *thd) close_connection(thd, 0); unlink_thd(thd); delete thd; - mysql_cond_broadcast(&COND_thread_count); + dec_thread_count(); /* Free resources associated with this connection: