Skip to content

Commit

Permalink
MDEV-18225 Avoid use of LOCK_prepared_stmt_count mutex in Statement_m…
Browse files Browse the repository at this point in the history
…ap destructo

This mutex can be freed when server shuts down (when thread_count goes down to 0)
, but it is still used inside THD::~THD() when Statement_map is destroyed.

The fix is to call Statement_map::reset() at the point where thread_count
is still positive, and avoid locking LOCK_prepared_stmt_count in THD
destructor.
  • Loading branch information
vaintroub committed Jan 15, 2019
1 parent 7372fe4 commit 7d3161d
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions sql/sql_class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,7 @@ void THD::cleanup(void)
sp_cache_clear(&sp_func_cache);

mysql_ull_cleanup(this);
stmt_map.reset();
/* All metadata locks must have been released by now. */
DBUG_ASSERT(!mdl_context.has_locks());

Expand Down Expand Up @@ -3777,11 +3778,13 @@ void Statement_map::erase(Statement *statement)
void Statement_map::reset()
{
/* Must be first, hash_free will reset st_hash.records */
mysql_mutex_lock(&LOCK_prepared_stmt_count);
DBUG_ASSERT(prepared_stmt_count >= st_hash.records);
prepared_stmt_count-= st_hash.records;
mysql_mutex_unlock(&LOCK_prepared_stmt_count);

if (st_hash.records)
{
mysql_mutex_lock(&LOCK_prepared_stmt_count);
DBUG_ASSERT(prepared_stmt_count >= st_hash.records);
prepared_stmt_count-= st_hash.records;
mysql_mutex_unlock(&LOCK_prepared_stmt_count);
}
my_hash_reset(&names_hash);
my_hash_reset(&st_hash);
last_found_statement= 0;
Expand All @@ -3790,12 +3793,8 @@ void Statement_map::reset()

Statement_map::~Statement_map()
{
/* Must go first, hash_free will reset st_hash.records */
mysql_mutex_lock(&LOCK_prepared_stmt_count);
DBUG_ASSERT(prepared_stmt_count >= st_hash.records);
prepared_stmt_count-= st_hash.records;
mysql_mutex_unlock(&LOCK_prepared_stmt_count);

/* Statement_map::reset() should be called prior to destructor. */
DBUG_ASSERT(!st_hash.records);
my_hash_free(&names_hash);
my_hash_free(&st_hash);
}
Expand Down

0 comments on commit 7d3161d

Please sign in to comment.