From eabb0aef12e1a6310dea18a2c72efcef42b6dd86 Mon Sep 17 00:00:00 2001
From: Sergei Golubchik <serg@mariadb.org>
Date: Wed, 14 Dec 2016 17:47:24 +0100
Subject: [PATCH] sporadic crashes of innodb.innodb_prefix_index_restart_server

in slow shutdown mode purge threads really must exit only when there is
nothing to purge. Restore the trx_commit_disallowed check and
don't stop purge threads until all connection thread transactions
are gone.
---
 storage/innobase/handler/ha_innodb.cc | 6 ++++++
 storage/innobase/srv/srv0srv.cc       | 6 ++++++
 2 files changed, 12 insertions(+)

diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc
index cac438c71d8..42dce9d79b7 100644
--- a/storage/innobase/handler/ha_innodb.cc
+++ b/storage/innobase/handler/ha_innodb.cc
@@ -340,6 +340,12 @@ thd_destructor_proxy(void *)
 	mysql_mutex_unlock(&thd_destructor_mutex);
 	thd_destructor_myvar = NULL;
 
+	srv_fast_shutdown = (ulint) innobase_fast_shutdown;
+	if (srv_fast_shutdown == 0) {
+		while (trx_sys_any_active_transactions()) {
+			os_thread_sleep(1000);
+		}
+	}
 	srv_purge_wakeup();
 
 	destroy_thd(thd);
diff --git a/storage/innobase/srv/srv0srv.cc b/storage/innobase/srv/srv0srv.cc
index 2e9e3b7ea8a..efc33a9baa2 100644
--- a/storage/innobase/srv/srv0srv.cc
+++ b/storage/innobase/srv/srv0srv.cc
@@ -3073,6 +3073,12 @@ DECLARE_THREAD(srv_purge_coordinator_thread)(
 		n_pages_purged = trx_purge(1, srv_purge_batch_size, false);
 	}
 
+#ifdef UNIV_DEBUG
+	if (srv_fast_shutdown == 0) {
+		trx_commit_disallowed = true;
+	}
+#endif /* UNIV_DEBUG */
+
 	/* This trx_purge is called to remove any undo records (added by
 	background threads) after completion of the above loop. When
 	srv_fast_shutdown != 0, a large batch size can cause significant
-- 
2.30.9