Skip to content

Commit

Permalink
PS-3951: Fix decreasing status counters
Browse files Browse the repository at this point in the history
Status counters were cleaned in THD::release_resources, before an optional thread cleanup.
The cleanup could possibly perform a rollback, or other counter-increasing operations.
In this case, these additions to the thread local counters were lost during the destruction of the thread, but were visible for a short time, before the thread was removed from the global thread list.

This change avoids this issue by moving the global counter update to the end of the release_resources method, ensuring that possible counter-changing operation happen before it.
  • Loading branch information
dutow committed May 23, 2018
1 parent 69c0ed2 commit 36a7d96
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
6 changes: 6 additions & 0 deletions mysql-test/r/bug90351.result
@@ -0,0 +1,6 @@
CREATE TABLE t1 (i INTEGER);
INSERT INTO t1 VALUES (1);
SET AUTOCOMMIT=0;
START TRANSACTION;
DELETE FROM t1;
DROP TABLE t1;
26 changes: 26 additions & 0 deletions mysql-test/t/bug90351.test
@@ -0,0 +1,26 @@
#
# Bug 90351: status counters decrease on thread destruction
#
--source include/have_innodb.inc
--source include/count_sessions.inc

connect (con1,localhost,root,,);

connection con1;
CREATE TABLE t1 (i INTEGER);
INSERT INTO t1 VALUES (1);
let $con1_id= `SELECT connection_id()`;
SET AUTOCOMMIT=0;
START TRANSACTION;
DELETE FROM t1;

connection default;
--disable_query_log
eval KILL $con1_id;
--enable_query_log

connection default;
DROP TABLE t1;

disconnect con1;
--source include/wait_until_count_sessions.inc
17 changes: 12 additions & 5 deletions sql/sql_class.cc
Expand Up @@ -1773,11 +1773,6 @@ void THD::release_resources()
mysql_mutex_assert_not_owner(&LOCK_thread_count);
DBUG_ASSERT(m_release_resources_done == false);

mysql_mutex_lock(&LOCK_status);
add_to_status(&global_status_var, &status_var);
memset(&status_var, 0, sizeof(status_var));
mysql_mutex_unlock(&LOCK_status);

/* Ensure that no one is using THD */
mysql_mutex_lock(&LOCK_thd_data);

Expand All @@ -1802,6 +1797,10 @@ void THD::release_resources()
if (m_enable_plugins)
plugin_thdvar_cleanup(this);

mysql_mutex_lock(&LOCK_status);
add_to_status(&global_status_var, &status_var);
mysql_mutex_unlock(&LOCK_status);
memset(&status_var, 0, sizeof(status_var));
m_release_resources_done= true;
}

Expand All @@ -1816,6 +1815,14 @@ THD::~THD()
if (!m_release_resources_done)
release_resources();

#ifndef DBUG_OFF
{
const char* empty_status[sizeof(status_var)] = {};
DBUG_ASSERT(memcmp(reinterpret_cast<void*>(&status_var),
empty_status, sizeof(status_var))==0);
}
#endif // DBUG_OFF

clear_next_event_pos();

/* Ensure that no one is using THD */
Expand Down

0 comments on commit 36a7d96

Please sign in to comment.