mirror of
https://github.com/MariaDB/server.git
synced 2025-07-29 05:21:33 +03:00
Bug#17283409 4-WAY DEADLOCK: ZOMBIES, PURGING BINLOGS,
SHOW PROCESSLIST, SHOW BINLOGS Problem: A deadlock was occurring when 4 threads were involved in acquiring locks in the following way Thread 1: Dump thread ( Slave is reconnecting, so on Master, a new dump thread is trying kill zombie dump threads. It acquired thread's LOCK_thd_data and it is about to acquire mysys_var->current_mutex ( which LOCK_log) Thread 2: Application thread is executing show binlogs and acquired LOCK_log and it is about to acquire LOCK_index. Thread 3: Application thread is executing Purge binary logs and acquired LOCK_index and it is about to acquire LOCK_thread_count. Thread 4: Application thread is executing show processlist and acquired LOCK_thread_count and it is about to acquire zombie dump thread's LOCK_thd_data. Deadlock Cycle: Thread 1 -> Thread 2 -> Thread 3-> Thread 4 ->Thread 1 The same above deadlock was observed even when thread 4 is executing 'SELECT * FROM information_schema.processlist' command and acquired LOCK_thread_count and it is about to acquire zombie dump thread's LOCK_thd_data. Analysis: There are four locks involved in the deadlock. LOCK_log, LOCK_thread_count, LOCK_index and LOCK_thd_data. LOCK_log, LOCK_thread_count, LOCK_index are global mutexes where as LOCK_thd_data is local to a thread. We can divide these four locks in two groups. Group 1 consists of LOCK_log and LOCK_index and the order should be LOCK_log followed by LOCK_index. Group 2 consists of other two mutexes LOCK_thread_count, LOCK_thd_data and the order should be LOCK_thread_count followed by LOCK_thd_data. Unfortunately, there is no specific predefined lock order defined to follow in the MySQL system when it comes to locks across these two groups. In the above problematic example, there is no problem in the way we are acquiring the locks if you see each thread individually. But If you combine all 4 threads, they end up in a deadlock. Fix: Since everything seems to be fine in the way threads are taking locks, In this patch We are changing the duration of the locks in Thread 4 to break the deadlock. i.e., before the patch, Thread 4 ('show processlist' command) mysqld_list_processes() function acquires LOCK_thread_count for the complete duration of the function and it also acquires/releases each thread's LOCK_thd_data. LOCK_thread_count is used to protect addition and deletion of threads in global threads list. While show process list is looping through all the existing threads, it will be a problem if a thread is exited but there is no problem if a new thread is added to the system. Hence a new mutex is introduced "LOCK_thd_remove" which will protect deletion of a thread from global threads list. All threads which are getting exited should acquire LOCK_thd_remove followed by LOCK_thread_count. (It should take LOCK_thread_count also because other places of the code still thinks that exit thread is protected with LOCK_thread_count. In this fix, we are changing only 'show process list' query logic ) (Eg: unlink_thd logic will be protected with LOCK_thd_remove). Logic of mysqld_list_processes(or file_schema_processlist) will now be protected with 'LOCK_thd_remove' instead of 'LOCK_thread_count'. Now the new locking order after this patch is: LOCK_thd_remove -> LOCK_thd_data -> LOCK_log -> LOCK_index -> LOCK_thread_count
This commit is contained in:
@ -1814,13 +1814,23 @@ void mysqld_list_processes(THD *thd,const char *user, bool verbose)
|
||||
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
|
||||
DBUG_VOID_RETURN;
|
||||
|
||||
mysql_mutex_lock(&LOCK_thread_count); // For unlink from list
|
||||
|
||||
if (!thd->killed)
|
||||
{
|
||||
/*
|
||||
Acquire only LOCK_thd_remove and not LOCK_thread_count.
|
||||
i.e., we allow new threads to be added to the list while processing
|
||||
the list but we will not allow deletions from the list (Note that unlink
|
||||
a thread is protected by both LOCK_thd_remove and LOCK_thread_count
|
||||
mutexes).
|
||||
*/
|
||||
mysql_mutex_lock(&LOCK_thd_remove);
|
||||
I_List_iterator<THD> it(threads);
|
||||
THD *tmp;
|
||||
DEBUG_SYNC(thd,"before_one_element_read_from_threads_iterator");
|
||||
while ((tmp=it++))
|
||||
{
|
||||
DEBUG_SYNC(thd,"after_one_element_read_from_threads_iterator");
|
||||
Security_context *tmp_sctx= tmp->security_ctx;
|
||||
struct st_my_thread_var *mysys_var;
|
||||
if ((tmp->vio_ok() || tmp->system_thread) &&
|
||||
@ -1845,6 +1855,11 @@ void mysqld_list_processes(THD *thd,const char *user, bool verbose)
|
||||
tmp_sctx->get_host()->length() ?
|
||||
tmp_sctx->get_host()->ptr() : "");
|
||||
thd_info->command=(int) tmp->command;
|
||||
DBUG_EXECUTE_IF("processlist_acquiring_dump_threads_LOCK_thd_data",
|
||||
{
|
||||
if (tmp->command == COM_BINLOG_DUMP)
|
||||
DEBUG_SYNC(thd, "processlist_after_LOCK_thd_count_before_LOCK_thd_data");
|
||||
});
|
||||
mysql_mutex_lock(&tmp->LOCK_thd_data);
|
||||
if ((thd_info->db= tmp->db)) // Safe test
|
||||
thd_info->db= thd->strdup(thd_info->db);
|
||||
@ -1869,8 +1884,8 @@ void mysqld_list_processes(THD *thd,const char *user, bool verbose)
|
||||
thread_infos.append(thd_info);
|
||||
}
|
||||
}
|
||||
mysql_mutex_unlock(&LOCK_thd_remove);
|
||||
}
|
||||
mysql_mutex_unlock(&LOCK_thread_count);
|
||||
|
||||
thread_info *thd_info;
|
||||
time_t now= my_time(0);
|
||||
@ -1910,10 +1925,15 @@ int fill_schema_processlist(THD* thd, TABLE_LIST* tables, COND* cond)
|
||||
user= thd->security_ctx->master_access & PROCESS_ACL ?
|
||||
NullS : thd->security_ctx->priv_user;
|
||||
|
||||
mysql_mutex_lock(&LOCK_thread_count);
|
||||
|
||||
if (!thd->killed)
|
||||
{
|
||||
/*
|
||||
Acquire only LOCK_thd_remove and not LOCK_thread_count.
|
||||
i.e., we allow new threads to be added to the list while processing
|
||||
the list but we will not allow deletions from the list (Note that unlink
|
||||
thread is also protected with LOCK_thd_remove mutex).
|
||||
*/
|
||||
mysql_mutex_lock(&LOCK_thd_remove);
|
||||
I_List_iterator<THD> it(threads);
|
||||
THD* tmp;
|
||||
|
||||
@ -1946,7 +1966,13 @@ int fill_schema_processlist(THD* thd, TABLE_LIST* tables, COND* cond)
|
||||
else
|
||||
table->field[2]->store(tmp_sctx->host_or_ip,
|
||||
strlen(tmp_sctx->host_or_ip), cs);
|
||||
DBUG_EXECUTE_IF("processlist_acquiring_dump_threads_LOCK_thd_data",
|
||||
{
|
||||
if (tmp->command == COM_BINLOG_DUMP)
|
||||
DEBUG_SYNC(thd, "processlist_after_LOCK_thd_count_before_LOCK_thd_data");
|
||||
});
|
||||
/* DB */
|
||||
/* Lock THD mutex that protects its data when looking at it. */
|
||||
mysql_mutex_lock(&tmp->LOCK_thd_data);
|
||||
if ((db= tmp->db))
|
||||
{
|
||||
@ -1974,11 +2000,8 @@ int fill_schema_processlist(THD* thd, TABLE_LIST* tables, COND* cond)
|
||||
|
||||
if (mysys_var)
|
||||
mysql_mutex_unlock(&mysys_var->mutex);
|
||||
mysql_mutex_unlock(&tmp->LOCK_thd_data);
|
||||
|
||||
/* INFO */
|
||||
/* Lock THD mutex that protects its data when looking at it. */
|
||||
mysql_mutex_lock(&tmp->LOCK_thd_data);
|
||||
if (tmp->query())
|
||||
{
|
||||
table->field[7]->store(tmp->query(),
|
||||
@ -1990,13 +2013,12 @@ int fill_schema_processlist(THD* thd, TABLE_LIST* tables, COND* cond)
|
||||
|
||||
if (schema_table_store_record(thd, table))
|
||||
{
|
||||
mysql_mutex_unlock(&LOCK_thread_count);
|
||||
mysql_mutex_unlock(&LOCK_thd_remove);
|
||||
DBUG_RETURN(1);
|
||||
}
|
||||
}
|
||||
mysql_mutex_unlock(&LOCK_thd_remove);
|
||||
}
|
||||
|
||||
mysql_mutex_unlock(&LOCK_thread_count);
|
||||
DBUG_RETURN(0);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user