Commit Diff


commit - dc5edaa4ce5f75a3d2d69fef40c3c226ef7d12a4
commit + e72eaa8a4868167d49984c74a1cb884ba3887e7a
blob - /dev/null
blob + a4ee68fafef6d0f9ef3914e1a293e51558d1a094 (mode 644)
--- /dev/null
+++ changelogs/unreleased/gh-9266-fix-on-shutdown-and-osexit-from-cmd-expr.md
@@ -0,0 +1,4 @@
+## bugfix/box
+
+* Fixed a bug when `on_shutdown` triggers weren't run if `os.exit()` was
+  called from `-e` command-line option (gh-9266).
blob - bfbd354bf0d33d21ab72cf8cfe9d88370bec1cf7
blob + 389abddf88d9b2f6e1b474bb587a7f4c4e59f885
--- src/lua/init.c
+++ src/lua/init.c
@@ -1057,8 +1057,17 @@ run_script_f(va_list ap)
 	 * never really dead. It never returns from its function.
 	 */
 	struct diag *diag = va_arg(ap, struct diag *);
-	bool aux_loop_is_run = false;
 	bool is_option_e_ran = false;
+
+	/*
+	 * Return control to tarantool_lua_run_script.
+	 * tarantool_lua_run_script then will start an auxiliary event
+	 * loop and re-schedule this fiber.
+	 *
+	 * This also update time in ev after Tarantool startup which
+	 * reduce time slip in timers (see #9261).
+	 */
+	fiber_sleep(0.0);
 
 	/*
 	 * Execute scripts or modules pointed by TT_PRELOAD
@@ -1118,14 +1127,6 @@ run_script_f(va_list ap)
 		}
 	}
 
-	/*
-	 * Return control to tarantool_lua_run_script.
-	 * tarantool_lua_run_script then will start an auxiliary event
-	 * loop and re-schedule this fiber.
-	 */
-	fiber_sleep(0.0);
-	aux_loop_is_run = true;
-
 	int is_a_tty = isatty(STDIN_FILENO);
 
 	if (bytecode) {
@@ -1189,13 +1190,6 @@ run_script_f(va_list ap)
 	 * return control back to tarantool_lua_run_script.
 	 */
 end:
-	/*
-	 * Auxiliary loop in tarantool_lua_run_script
-	 * should start (ev_run()) before this fiber
-	 * invokes ev_break().
-	 */
-	if (!aux_loop_is_run)
-		fiber_sleep(0.0);
 	ev_break(loop(), EVBREAK_ALL);
 	return 0;
 
@@ -1238,8 +1232,7 @@ tarantool_lua_run_script(char *path, uint32_t opt_mask
 	 * Run an auxiliary event loop to re-schedule run_script fiber.
 	 * When this fiber finishes, it will call ev_break to stop the loop.
 	 */
-	if (start_loop)
-		ev_run(loop(), 0);
+	ev_run(loop(), 0);
 	/* The fiber running the startup script has ended. */
 	script_fiber = NULL;
 	diag_move(&script_diag, diag_get());
blob - /dev/null
blob + 15152652add449fac1d28889ac39e1e44ebf0ab3 (mode 644)
--- /dev/null
+++ test/box-luatest/gh_9266_fix_on_shutdown_and_osexit_from_cmd_expr_test.lua
@@ -0,0 +1,34 @@
+local t = require('luatest')
+local popen = require('popen')
+
+local g = t.group()
+
+local tarantool = arg[-1]
+
+g.after_each(function()
+    if g.handle ~= nil then
+        g.handle:close()
+    end
+    g.handle = nil
+end)
+
+g.test = function()
+    local code = [[
+        box.ctl.on_shutdown(function()
+            print('hello')
+        end)
+        os.exit()
+    ]]
+    local handle, err = popen.new({tarantool, '-e', code},
+                                  {stdout = popen.opts.PIPE,
+                                   stdin = popen.opts.DEVNULL,
+                                   stderr = popen.opts.DEVNULL})
+    assert(handle, err)
+    g.handle = handle
+    local output, err = handle:read({timeout = 3})
+    assert(output, err)
+    t.assert_equals(output, "hello\n")
+    local status = handle:wait()
+    t.assert_equals(status.state, 'exited')
+    t.assert_equals(status.exit_code, 0)
+end