static thread_local destructor not called at program exit

Hi all,

I’ve been using static thread_locals and I observed confusing behavior for thread_local destruction at program exit. In my simplified example, an object owns a thread and joins on it when the object is destroyed. This is derived from a thread pool construct.

The object, defined as Wrapper, is held in a static global and I expect it to be destroyed at the end of main. The issue is that the static thread_local destructor from the spawned thread owned by Wrapper is not called and is reported as a leak by leak sanitizer. However, if I destroy the object before the end of main, the static thread_local destructor is indeed called.

My understanding is that thread_local destructors should be invoked before join returns. Why is it that the thread_local destructor is not invoked when the static global object is destroyed and joins on the thread after main ends?

In the output below, there is a message for the construction of the thread_local, but no corresponding message for the destruction of the thread_local.

clang --version

clang version 3.7.0 (tags/RELEASE_370/final)

Target: x86_64-redhat-linux-gnu

Thread model: posix

clang++ -g -o foo foo.cc -fsanitize=leak -std=c++11 -lpthread

./foo

Started 140182605002496 from 140182628901568

Constructed thread_local from 140182605002496

Joining 140182605002496 from 140182628901568

foo.cc (1.86 KB)