From 21395bbb6cc5747cd32bed937fbcc2645dc4826b Mon Sep 17 00:00:00 2001 From: Vladislav Vaintroub Date: Wed, 24 Sep 2025 21:31:34 +0200 Subject: [PATCH] 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. --- tpool/tpool_generic.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tpool/tpool_generic.cc b/tpool/tpool_generic.cc index b17f1d5480a..c9b69147792 100644 --- a/tpool/tpool_generic.cc +++ b/tpool/tpool_generic.cc @@ -393,7 +393,9 @@ public: */ void set_period(int period_ms) { - std::unique_lock lk(m_mtx); + std::unique_lock lk(m_mtx, std::defer_lock); + if (!lk.try_lock()) + return; if (!m_on) return; if (!m_pool)