ThreadSanitizer reports a data race despite mutex-protected access in pthread_cancel cleanup handler

I am encountering a puzzling issue with ThreadSanitizer while using pthread_cancel and a cleanup handler in a multithreaded C++ program. The sanitizer reports a data race on a global variable even though all accesses to the variable are protected by the same mutex. Below is a minimal reproducible example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mtx_test = PTHREAD_MUTEX_INITIALIZER;
int ga = 0;
void cleanup(void *arg)
{
pthread_mutex_lock(&mtx_test);
ga += 1;
pthread_mutex_unlock(&mtx_test);
printf("cleanupn");
}
void* thr_fn(void * arg)
{
pthread_cleanup_push(cleanup, arg);
while(true)
{
pthread_mutex_lock(&mtx_test);
ga += 1;
pthread_mutex_unlock(&mtx_test);
sleep(1);
}
pthread_cleanup_pop(1);
}
int main()
{
pthread_t tid;
pthread_create(&tid, NULL, thr_fn, nullptr);
sleep(3);
pthread_cancel(tid);
pthread_mutex_lock(&mtx_test);
printf("ga: %dn", ga);
pthread_mutex_unlock(&mtx_test);
pthread_join(tid, nullptr);
}
</code>
<code>#include <stdio.h> #include <pthread.h> #include <unistd.h> pthread_mutex_t mtx_test = PTHREAD_MUTEX_INITIALIZER; int ga = 0; void cleanup(void *arg) { pthread_mutex_lock(&mtx_test); ga += 1; pthread_mutex_unlock(&mtx_test); printf("cleanupn"); } void* thr_fn(void * arg) { pthread_cleanup_push(cleanup, arg); while(true) { pthread_mutex_lock(&mtx_test); ga += 1; pthread_mutex_unlock(&mtx_test); sleep(1); } pthread_cleanup_pop(1); } int main() { pthread_t tid; pthread_create(&tid, NULL, thr_fn, nullptr); sleep(3); pthread_cancel(tid); pthread_mutex_lock(&mtx_test); printf("ga: %dn", ga); pthread_mutex_unlock(&mtx_test); pthread_join(tid, nullptr); } </code>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t mtx_test = PTHREAD_MUTEX_INITIALIZER;
int ga = 0;
void cleanup(void *arg)
{
    pthread_mutex_lock(&mtx_test);
    ga += 1;
    pthread_mutex_unlock(&mtx_test);
    printf("cleanupn");
}

void* thr_fn(void * arg)
{
    pthread_cleanup_push(cleanup, arg);
    while(true)
    {
        pthread_mutex_lock(&mtx_test);
        ga += 1;
        pthread_mutex_unlock(&mtx_test);
        sleep(1);
    }
    pthread_cleanup_pop(1);
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, NULL, thr_fn, nullptr);

    sleep(3);
    pthread_cancel(tid);

    pthread_mutex_lock(&mtx_test);
    printf("ga: %dn", ga);
    pthread_mutex_unlock(&mtx_test);

    pthread_join(tid, nullptr);
}

