[PATCH v5 2/3] lua: patch os.exit() to execute on_shutdown triggers.

Serge Petrenko sergepetrenko at tarantool.org
Fri Jan 25 18:41:33 MSK 2019


Make os.exit() call tarantool_exit(), just like the signal handler does.
Now on_shutdown triggers are not run only when a fatal signal is
received.

@TarantoolBot document
Title: Document box.ctl.on_shutdown triggers
on_shutdown triggers may be set similar to space:on_replace triggers:
```
box.ctl.on_shutdown(new_trigger, old_trigger)
```
The triggers will be run when tarantool exits due to receiving one of
the signals: `SIGTERM`, `SIGINT`, `SIGHUP` or when user executes
`os.exit()`.

Note that the triggers will not be run if tarantool receives a fatal
signal: `SIGSEGV`, `SIGABORT` or any signal causing immediate program
termination.

Closes #1607
---
 extra/exports    |  1 +
 src/lua/init.lua |  7 +++++++
 src/main.cc      | 10 ++++++----
 src/main.h       |  3 +++
 4 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/extra/exports b/extra/exports
index 5f69e0730..35b47ae00 100644
--- a/extra/exports
+++ b/extra/exports
@@ -69,6 +69,7 @@ say_set_log_level
 say_logrotate
 say_set_log_format
 tarantool_uptime
+tarantool_exit
 log_pid
 space_by_id
 space_run_triggers
diff --git a/src/lua/init.lua b/src/lua/init.lua
index fa324d34c..a961db328 100644
--- a/src/lua/init.lua
+++ b/src/lua/init.lua
@@ -38,6 +38,8 @@ double
 tarantool_uptime(void);
 typedef int32_t pid_t;
 pid_t getpid(void);
+void
+tarantool_exit(int);
 ]]
 
 local fio = require("fio")
@@ -50,6 +52,11 @@ dostring = function(s, ...)
     return chunk(...)
 end
 
+os.exit = function(code)
+    code = (type(code) == 'number') and code or 0
+    ffi.C.tarantool_exit(code)
+end
+
 local function uptime()
     return tonumber(ffi.C.tarantool_uptime());
 end
diff --git a/src/main.cc b/src/main.cc
index 96df9e851..a3d1a6f22 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -94,6 +94,7 @@ static struct fiber *on_shutdown_fiber = NULL;
 static bool is_shutting_down = false;
 /** A trigger which will break the event loop on shutdown. */
 static struct trigger shutdown_trig;
+static int exit_code = 0;
 
 double
 tarantool_uptime(void)
@@ -134,8 +135,9 @@ on_shutdown_f(va_list ap)
 }
 
 void
-tarantool_exit(void)
+tarantool_exit(int code)
 {
+	start_loop = false;
 	if (is_shutting_down) {
 		/*
 		 * We are already running on_shutdown triggers,
@@ -145,6 +147,7 @@ tarantool_exit(void)
 		return;
 	}
 	is_shutting_down = true;
+	exit_code = code;
 	fiber_wakeup(on_shutdown_fiber);
 }
 
@@ -164,8 +167,7 @@ signal_cb(ev_loop *loop, struct ev_signal *w, int revents)
 	 */
 	if (pid_file)
 		say_crit("got signal %d - %s", w->signum, strsignal(w->signum));
-	start_loop = false;
-	tarantool_exit();
+	tarantool_exit(0);
 }
 
 static void
@@ -839,5 +841,5 @@ main(int argc, char **argv)
 	if (start_loop)
 		say_crit("exiting the event loop");
 	/* freeing resources */
-	return 0;
+	return exit_code;
 }
diff --git a/src/main.h b/src/main.h
index 221374144..f509e905b 100644
--- a/src/main.h
+++ b/src/main.h
@@ -39,6 +39,9 @@ extern "C" {
 
 double tarantool_uptime(void);
 
+void
+tarantool_exit(int);
+
 void
 load_cfg();
 
-- 
2.17.2 (Apple Git-113)




More information about the Tarantool-patches mailing list