Reputation: 83
I am profiling a program compiled with gcc 6.1 with -fsanitize=address option. The program is multi-threaded with clean exits for each thread (with pthread_exit).
Address Sanitizer fails with the message: ==16800==AddressSanitizer: Thread limit (4194304 threads) exceeded. Dying.
The thread count makes no sense (there is no way I am allocating that many threads). Is there a way I can instrument AddressSanitizer to see where and how the threads are being created?
Upvotes: 3
Views: 1169
Reputation: 108
The issue you're encountering with AddressSanitizer (ASan) reporting an extremely high thread count usually indicates that threads are not being properly joined or detached, leading to resource leaks.
Here are 2 steps you can take to debug this issue:
Check Thread Joining and Detaching: Ensure that all threads are either joined with pthread_join or detached with pthread_detach. If threads are neither joined nor detached, they will remain in the system's thread table, potentially leading to the issue you're seeing.
Use ASan Thread Annotations: ASan provides thread annotations that can help you track the creation and destruction of threads:
__asan_register_thread
: Registers a thread with ASan.
__asan_unregister_thread
: Unregisters a thread with ASan.
You can use these functions to annotate your thread creation and destruction points.
Here is the code:
#include <pthread.h>
#include <sanitizer/asan_interface.h>
void* thread_func(void* arg) {
__asan_register_thread();
// Your thread code here
__asan_unregister_thread();
return nullptr;
}
int main() {
pthread_t thread;
pthread_create(&thread, nullptr, thread_func, nullptr);
pthread_join(thread, nullptr);
return 0;
}
Upvotes: 0