From 82c4e1074dac75f9d443c23fa4e807a87671c00a Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Tue, 1 Dec 2020 12:45:46 -0600 Subject: [PATCH] Fix race conditions in the PluginSystem - Use tbb::zero_allocator to be sure std::atomic is nullptr even before its constructor has finished - Avoid having a temporary empty std::shared_ptr be placed in the map before being reset - Be sure object is fully initialized before using it --- FWCore/PluginManager/interface/PluginFactoryBase.h | 2 +- FWCore/PluginManager/src/PluginFactoryBase.cc | 4 ++++ FWCore/PluginManager/src/PluginManager.cc | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/FWCore/PluginManager/interface/PluginFactoryBase.h b/FWCore/PluginManager/interface/PluginFactoryBase.h index 2542e7d20a91f..67577ae3beeaa 100644 --- a/FWCore/PluginManager/interface/PluginFactoryBase.h +++ b/FWCore/PluginManager/interface/PluginFactoryBase.h @@ -61,7 +61,7 @@ namespace edmplugin { std::atomic m_ptr; }; - typedef tbb::concurrent_vector PMakers; + typedef tbb::concurrent_vector> PMakers; typedef tbb::concurrent_unordered_map Plugins; // ---------- const member functions --------------------- diff --git a/FWCore/PluginManager/src/PluginFactoryBase.cc b/FWCore/PluginManager/src/PluginFactoryBase.cc index 67c99ec9c9116..7949753961be4 100644 --- a/FWCore/PluginManager/src/PluginFactoryBase.cc +++ b/FWCore/PluginManager/src/PluginFactoryBase.cc @@ -75,6 +75,10 @@ namespace edmplugin { << "'\n but was not there. This means the plugin cache is incorrect. Please run 'EdmPluginRefresh " << lib << "'"; } + //The item in the container can still be under construction so wait until the m_ptr has been set since that is done last + auto const& value = itFound->second.front(); + while (value.m_ptr.load(std::memory_order_acquire) == nullptr) { + } } else { //The item in the container can still be under construction so wait until the m_ptr has been set since that is done last auto const& value = itFound->second.front(); diff --git a/FWCore/PluginManager/src/PluginManager.cc b/FWCore/PluginManager/src/PluginManager.cc index 2d91f8c96326e..5f8da0342fbbd 100644 --- a/FWCore/PluginManager/src/PluginManager.cc +++ b/FWCore/PluginManager/src/PluginManager.cc @@ -251,7 +251,7 @@ namespace edmplugin { throw; } } - loadables_[p] = ptr; + loadables_.emplace(p, ptr); justLoaded_(*ptr); return *ptr; }