From 632f8c3b14961ed97f515af6ee3359fe52cd29cd Mon Sep 17 00:00:00 2001 From: Teemu Ollakka Date: Wed, 23 Jan 2019 19:07:08 +0200 Subject: [PATCH] Fixed race condition in checking init_initialized on prim view Flag init_initialized_ must be checked before changing the state to s_initializing in on_primary_view() in order to avoid race between main thread and applier thread. Otherwise it is possible that main thread gains control after setting state to initializing and changes the flag init_initialized_ to true before the check is done in on_primary_view(). --- src/server_state.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/server_state.cpp b/src/server_state.cpp index d8a264e..9d76b62 100644 --- a/src/server_state.cpp +++ b/src/server_state.cpp @@ -705,8 +705,17 @@ void wsrep::server_state::on_primary_view( if (state_ == s_connected) { state(lock, s_joiner); + // We need to assign init_initialized_ here to local + // variable. If the value here was false, we need to skip + // the initializing -> initialized -> joined state cycle + // below. However, if we don't assign the value to + // local, it is possible that the main thread gets control + // between changing the state to initializing and checking + // initialized flag, which may cause the initialzing -> initialized + // state change to be executed even if it should not be. + const bool was_initialized(init_initialized_); state(lock, s_initializing); - if (init_initialized_) + if (was_initialized) { // If server side has already been initialized, // skip directly to s_joined.