Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladimir Davydov <vdavydov.dev@gmail.com>
To: Serge Petrenko <sergepetrenko@tarantool.org>
Cc: kostja@tarantool.org, tarantool-patches@freelists.org
Subject: Re: [PATCH v5 2/3] lua: patch os.exit() to execute on_shutdown triggers.
Date: Mon, 28 Jan 2019 11:45:33 +0300	[thread overview]
Message-ID: <20190128084533.6qz6cxlsvr6c27rd@esperanza> (raw)
In-Reply-To: <94ded865ada42cee1b807c738dbdd31399a06914.1548430046.git.sergepetrenko@tarantool.org>

On Fri, Jan 25, 2019 at 06:41:33PM +0300, Serge Petrenko wrote:
> 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

Should go before the TarantoolBot request.

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

A test is missing. Please add.

> 
> 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);

os.exit() should exit even if the fiber doesn't yield, but the following
code will hang, which is unexpected. Please fix.

	os.exit()
	while true do end

>  }
>  
> @@ -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();

  reply	other threads:[~2019-01-28  8:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-25 15:41 [PATCH v5 0/3] box: implement " Serge Petrenko
2019-01-25 15:41 ` [PATCH v5 1/3] " Serge Petrenko
2019-01-28  8:43   ` Vladimir Davydov
2019-01-29 14:47     ` Serge Petrenko
2019-01-25 15:41 ` [PATCH v5 2/3] lua: patch os.exit() to execute " Serge Petrenko
2019-01-28  8:45   ` Vladimir Davydov [this message]
2019-01-29 14:49     ` [tarantool-patches] " Serge Petrenko
2019-01-25 15:41 ` [PATCH v5 3/3] box: get rid of atexit() for calling cleanup routines Serge Petrenko
2019-01-30 10:52 ` [PATCH v5 0/3] box: implement on_shutdown triggers Vladimir Davydov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190128084533.6qz6cxlsvr6c27rd@esperanza \
    --to=vdavydov.dev@gmail.com \
    --cc=kostja@tarantool.org \
    --cc=sergepetrenko@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [PATCH v5 2/3] lua: patch os.exit() to execute on_shutdown triggers.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox