From 74d3109eb09cc32f1acf3a81c042ff2ec988345a Mon Sep 17 00:00:00 2001
From: Georgy Moshkin <gmoshkin@picodata.io>
Date: Thu, 16 Jun 2022 17:28:32 +0300
Subject: [PATCH] fix(tarantool-sys): yielding on_shutdown triggers on ctrl-d

---
 ...-yielding-on_shutdown-triggers-on-ct.patch | 59 +++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 tarantool-patches/0009-fix-triggers-fix-yielding-on_shutdown-triggers-on-ct.patch

diff --git a/tarantool-patches/0009-fix-triggers-fix-yielding-on_shutdown-triggers-on-ct.patch b/tarantool-patches/0009-fix-triggers-fix-yielding-on_shutdown-triggers-on-ct.patch
new file mode 100644
index 0000000000..3ab776d3f4
--- /dev/null
+++ b/tarantool-patches/0009-fix-triggers-fix-yielding-on_shutdown-triggers-on-ct.patch
@@ -0,0 +1,59 @@
+From 2242618aceaf955b5ded6d3cda857287e71a7a17 Mon Sep 17 00:00:00 2001
+From: Georgy Moshkin <gmoshkin@picodata.io>
+Date: Thu, 16 Jun 2022 17:13:58 +0300
+Subject: [PATCH] fix(triggers): fix yielding on_shutdown triggers on ctrl-d
+
+The problem was: when on_shutdown triggers are running at the end of the
+main function, no event loop is running. So if a trigger yields, then
+on_shutdown_fiber yields when trying to join it and returns execution
+into the main fiber, which won't ever let the triggers run again.
+
+A fix is to run the event loop in the main fiber while the on_shutdown
+triggers are running. This event loop will be interrupted once the
+triggers are finished and tarantool will be able to finish successfully.
+---
+ src/main.cc |   11 +++++++++++
+ 1 file changed, 11 insertions(+)
+
+diff --git a/src/main.cc b/src/main.cc
+index de082c17f..67ae056f1 100644
+--- a/src/main.cc
++++ b/src/main.cc
+@@ -100,6 +100,11 @@ static double start_time;
+ static struct fiber *on_shutdown_fiber = NULL;
+ /** A flag restricting repeated execution of tarantool_exit(). */
+ static bool is_shutting_down = false;
++/**
++ * A flag preventing main fiber from exiting until on_shutdown triggers have
++ * finished.
++ */
++static bool on_shutdown_is_running = false;
+ static int exit_code = 0;
+ 
+ double
+@@ -136,9 +141,11 @@ static int
+ on_shutdown_f(va_list ap)
+ {
+ 	(void) ap;
++	on_shutdown_is_running = true;
+ 	trigger_fiber_run(&box_on_shutdown_trigger_list, NULL,
+ 			  on_shutdown_trigger_timeout);
+ 	ev_break(loop(), EVBREAK_ALL);
++	on_shutdown_is_running = false;
+ 	return 0;
+ }
+ 
+@@ -791,6 +803,10 @@ main(int argc, char **argv)
+ 	 * any signals.
+ 	 */
+ 	tarantool_exit(exit_code);
++	if (on_shutdown_is_running) {
++		/* on_shutdown_fiber will call ev_break(ALL) after it ends */
++		ev_run(loop(), 0);
++	}
+ 	/* freeing resources */
+ 	tarantool_free();
+ 	return exit_code;
+-- 
+2.25.1
+
-- 
GitLab