Skip to content

Commit 21395bb

Browse files
committed
MDEV-37728: Fix shutdown deadlock between thr_timer_end and set_period
The mysys timer (thr_timer) could deadlock with srv_thread_pool_end() due to lock inversion: - LOCK_timer is held during the entire timer action routine. - timer action executed in mysys thread may lock timer_generic::m_mtx. - main thread holding the same timer_generic::m_mtx calls thr_timer_end(), which also needs LOCK_timer. This inversion can cause a very rare shutdown deadlock, when the threadpool extends the maintenance() period after idling for a minute and shutdown occurs simultaneously. Workaround: Use try_lock in timer_generic::set_period() to avoid blocking, to break the deadlock cycle, and ignore errors. Root cause is the thr_timer holds the global LOCK_timer for the entire duration of every timer action. This makes it difficult or impossible to use additional locks safely inside timer routines. Fortunately, in this case a failed try_lock has no impact.
1 parent 990b444 commit 21395bb

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

tpool/tpool_generic.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ class thread_pool_generic : public thread_pool
393393
*/
394394
void set_period(int period_ms)
395395
{
396-
std::unique_lock<std::mutex> lk(m_mtx);
396+
std::unique_lock<std::mutex> lk(m_mtx, std::defer_lock);
397+
if (!lk.try_lock())
398+
return;
397399
if (!m_on)
398400
return;
399401
if (!m_pool)

0 commit comments

Comments
 (0)