From bfd26e5302b4ba6fabcda07e74de76d0d565af24 Mon Sep 17 00:00:00 2001 From: Vakhtang Tsulaia <vakhtang.tsulaia@cern.ch> Date: Thu, 16 Sep 2021 16:22:47 +0200 Subject: [PATCH] Merge branch 'race.GeneratorObjects-20210916' into 'master' GeneratorObjects: Fix potential race. See merge request atlas/athena!46528 (cherry picked from commit a5d2958c0edc0bcd99b3f35a78eff333156b319b) a5176988 GeneratorObjects: Fix potential race. --- .../src/HepMcParticleLink.cxx | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Generators/GeneratorObjects/src/HepMcParticleLink.cxx b/Generators/GeneratorObjects/src/HepMcParticleLink.cxx index d744c198a05a..0fbd7512e88a 100644 --- a/Generators/GeneratorObjects/src/HepMcParticleLink.cxx +++ b/Generators/GeneratorObjects/src/HepMcParticleLink.cxx @@ -1,5 +1,5 @@ /* - Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration */ @@ -211,6 +211,11 @@ const HepMC::GenParticle* HepMcParticleLink::cptr() const if (0 != pEvt) { auto pp = HepMC::barcode_to_particle(pEvt,barcode()); + // Be sure to update m_extBarcode before m_ptrs; + // otherwise, the logic in eventIndex() won't work correctly. + if (position != ExtendedBarCode::UNDEFINED) { + m_extBarcode.makeIndex (pEvt->event_number(), position); + } if (pp) { #ifdef HEPMC3 m_ptrs.set (sg, pp.get()); @@ -220,9 +225,6 @@ const HepMC::GenParticle* HepMcParticleLink::cptr() const p=pp; #endif } - if (position != ExtendedBarCode::UNDEFINED) { - m_extBarcode.makeIndex (pEvt->event_number(), position); - } } else { MsgStream log (Athena::getMessageSvc(), "HepMcParticleLink"); if (position != ExtendedBarCode::UNDEFINED) { @@ -343,6 +345,15 @@ HepMcParticleLink::index_type HepMcParticleLink::eventIndex() const if (pEvt) { const int event_number = pEvt->event_number(); auto pp = HepMC::barcode_to_particle(pEvt,barcode()); + // Be sure to update m_extBarcode before m_ptrs. + // Otherwise, if two threads run this method simultaneously, + // one thread could see index == UNDEFINED, but where m_ptr + // is already updated so we get nullptr back for sg. + if(event_number>-1) { + index = static_cast<index_type>(event_number); + m_extBarcode.makeIndex (index, position); + return index; + } if (pp) { #ifdef HEPMC3 m_ptrs.set (sg, pp.get()); @@ -350,11 +361,6 @@ HepMcParticleLink::index_type HepMcParticleLink::eventIndex() const m_ptrs.set (sg, pp); #endif } - if(event_number>-1) { - index = static_cast<index_type>(event_number); - m_extBarcode.makeIndex (index, position); - return index; - } } } } -- GitLab