In testing my own app with clang thread sanitizer (TSan) I hit a race condition with ITK. I reduced it to many threads running this code:
typedef itk::Image<float, 2> MyTypedef; MyTypedef::New();
The issue is that two threads end up in itk::ObjectFactoryBase::Initialize() simultaneously and race on the
Should I be able to do this? i.e. is ITK missing some mutex (or other synchronization) here?
Or is it my responsibility to first “prime” ObjectFactoryBase somehow? Doing so seems to fix my issue. If I create a dummy itk::Image on the main thread before spawning all my other threads, then TSan no longer complains.
Interestingly, I tried to create a reproducer test case by hijacking the existing
itkImageAlgorithmCopyTest2 test, but initially I could not repro. I eventually determined this is because
itkTestDriverIncludeRequiredIOFactories.cxx basically “primes” ObjectFactoryBase on the main thread when it calls