From 69d951fed2c047a3e74fb745c7cddd000daf07e0 Mon Sep 17 00:00:00 2001
From: Nils Krumnack <nils.erik.krumnack@cern.ch>
Date: Tue, 18 Mar 2025 10:17:24 -0500
Subject: [PATCH] switch the ComponentFactoryPreloader to only run once

I had some cases in which this got called more than once, causing errors
on the subsequent calls.  This should fix that.
---
 .../Root/ComponentFactoryPreloader.cxx             | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/PhysicsAnalysis/Algorithms/ComponentFactoryPreloader/Root/ComponentFactoryPreloader.cxx b/PhysicsAnalysis/Algorithms/ComponentFactoryPreloader/Root/ComponentFactoryPreloader.cxx
index 019404abd748..e566514c4e61 100644
--- a/PhysicsAnalysis/Algorithms/ComponentFactoryPreloader/Root/ComponentFactoryPreloader.cxx
+++ b/PhysicsAnalysis/Algorithms/ComponentFactoryPreloader/Root/ComponentFactoryPreloader.cxx
@@ -12,6 +12,7 @@
 
 #include <AsgTools/AsgComponentFactories.h>
 #include <AsgTools/MessageCheckAsgTools.h>
+#include <mutex>
 
 #include <AsgAnalysisAlgorithms/AsgClassificationDecorationAlg.h>
 #include <AsgAnalysisAlgorithms/AsgCutBookkeeperAlg.h>
@@ -203,7 +204,10 @@
 
 namespace CP
 {
-  bool preloadComponentFactories ()
+  // this function gets called once by the function below.  calling it
+  // once avoids any errors if setup happens multiple times, e.g. in
+  // test fixtures
+  static bool doPreloadComponentFactories ()
   {
     using namespace asg::msgComponentConfig;
     ANA_CHECK_SET_TYPE (bool);
@@ -398,4 +402,12 @@ namespace CP
 
     return true;
   }
+
+  bool preloadComponentFactories ()
+  {
+    static bool result = false;
+    static std::once_flag flag;
+    std::call_once (flag, [&] () { result = doPreloadComponentFactories (); });
+    return result;
+  }
 }
-- 
GitLab