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 20A3F21B63 for ; Tue, 11 Dec 2018 08:26:09 -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 HZOEsRhkNpXU for ; Tue, 11 Dec 2018 08:26:08 -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 1BE8B21B78 for ; Tue, 11 Dec 2018 08:24:48 -0500 (EST) From: Serge Petrenko Subject: [tarantool-patches] [PATCH v3 2/3] box: implement on_shutdown triggers Date: Tue, 11 Dec 2018 16:24:39 +0300 Message-Id: <33b51ca5edbeb8bce134d62bff83e02faaa936e9.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 From: Konstantin Osipov @sergepetrenko: Instead of running on_shutdown trigggers in box_free() execute them in a fiber which breaks event loop before the break. In tarantool_free() move box_free() back after coio_shutdown(). Otherwise tarantool hangs on shutdown occasionally. Part of #1607 --- src/box/box.cc | 8 ++++++++ src/box/box.h | 11 +++++++++++ src/main.cc | 16 ++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/box/box.cc b/src/box/box.cc index 9642364f6..72119eca1 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -79,6 +79,8 @@ static char status[64] = "unknown"; /** box.stat rmean */ struct rmean *rmean_box; +struct rlist on_shutdown = RLIST_HEAD_INITIALIZER(on_shutdown); + static void title(const char *new_status) { snprintf(status, sizeof(status), "%s", new_status); @@ -1671,6 +1673,12 @@ box_set_replicaset_uuid(const struct tt_uuid *replicaset_uuid) diag_raise(); } +void +box_run_on_shutdown_triggers(void) +{ + trigger_run(&on_shutdown, NULL); +} + void box_free(void) { diff --git a/src/box/box.h b/src/box/box.h index cb9a512be..e9c14e5cf 100644 --- a/src/box/box.h +++ b/src/box/box.h @@ -64,6 +64,11 @@ struct vclock; */ extern const struct vclock *box_vclock; +struct trigger; + +/** Invoked on box shutdown. */ +extern struct rlist on_shutdown; + /* * Initialize box library * @throws C++ exception @@ -84,6 +89,12 @@ box_free(void); void box_shutdown_wal(void); +/** + * Run on_shutdown triggers. + */ +void +box_run_on_shutdown_triggers(void); + /** * Load configuration for box library. * Panics on error. diff --git a/src/main.cc b/src/main.cc index 2240c564a..8bcc785d5 100644 --- a/src/main.cc +++ b/src/main.cc @@ -124,6 +124,11 @@ static int on_exit_f(va_list ap) { (void) ap; + /* + * run on_shutdown triggers before event loop break, + * so that we are able to yield in them. + */ + box_run_on_shutdown_triggers(); /* Terminate the main event loop. */ ev_break(loop(), EVBREAK_ALL); return 0; @@ -132,6 +137,17 @@ on_exit_f(va_list ap) void tarantool_exit(void) { + static volatile sig_atomic_t num_calls = 0; + /* + * We are already running on_shutdown triggers, + * and will exit as soon as they'll finish. + * Do not execute them twice. + */ + if (num_calls > 0) + return; + + ++num_calls; + struct fiber *f = fiber_new("on_shutdown", on_exit_f); if (f == NULL) { say_warn("failed to allocate a fiber to run shutdown routines."); -- 2.17.2 (Apple Git-113)