From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id C219B21B8F for ; Tue, 11 Dec 2018 08:26:08 -0500 (EST) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id WNL0n69vkgim for ; Tue, 11 Dec 2018 08:24:47 -0500 (EST) Received: from smtp56.i.mail.ru (smtp56.i.mail.ru [217.69.128.36]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id F1D4521B6A for ; Tue, 11 Dec 2018 08:24:46 -0500 (EST) From: Serge Petrenko Subject: [tarantool-patches] [PATCH v3 1/3] box: get rid of atexit() for calling cleanup routine Date: Tue, 11 Dec 2018 16:24:38 +0300 Message-Id: <98b93e4fd2d6d6affb4dc191ad5d4e8432ffefaa.1544533138.git.sergepetrenko@tarantool.org> In-Reply-To: References: In-Reply-To: References: Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: kostja@tarantool.org Cc: tarantool-patches@freelists.org, Serge Petrenko Move a call to tarantool_free() to the end of main(). Also instead of breaking the event loop directly when processing a signal start a fiber which can do some work before event loop break and then break the event loop (this will be helpful when on_shutdown triggers are implemented to run them while ev loop active). Only wal_thread_stop() is left in atexit() to make sure we close the journal in case the user exits by typing os.exit() to the console. --- src/box/box.cc | 8 +++++++ src/box/box.h | 7 ++++++ src/main.cc | 59 ++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/box/box.cc b/src/box/box.cc index 9f2fd6da1..9642364f6 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -1693,9 +1693,17 @@ box_free(void) gc_free(); engine_shutdown(); wal_thread_stop(); + is_box_configured = false; } } +void +box_shutdown_wal(void) +{ + if (is_box_configured) + wal_thread_stop(); +} + static void engine_init() { diff --git a/src/box/box.h b/src/box/box.h index 6c6c319fc..cb9a512be 100644 --- a/src/box/box.h +++ b/src/box/box.h @@ -77,6 +77,13 @@ box_init(void); void box_free(void); +/** + * Kill WAL thread and close journal. + * Called at exit. + */ +void +box_shutdown_wal(void); + /** * Load configuration for box library. * Panics on error. diff --git a/src/main.cc b/src/main.cc index 993355ac4..2240c564a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -83,6 +83,7 @@ static char *script = NULL; static char *pid_file = NULL; static char **main_argv; static int main_argc; + /** Signals handled after start as part of the event loop. */ static ev_signal ev_sigs[6]; static const int ev_sig_count = sizeof(ev_sigs)/sizeof(*ev_sigs); @@ -119,11 +120,33 @@ sig_checkpoint(ev_loop * /* loop */, struct ev_signal * /* w */, fiber_wakeup(f); } +static int +on_exit_f(va_list ap) +{ + (void) ap; + /* Terminate the main event loop. */ + ev_break(loop(), EVBREAK_ALL); + return 0; +} + +void +tarantool_exit(void) +{ + struct fiber *f = fiber_new("on_shutdown", on_exit_f); + if (f == NULL) { + say_warn("failed to allocate a fiber to run shutdown routines."); + ev_break(loop(), EVBREAK_ALL); + return; + } + fiber_wakeup(f); +} + static void signal_cb(ev_loop *loop, struct ev_signal *w, int revents) { (void) w; (void) revents; + (void) loop; /** * If running in daemon mode, complain about possibly @@ -135,8 +158,8 @@ 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; - /* Terminate the main event loop */ - ev_break(loop, EVBREAK_ALL); + + tarantool_exit(); } static void @@ -534,6 +557,24 @@ load_cfg() box_cfg(); } +void +tarantool_atexit(void) +{ + box_shutdown_wal(); + + /* tarantool_lua_free() was formerly reponsible for terminal reset, + * but it is no longer called + */ + if (isatty(STDIN_FILENO)) { + /* + * Restore terminal state. Doesn't hurt if exiting not + * due to a signal. + */ + rl_cleanup_after_signal(); + } + +} + void tarantool_free(void) { @@ -568,16 +609,6 @@ tarantool_free(void) #ifdef ENABLE_GCOV __gcov_flush(); #endif - /* tarantool_lua_free() was formerly reponsible for terminal reset, - * but it is no longer called - */ - if (isatty(STDIN_FILENO)) { - /* - * Restore terminal state. Doesn't hurt if exiting not - * due to a signal. - */ - rl_cleanup_after_signal(); - } cbus_free(); #if 0 /* @@ -752,8 +783,7 @@ main(int argc, char **argv) box_init(); box_lua_init(tarantool_L); - /* main core cleanup routine */ - atexit(tarantool_free); + atexit(tarantool_atexit); if (!loop()) panic("%s", "can't init event loop"); @@ -793,5 +823,6 @@ main(int argc, char **argv) if (start_loop) say_crit("exiting the event loop"); /* freeing resources */ + tarantool_free(); return 0; } -- 2.17.2 (Apple Git-113)