Skip to content

Race condition in TlsAbstraction #20606

@vrabaud

Description

@vrabaud
System information (version)
  • OpenCV => 4.5.3
  • Operating System / Platform => UNIX
  • Compiler => clang
Detailed description

Our thread sanitizer finds the following race condition:
WARNING: ThreadSanitizer: data race (pid=5750)
Write of size 1 at 0x562bc541ed81 by main thread:
#0 ~DisposedSingletonMark OpenCV/v4_5_3/modules/core/src/system.cpp:1387:14
#1 cv::details::TlsAbstraction::~TlsAbstraction() OpenCV/v4_5_3/modules/core/src/system.cpp:1510:1
#2 cxa_at_exit_wrapper(void*) tsan_interceptors_posix.cpp

Previous read of size 1 at 0x562bc541ed81 by thread T31:
#0 isDisposed OpenCV/v4_5_3/modules/core/src/system.cpp:1390:39
#1 getTlsAbstraction OpenCV/v4_5_3/modules/core/src/system.cpp:1435:12
#2 cv::details::TlsStorage::releaseThread(void*) OpenCV/v4_5_3/modules/core/src/system.cpp:1559:31
#3 cv::details::opencv_tls_destructor(void*) OpenCV/v4_5_3/modules/core/src/system.cpp:1753:21

Location is global 'cv::details::DisposedSingletonMarkcv::details::TlsAbstraction::mark' of size 1 at 0x562bc541ed81

Line numbers might be slightly off.

If I understand correctly, it seems that ~DisposedSingletonMark/~TlsAbstraction can be called by the main thread (thus setting mark = true) when destroying static TlsAbstraction g_tls; in getTlsAbstraction_ in the termination of the main thread.
At the same time DisposedSingletonMark::isDisposed() reads mark when called from releaseThread in a secondary thread.

I cannot find the order in which static variables are destroyed at https://en.cppreference.com/w/cpp/utility/program/exit (basically whether it waits for other threads to be done or not) so I guess it is implementation defined.

Issue submission checklist
  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues,
    forum.opencv.org, Stack Overflow, etc and have not found solution
  • I updated to latest OpenCV version and the issue is still there

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions