Hi!
It seems there is a problem with many geometry reinitializations in MT mode. My application (9 logical volumes, local field managers, 16 threads) segfaults in G4LogicalVolume::GetFieldManager() after about 900–2300 rebuilds (11.4.2: varying; 11.4.3: at 911 in two runs, 1424 under gdb).
Some debugging with Claude and valgrind indicates that the problem is in G4WorkerThread::UpdateGeometryAndPhysicsVectorFromMaster(). When the geometry is rebuilt between runs (/run/reinitializeGeometry true, or G4RunManager::ReinitializeGeometry(true) from code), every rebuild creates new logical volumes with new instanceIDs. When the master’s G4GeomSplitter array grows past its capacity, the worker threads’ arrays are not resized before Step 0 of UpdateGeometryAndPhysicsVectorFromMaster(). Step 0 calls lv->GetSensitiveDetector() and lv->GetFieldManager() for every volume in the store whose master SD/FM pointer is set, so for the new volumes it reads offset[instanceID] past the end of the worker’s array. The arrays are only re-created in Steps 1–2, after these reads.
It looks related to bug #2747, but the fix in 11.4.3 doesn’t cover this path: G4WorkerThread.cc is unchanged and Step 0 still runs before the resize.
It can be seen in the unmodified examples/basic/B5, batch mode, with these two macros:
repro_vg.mac:
/run/numberOfThreads 2
/run/initialize
/control/loop rebuild.mac i 1 60 1
rebuild.mac:
/run/reinitializeGeometry true
/run/beamOn 1
G4RUN_MANAGER_TYPE=MT valgrind --tool=memcheck --leak-check=no ./exampleB5 repro_vg.mac
B5 itself doesn’t segfault (I ran 3000 rebuilds) – the out-of-bounds read usually lands in mapped memory – but valgrind reports it every time.
Valgrind output (11.4.3):
Invalid read of size 8
at G4LogicalVolume::GetSensitiveDetector() const (libG4geometry.so)
by G4WorkerThread::UpdateGeometryAndPhysicsVectorFromMaster() (libG4run.so)
by G4WorkerRunManager::DoWork() (libG4run.so)
by G4MTRunManagerKernel::StartThread(G4WorkerThread*) (libG4run.so)
ERROR SUMMARY: 22 errors from 1 contexts
Backtrace of the segfault in my application (11.4.3):
#0 G4LogicalVolume::GetFieldManager() const
#1 G4WorkerThread::UpdateGeometryAndPhysicsVectorFromMaster()
#2 G4WorkerRunManager::DoWork()
#3 G4MTRunManagerKernel::StartThread(G4WorkerThread*)
Geant4 version: 11.4.2 and 11.4.3 (G4MTRunManager)
Operating system: Ubuntu 26.04 (WSL2), g++ 15.2.0