From 46b79b8cd1c8becbc9fffad503a584e0edaf91a3 Mon Sep 17 00:00:00 2001
From: Sergei Golubchik <serg@mariadb.org>
Date: Mon, 3 Jul 2023 22:05:02 +0200
Subject: [PATCH] MDEV-30084 Shutdown hangs in some tests

debug-only issue. the test was doing

  set debug_sync='now SIGNAL go3';
  ...
  set debug_sync='reset';

which translated into

  add "go3" to the hash of active signals
  pthread_broadcast to wake up waiting threads
  ...
  clear the hash of active signals

as a result a waiting thread was awoken, but the hash was emptied
before the thread checked if its signal was in the hash. so the
thread didn't find its signal and went back to sleep.

let's wait until the awoken thread has completely finished
disconnecting and was added to the thread cache.
---
 mysql-test/main/kill_debug.result | 1 +
 mysql-test/main/kill_debug.test   | 6 ++++++
 2 files changed, 7 insertions(+)

diff --git a/mysql-test/main/kill_debug.result b/mysql-test/main/kill_debug.result
index 061e7602383..0c910906b92 100644
--- a/mysql-test/main/kill_debug.result
+++ b/mysql-test/main/kill_debug.result
@@ -234,6 +234,7 @@ connection default;
 set debug_sync='now WAIT_FOR go0';
 set debug_sync='found_killee SIGNAL go1 WAIT_FOR go2';
 kill $id;
+select variable_value into @threads_cached from information_schema.global_status where variable_name='threads_cached';
 set debug_sync='now SIGNAL go3';
 drop table t1;
 set debug_sync='reset';
diff --git a/mysql-test/main/kill_debug.test b/mysql-test/main/kill_debug.test
index 32a764004e3..6bade1d8d90 100644
--- a/mysql-test/main/kill_debug.test
+++ b/mysql-test/main/kill_debug.test
@@ -313,6 +313,12 @@ connection default;
 set debug_sync='now WAIT_FOR go0';
 set debug_sync='found_killee SIGNAL go1 WAIT_FOR go2';
 evalp kill $id;
+select variable_value into @threads_cached from information_schema.global_status where variable_name='threads_cached';
 set debug_sync='now SIGNAL go3';
+if (`select @@thread_handling != 'pool-of-threads'`) {
+  # cannot check that a thread was added to thread pool on windows, but the test works there w/o the wait
+  let wait_condition= select variable_value>@threads_cached from information_schema.global_status where variable_name='threads_cached';
+  source include/wait_condition.inc;
+}
 drop table t1;
 set debug_sync='reset';
-- 
2.30.9