From 2447172afb5afed1687779e46eed217c38f89ebc Mon Sep 17 00:00:00 2001 From: Monty Date: Mon, 6 Nov 2023 17:37:11 +0200 Subject: [PATCH] Ensure that process "State" is properly cleaned after query execution In some cases "SHOW PROCESSLIST" could show "Reset for next command" as State, even if the previous query had finished properly. Fixed by clearing State after end of command and also setting the State for the "Connect" command. Other things: - Changed usage of 'thd->set_command(COM_SLEEP)' to 'thd->mark_connection_idle()'. - Changed thread_state_info() to return "" instead of NULL. This is just a safety measurement and in line with the logic of the rest of the function. --- libmysqld/lib_sql.cc | 3 +-- plugin/feedback/sender_thread.cc | 2 +- sql/sql_class.cc | 1 + sql/sql_class.h | 13 ++++++++++++- sql/sql_connect.cc | 3 +-- sql/sql_parse.cc | 3 ++- sql/sql_show.cc | 2 +- sql/wsrep_client_service.cc | 2 +- sql/wsrep_mysqld.cc | 3 +-- sql/wsrep_server_service.cc | 2 +- 10 files changed, 22 insertions(+), 12 deletions(-) diff --git a/libmysqld/lib_sql.cc b/libmysqld/lib_sql.cc index 01a70e724cf..9a011b2180f 100644 --- a/libmysqld/lib_sql.cc +++ b/libmysqld/lib_sql.cc @@ -708,8 +708,7 @@ void *create_embedded_thd(int client_flag) if (thd->variables.max_join_size == HA_POS_ERROR) thd->variables.option_bits |= OPTION_BIG_SELECTS; - thd->proc_info=0; // Remove 'login' - thd->set_command(COM_SLEEP); + thd->mark_connection_idle(); thd->set_time(); thd->init_for_queries(); thd->client_capabilities= client_flag; diff --git a/plugin/feedback/sender_thread.cc b/plugin/feedback/sender_thread.cc index 3976c950541..dcd9d783dfb 100644 --- a/plugin/feedback/sender_thread.cc +++ b/plugin/feedback/sender_thread.cc @@ -97,8 +97,8 @@ static int prepare_for_fill(TABLE_LIST *tables) thd->mysys_var->current_cond= &sleep_condition; thd->mysys_var->current_mutex= &sleep_mutex; + thd->mark_connection_idle(); thd->proc_info="feedback"; - thd->set_command(COM_SLEEP); thd->system_thread= SYSTEM_THREAD_EVENT_WORKER; // whatever thd->set_time(); thd->init_for_queries(); diff --git a/sql/sql_class.cc b/sql/sql_class.cc index 55db1ff4f52..0f83b3d4c08 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -1630,6 +1630,7 @@ void THD::reset_for_reuse() abort_on_warning= 0; free_connection_done= 0; m_command= COM_CONNECT; + proc_info= "login"; // Same as in THD::THD() transaction.on= 1; #if defined(ENABLED_PROFILING) profiling.reset(); diff --git a/sql/sql_class.h b/sql/sql_class.h index e0f87383189..a903b70fa84 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -4630,13 +4630,24 @@ public: public: /** Overloaded to guard query/query_length fields */ virtual void set_statement(Statement *stmt); - void set_command(enum enum_server_command command) + inline void set_command(enum enum_server_command command) { + DBUG_ASSERT(command != COM_SLEEP); m_command= command; #ifdef HAVE_PSI_THREAD_INTERFACE PSI_STATEMENT_CALL(set_thread_command)(m_command); #endif } + /* As sleep needs a bit of special handling, we have a special case for it */ + inline void mark_connection_idle() + { + proc_info= 0; + m_command= COM_SLEEP; +#ifdef HAVE_PSI_THREAD_INTERFACE + PSI_STATEMENT_CALL(set_thread_command)(m_command); +#endif + } + inline enum enum_server_command get_command() const { return m_command; } diff --git a/sql/sql_connect.cc b/sql/sql_connect.cc index 263c9eab832..eae30ce76aa 100644 --- a/sql/sql_connect.cc +++ b/sql/sql_connect.cc @@ -1249,8 +1249,7 @@ void prepare_new_connection_state(THD* thd) embedded server library. TODO: refactor this to avoid code duplication there */ - thd->proc_info= 0; - thd->set_command(COM_SLEEP); + thd->mark_connection_idle(); thd->init_for_queries(); if (opt_init_connect.length && !(sctx->master_access & SUPER_ACL)) diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index ffba37b84a7..1c487ba9d14 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -2465,7 +2465,7 @@ dispatch_end: /* Performance Schema Interface instrumentation, end */ MYSQL_END_STATEMENT(thd->m_statement_psi, thd->get_stmt_da()); thd->set_examined_row_count(0); // For processlist - thd->set_command(COM_SLEEP); + thd->mark_connection_idle(); thd->m_statement_psi= NULL; thd->m_digest= NULL; @@ -7903,6 +7903,7 @@ static bool wsrep_mysql_parse(THD *thd, char *rawbuf, uint length, thd->wsrep_retry_query = NULL; thd->wsrep_retry_query_len = 0; thd->wsrep_retry_command = COM_CONNECT; + thd->proc_info= 0; } return false; } diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 7d549c653f2..2b3e8d41757 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -2818,7 +2818,7 @@ static const char *thread_state_info(THD *tmp) if (cond) return "Waiting on cond"; } - return NULL; + return ""; } diff --git a/sql/wsrep_client_service.cc b/sql/wsrep_client_service.cc index 0399cf4f442..0d0443ad5f2 100644 --- a/sql/wsrep_client_service.cc +++ b/sql/wsrep_client_service.cc @@ -287,7 +287,7 @@ enum wsrep::provider::status Wsrep_client_service::replay() replayer_thd->real_id= pthread_self(); replayer_thd->prior_thr_create_utime= replayer_thd->start_utime= microsecond_interval_timer(); - replayer_thd->set_command(COM_SLEEP); + replayer_thd->mark_connection_idle(); replayer_thd->reset_for_next_command(true); enum wsrep::provider::status ret; diff --git a/sql/wsrep_mysqld.cc b/sql/wsrep_mysqld.cc index 06991c7f44d..6008bc0b72c 100644 --- a/sql/wsrep_mysqld.cc +++ b/sql/wsrep_mysqld.cc @@ -3015,8 +3015,7 @@ void* start_wsrep_THD(void *arg) thd->security_ctx->skip_grants(); /* handle_one_connection() again... */ - thd->proc_info= 0; - thd->set_command(COM_SLEEP); + thd->mark_connection_idle(); thd->init_for_queries(); mysql_mutex_lock(&LOCK_wsrep_slave_threads); diff --git a/sql/wsrep_server_service.cc b/sql/wsrep_server_service.cc index 7bf9851c25b..70723aefb38 100644 --- a/sql/wsrep_server_service.cc +++ b/sql/wsrep_server_service.cc @@ -39,7 +39,7 @@ static void init_service_thd(THD* thd, char* thread_stack) thd->thread_stack= thread_stack; thd->real_id= pthread_self(); thd->prior_thr_create_utime= thd->start_utime= microsecond_interval_timer(); - thd->set_command(COM_SLEEP); + thd->mark_connection_idle(); thd->reset_for_next_command(true); }