From 55ddfe1c95cba66fb7b0b46007e97eba9b0d93eb Mon Sep 17 00:00:00 2001 From: Sergey Vojtovich Date: Mon, 28 Apr 2025 22:45:10 +0400 Subject: [PATCH] MDEV-36684 - main.mdl_sync fails under valgrind (test for Bug#42643) Valgrind is single threaded and only changes threads as part of system calls or waits. Some busy loops were identified and fixed where the server assumes that some other thread will change the state, which will not happen with valgrind. Based on patch by Monty. Original patch introduced VALGRIND_YIELD, which emits pthread_yield() only in valgrind builds. However it was agreed that it is a good idea to emit yield() unconditionally, such that other affected schedulers (like SCHED_FIFO) benefit from this change. Also avoid pthread_yield() in favour of standard std::this_thread::yield(). --- sql/sql_base.cc | 2 ++ sql/sql_class.h | 1 + sql/table_cache.cc | 1 + storage/innobase/trx/trx0purge.cc | 1 + 4 files changed, 5 insertions(+) diff --git a/sql/sql_base.cc b/sql/sql_base.cc index ea25ffd6b4d..713a0b455e9 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -4460,6 +4460,7 @@ restart: goto error; error= FALSE; + std::this_thread::yield(); goto restart; } goto error; @@ -4522,6 +4523,7 @@ restart: goto error; error= FALSE; + std::this_thread::yield(); goto restart; } /* diff --git a/sql/sql_class.h b/sql/sql_class.h index f544c2e3fd1..7da9418c5ea 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -20,6 +20,7 @@ /* Classes in mysql */ #include +#include #include "dur_prop.h" #include #include "sql_const.h" diff --git a/sql/table_cache.cc b/sql/table_cache.cc index b804a3e0627..48cd63401e6 100644 --- a/sql/table_cache.cc +++ b/sql/table_cache.cc @@ -901,6 +901,7 @@ retry: { mysql_mutex_unlock(&element->LOCK_table_share); lf_hash_search_unpin(thd->tdc_hash_pins); + std::this_thread::yield(); goto retry; } lf_hash_search_unpin(thd->tdc_hash_pins); diff --git a/storage/innobase/trx/trx0purge.cc b/storage/innobase/trx/trx0purge.cc index 8292bb063b4..9e8b256ffb6 100644 --- a/storage/innobase/trx/trx0purge.cc +++ b/storage/innobase/trx/trx0purge.cc @@ -1076,6 +1076,7 @@ static void trx_purge_close_tables(purge_node_t *node, THD *thd) noexcept void purge_sys_t::wait_FTS(bool also_sys) { + std::this_thread::yield(); for (const uint32_t mask= also_sys ? ~0U : ~PAUSED_SYS; m_FTS_paused & mask;) std::this_thread::sleep_for(std::chrono::milliseconds(10)); }