And here is the output reported by ThreadSanitizer:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>==================
WARNING: ThreadSanitizer: data race (pid=29924)
Write of size 4 at 0x5593bd535068 by thread T1:
#0 cleanup(void*) /tmp/test/test.cpp:10 (a.out+0x139b) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)
#1 __pthread_cleanup_class::~__pthread_cleanup_class() /usr/include/pthread.h:578 (a.out+0x16c9) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)
#2 thr_fn(void*) /tmp/test/test.cpp:25 (a.out+0x1493) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)
#3 thr_fn(void*) /tmp/test/test.cpp:23 (a.out+0x147e) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)
Previous read of size 4 at 0x5593bd535068 by main thread (mutexes: write M0):
#0 main /tmp/test/test.cpp:37 (a.out+0x153c) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)
As if synchronized via sleep:
#0 sleep ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:383 (libtsan.so.2+0x58691) (BuildId: 38097064631f7912bd33117a9c83d08b42e15571)
#1 thr_fn(void*) /tmp/test/test.cpp:23 (a.out+0x147e) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)
Location is global 'ga' of size 4 at 0x5593bd535068 (a.out+0x4068)
Mutex M0 (0x5593bd535040) created at:
#0 pthread_mutex_lock ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:1341 (libtsan.so.2+0x59a13) (BuildId: 38097064631f7912bd33117a9c83d08b42e15571)
#1 thr_fn(void*) /tmp/test/test.cpp:20 (a.out+0x1438) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)
Thread T1 (tid=29926, running) created by main thread at:
#0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:1022 (libtsan.so.2+0x5ac1a) (BuildId: 38097064631f7912bd33117a9c83d08b42e15571)
#1 main /tmp/test/test.cpp:31 (a.out+0x14fc) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)
SUMMARY: ThreadSanitizer: data race /tmp/test/test.cpp:10 in cleanup(void*)
==================
</code>
<code>================== WARNING: ThreadSanitizer: data race (pid=29924) Write of size 4 at 0x5593bd535068 by thread T1: #0 cleanup(void*) /tmp/test/test.cpp:10 (a.out+0x139b) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791) #1 __pthread_cleanup_class::~__pthread_cleanup_class() /usr/include/pthread.h:578 (a.out+0x16c9) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791) #2 thr_fn(void*) /tmp/test/test.cpp:25 (a.out+0x1493) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791) #3 thr_fn(void*) /tmp/test/test.cpp:23 (a.out+0x147e) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791) Previous read of size 4 at 0x5593bd535068 by main thread (mutexes: write M0): #0 main /tmp/test/test.cpp:37 (a.out+0x153c) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791) As if synchronized via sleep: #0 sleep ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:383 (libtsan.so.2+0x58691) (BuildId: 38097064631f7912bd33117a9c83d08b42e15571) #1 thr_fn(void*) /tmp/test/test.cpp:23 (a.out+0x147e) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791) Location is global 'ga' of size 4 at 0x5593bd535068 (a.out+0x4068) Mutex M0 (0x5593bd535040) created at: #0 pthread_mutex_lock ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:1341 (libtsan.so.2+0x59a13) (BuildId: 38097064631f7912bd33117a9c83d08b42e15571) #1 thr_fn(void*) /tmp/test/test.cpp:20 (a.out+0x1438) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791) Thread T1 (tid=29926, running) created by main thread at: #0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:1022 (libtsan.so.2+0x5ac1a) (BuildId: 38097064631f7912bd33117a9c83d08b42e15571) #1 main /tmp/test/test.cpp:31 (a.out+0x14fc) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791) SUMMARY: ThreadSanitizer: data race /tmp/test/test.cpp:10 in cleanup(void*) ================== </code>
==================
WARNING: ThreadSanitizer: data race (pid=29924)
  Write of size 4 at 0x5593bd535068 by thread T1:
    #0 cleanup(void*) /tmp/test/test.cpp:10 (a.out+0x139b) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)
    #1 __pthread_cleanup_class::~__pthread_cleanup_class() /usr/include/pthread.h:578 (a.out+0x16c9) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)
    #2 thr_fn(void*) /tmp/test/test.cpp:25 (a.out+0x1493) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)
    #3 thr_fn(void*) /tmp/test/test.cpp:23 (a.out+0x147e) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)

  Previous read of size 4 at 0x5593bd535068 by main thread (mutexes: write M0):
    #0 main /tmp/test/test.cpp:37 (a.out+0x153c) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)

  As if synchronized via sleep:
    #0 sleep ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:383 (libtsan.so.2+0x58691) (BuildId: 38097064631f7912bd33117a9c83d08b42e15571)
    #1 thr_fn(void*) /tmp/test/test.cpp:23 (a.out+0x147e) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)

  Location is global 'ga' of size 4 at 0x5593bd535068 (a.out+0x4068)

  Mutex M0 (0x5593bd535040) created at:
    #0 pthread_mutex_lock ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:1341 (libtsan.so.2+0x59a13) (BuildId: 38097064631f7912bd33117a9c83d08b42e15571)
    #1 thr_fn(void*) /tmp/test/test.cpp:20 (a.out+0x1438) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)

  Thread T1 (tid=29926, running) created by main thread at:
    #0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:1022 (libtsan.so.2+0x5ac1a) (BuildId: 38097064631f7912bd33117a9c83d08b42e15571)
    #1 main /tmp/test/test.cpp:31 (a.out+0x14fc) (BuildId: ad9c32e6694e2996bfed96f0eb507bec0278f791)

SUMMARY: ThreadSanitizer: data race /tmp/test/test.cpp:10 in cleanup(void*)
==================

The global variable ga is only accessed while holding the mtx_test mutex.
Both in the cleanup handler and in main, the pthread_mutex_lock is explicitly used before accessing ga, ensuring mutual exclusion.
The warning points to a “data race” on ga, but this seems incorrect because the mutex guarantees thread-safe access.

Why does ThreadSanitizer report a data race in this case? Is this a known limitation of ThreadSanitizer when using pthread_cancel and cleanup handlers? If so, how can I address or suppress this false positive? Or is there an actual flaw in my code that I am overlooking?

Any help or insights would be greatly appreciated! Thank you!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật