From ba78fa82a2e52b550f54324641a5f7602fc549f5 Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Wed, 30 Sep 2020 08:36:29 -0600 Subject: [PATCH] Wrap access to facade_factory in a shared lock so it doesn't get changed partway through access, leading to a crash. --- include/engine/data_watchdog.hpp | 46 ++++++++++++++++---------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/include/engine/data_watchdog.hpp b/include/engine/data_watchdog.hpp index b71ab080325..0f65ed10f65 100644 --- a/include/engine/data_watchdog.hpp +++ b/include/engine/data_watchdog.hpp @@ -56,11 +56,14 @@ class DataWatchdogImpl>( - std::make_shared( - std::vector{ - static_region.shm_key, updatable_region.shm_key})); + { + boost::unique_lock swap_lock(factory_mutex); + facade_factory = + DataFacadeFactory( + std::make_shared( + std::vector{ + static_region.shm_key, updatable_region.shm_key})); + } } watcher = std::thread(&DataWatchdogImpl::Run, this); @@ -75,21 +78,15 @@ class DataWatchdogImpl Get(const api::BaseParameters ¶ms) const { - // Ensure that an exchange of facade_factory while ->Get() is in progress - // doesn't access an object undergoing destruction from the Run() thread - // We increase the ref count on facade_factory, and it only gets decreased - // (possibly to zero) *after* this function returns - auto facade_factory_copy = facade_factory; - return facade_factory_copy->Get(params); + // make sure facade_factory stays stable while we call Get() + boost::shared_lock swap_lock(factory_mutex); + return facade_factory.Get(params); } std::shared_ptr Get(const api::TileParameters ¶ms) const { - // Ensure that an exchange of facade_factory while ->Get() is in progress - // doesn't access an object undergoing destruction from the Run() thread - // We increase the ref count on facade_factory, and it only gets decreased - // (possibly to zero) *after* this function returns - auto facade_factory_copy = facade_factory; - return facade_factory_copy->Get(params); + // make sure facade_factory stays stable while we call Get() + boost::shared_lock swap_lock(factory_mutex); + return facade_factory.Get(params); } private: @@ -121,16 +118,19 @@ class DataWatchdogImpl>( - std::make_shared( - std::vector{ - static_region.shm_key, updatable_region.shm_key})); + { + boost::unique_lock swap_lock(factory_mutex); + facade_factory =DataFacadeFactory( + std::make_shared( + std::vector{ + static_region.shm_key, updatable_region.shm_key})); + } } util::Log() << "DataWatchdog thread stopped"; } + mutable boost::shared_mutex factory_mutex; const std::string dataset_name; storage::SharedMonitor barrier; std::thread watcher; @@ -139,7 +139,7 @@ class DataWatchdogImpl> facade_factory; + DataFacadeFactory facade_factory; }; }