Skip to content

Commit f206cf5

Browse files
shahor02sawenzel
authored andcommitted
Fix race condition in HitMerger
lock regions where mEventToTTreeMap and mEventToTMemFileMap are modified
1 parent 5a58196 commit f206cf5

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

run/O2HitMerger.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include <map>
6262
#include <vector>
6363
#include <csignal>
64+
#include <mutex>
6465

6566
#ifdef ENABLE_UPGRADES
6667
#include <ITS3Simulation/Detector.h>
@@ -192,7 +193,7 @@ class O2HitMerger : public FairMQDevice
192193
void fillBranch(int eventID, std::string const& name, T* ptr)
193194
{
194195
// fetch tree into which to fill
195-
196+
const std::lock_guard<std::mutex> lock(mMapsMtx);
196197
auto iter = mEventToTTreeMap.find(eventID);
197198
if (iter == mEventToTTreeMap.end()) {
198199
{
@@ -312,11 +313,11 @@ class O2HitMerger : public FairMQDevice
312313
void cleanEvent(int eventID)
313314
{
314315
// remove tree for that eventID
316+
const std::lock_guard<std::mutex> lock(mMapsMtx);
315317
delete mEventToTTreeMap[eventID];
316-
mEventToTTreeMap.erase(eventID);
317-
// remove memfile
318318
delete mEventToTMemFileMap[eventID];
319-
mEventToTMemFileMap.erase(eventID);
319+
mEventToTTreeMap.erase(eventID);
320+
mEventToTMemFileMap.erase(eventID); // remove memfile
320321
}
321322

322323
template <typename T>
@@ -648,7 +649,7 @@ class O2HitMerger : public FairMQDevice
648649
std::unordered_map<int, TTree*> mEventToTTreeMap; //! in memory trees to collect / presort incoming data per event
649650
std::unordered_map<int, TMemFile*> mEventToTMemFileMap; //! files associated to the TTrees
650651
std::thread mMergerIOThread; //! a thread used to do hit merging and IO flushing asynchronously
651-
652+
std::mutex mMapsMtx; //!
652653
int mEntries = 0; //! counts the number of entries in the branches
653654
int mEventChecksum = 0; //! checksum for events
654655
int mNExpectedEvents = 0; //! number of events that we expect to receive

0 commit comments

Comments
 (0)