Deadlock when using osrt of Nsight Systems that hooks pthread_mutex_lock()
Author: wenjianhnCreated Sep 22, 2026Updated Sep 24, 2026
NSYS_OSRT_pthread_mutex_lock_0, which is a hook of pthread_mutex_lock(), allocates memory.
It lead to the following dead lock issue.
(gdb) p je_arenas_lock.lock.__data.__owner
$6 = 1565767
(gdb) thread apply 2 bt
Thread 2 (Thread 0x7815bdaf86c0 (LWP 1565767) "repro"):
#0 futex_wait (private=0, expected=2, futex_word=0x7815c5ce7700 <je_arenas_lock+64>) at ../sysdeps/nptl/futex-internal.h:146
#1 __GI___lll_lock_wait (futex=futex@entry=0x7815c5ce7700 <je_arenas_lock+64>, private=0) at ./nptl/lowlevellock.c:49
#2 0x00007815c54a01e1 in lll_mutex_lock_optimized (mutex=0x7815c5ce7700 <je_arenas_lock+64>) at ./nptl/pthread_mutex_lock.c:48
#3 ___pthread_mutex_lock (mutex=0x7815c5ce7700 <je_arenas_lock+64>) at ./nptl/pthread_mutex_lock.c:93
#4 0x00007815c40d1bae in NSYS_OSRT_pthread_mutex_lock_0 () from /opt/nvidia/nsight-systems/2026.5.1/target-linux-x64/libToolsInjection64.so
#5 0x00007815c5c9eda1 in malloc_mutex_lock_final (mutex=0x7815c5ce76c0 <je_arenas_lock>) at include/jemalloc/internal/mutex.h:151
#6 je_malloc_mutex_lock_slow (mutex=mutex@entry=0x7815c5ce76c0 <je_arenas_lock>) at src/mutex.c:90
#7 0x00007815c5c240ec in malloc_mutex_lock (mutex=0x7815c5ce76c0 <je_arenas_lock>, tsdn=0x7815bdaf7c78) at include/jemalloc/internal/mutex.h:219
#8 je_arena_choose_hard (tsd=tsd@entry=0x7815bdaf7c78, internal=internal@entry=false) at src/jemalloc.c:534
#9 0x00007815c5c25808 in arena_choose_impl (internal=false, arena=0x0, tsd=0x7815bdaf7c78) at include/jemalloc/internal/jemalloc_internal_inlines_b.h:46
#10 arena_choose (arena=0x0, tsd=0x7815bdaf7c78) at include/jemalloc/internal/jemalloc_internal_inlines_b.h:88
#11 tcache_alloc_small (slow_path=<optimized out>, zero=false, binind=26, size=2856, tcache=0x7815bdaf7fd0, arena=0x0, tsd=0x7815bdaf7c78) at include/jemalloc/internal/tcache_inlines.h:56
#12 arena_malloc (slow_path=<optimized out>, tcache=0x7815bdaf7fd0, zero=false, ind=26, size=2856, arena=0x0, tsdn=0x7815bdaf7c78) at include/jemalloc/internal/arena_inlines_b.h:151
#13 iallocztm (slow_path=<optimized out>, arena=0x0, is_internal=false, tcache=0x7815bdaf7fd0, zero=false, ind=26, size=2856, tsdn=0x7815bdaf7c78) at include/jemalloc/internal/jemalloc_internal_inlines_c.h:55
#14 imalloc_no_sample (ind=26, usize=3072, size=2856, tsd=0x7815bdaf7c78, dopts=<synthetic pointer>, sopts=<synthetic pointer>) at src/jemalloc.c:2404
#15 imalloc_body (tsd=0x7815bdaf7c78, dopts=<synthetic pointer>, sopts=<synthetic pointer>) at src/jemalloc.c:2579
#16 imalloc (dopts=<optimized out>, sopts=<optimized out>) at src/jemalloc.c:2693
#17 je_malloc_default (size=2856) at src/jemalloc.c:2728
#18 0x00007815c5fb5ad1 in malloc (size=<optimized out>) at ../include/rtld-malloc.h:56
#19 allocate_dtv_entry (size=<optimized out>, alignment=32) at ../elf/dl-tls.c:691
#20 allocate_and_init (map=0x7815c503c000) at ../elf/dl-tls.c:704
#21 tls_get_addr_tail (ti=0x7815c4b157b8, dtv=0x7815c505cfd0, the_map=0x7815c503c000) at ../elf/dl-tls.c:904
#22 0x00007815c5fb985c in __tls_get_addr () at ../sysdeps/x86_64/tls_get_addr.S:55
#23 0x00007815c41df99a in ?? () from /opt/nvidia/nsight-systems/2026.5.1/target-linux-x64/libToolsInjection64.so
#24 0x00007815c41b2e86 in ?? () from /opt/nvidia/nsight-systems/2026.5.1/target-linux-x64/libToolsInjection64.so
#25 0x00007815c41b5595 in ?? () from /opt/nvidia/nsight-systems/2026.5.1/target-linux-x64/libToolsInjection64.so
#26 0x00007815c4009e3c in ?? () from /opt/nvidia/nsight-systems/2026.5.1/target-linux-x64/libToolsInjection64.so
#27 0x00007815c40d1bbd in NSYS_OSRT_pthread_mutex_lock_0 () from /opt/nvidia/nsight-systems/2026.5.1/target-linux-x64/libToolsInjection64.so
#28 0x00007815c5c9eda1 in malloc_mutex_lock_final (mutex=0x7815c5ce76c0 <je_arenas_lock>) at include/jemalloc/internal/mutex.h:151
#29 je_malloc_mutex_lock_slow (mutex=mutex@entry=0x7815c5ce76c0 <je_arenas_lock>) at src/mutex.c:90
#30 0x00007815c5c240ec in malloc_mutex_lock (mutex=0x7815c5ce76c0 <je_arenas_lock>, tsdn=0x7815bdaf7c78) at include/jemalloc/internal/mutex.h:219
#31 je_arena_choose_hard (tsd=tsd@entry=0x7815bdaf7c78, internal=internal@entry=false) at src/jemalloc.c:534
#32 0x00007815c5cbcb4a in arena_choose_impl (arena=0x0, internal=false, tsd=0x7815bdaf7c78) at include/jemalloc/internal/jemalloc_internal_inlines_b.h:46
#33 arena_choose (arena=0x0, tsd=0x7815bdaf7c78) at include/jemalloc/internal/jemalloc_internal_inlines_b.h:88
#34 je_tsd_tcache_data_init (tsd=tsd@entry=0x7815bdaf7c78) at src/tcache.c:740
#35 0x00007815c5cbcde0 in je_tsd_tcache_enabled_data_init (tsd=tsd@entry=0x7815bdaf7c78) at src/tcache.c:644
#36 0x00007815c5cbed0d in tsd_data_init (tsd=0x7815bdaf7c78) at src/tsd.c:244
#37 je_tsd_fetch_slow (tsd=tsd@entry=0x7815bdaf7c78, minimal=minimal@entry=false) at src/tsd.c:311
#38 0x00007815c5c2f738 in tsd_fetch_impl (minimal=false, init=true) at include/jemalloc/internal/tsd.h:422
#39 tsd_fetch () at include/jemalloc/internal/tsd.h:448
#40 imalloc (dopts=<synthetic pointer>, sopts=<synthetic pointer>) at src/jemalloc.c:2687
#41 realloc (ptr=ptr@entry=0x0, size=size@entry=32) at src/jemalloc.c:3659
#42 0x00007815c549e05a in __pthread_getattr_np (thread_id=132034772043456, attr=0x7815bdaf7330) at ./nptl/pthread_getattr_np.c:180
#43 0x00007815c41e1854 in ?? () from /opt/nvidia/nsight-systems/2026.5.1/target-linux-x64/libToolsInjection64.so
#44 0x00007815c40d247c in ?? () from /opt/nvidia/nsight-systems/2026.5.1/target-linux-x64/libToolsInjection64.so
#45 0x00007815c549cb84 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:447
#46 0x00007815c5529ecc in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78Steps to reproduce the issue:
g++ -O2 -g -pthread reproducer.cpp -o repro
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 nsys profile --trace=osrt --sample=none --cpuctxsw=none --force-overwrite=true -o output/jemalloc-osrt-default ./repro 100 32reproducer.cpp
#include <atomic>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <thread>
#include <vector>
#include <unistd.h>
// CPU-only reproducer: no CUDA, NVTX, Python, PyTorch, NCCL dependency.
int main(int argc, char** argv) {
const int batches = argc > 1 ? std::atoi(argv[1]) : 10;
const int width = argc > 2 ? std::atoi(argv[2]) : 32;
if (batches <= 0 || width <= 0) {
std::fprintf(stderr, "usage: %s [positive_batches] [positive_threads]\n", argv[0]);
return 2;
}
std::printf("pid=%d batches=%d width=%d\n", getpid(), batches, width);
std::fflush(stdout);
std::atomic<unsigned long> completed{0};
std::atomic<bool> allocation_failed{false};
for (int batch = 0; batch < batches; ++batch) {
std::vector<std::thread> workers;
for (int i = 0; i < width; ++i) {
workers.emplace_back([&completed, &allocation_failed, i] {
const size_t bytes = 65536 + static_cast<size_t>(i) * 16;
char* a = static_cast<char*>(std::malloc(bytes));
char* b = static_cast<char*>(std::malloc(bytes));
if (a == nullptr || b == nullptr) {
std::free(a);
std::free(b);
allocation_failed.store(true, std::memory_order_relaxed);
return;
}
std::memset(a, i, bytes);
std::memcpy(b, a, bytes);
char* expanded = static_cast<char*>(std::realloc(b, bytes * 2));
if (expanded == nullptr) {
std::free(a);
std::free(b);
allocation_failed.store(true, std::memory_order_relaxed);
return;
}
completed.fetch_add(static_cast<unsigned char>(expanded[0]) + 1,
std::memory_order_relaxed);
std::free(a);
std::free(expanded);
});
}
for (auto& worker : workers) worker.join();
if (allocation_failed.load(std::memory_order_relaxed)) {
std::fputs("allocation failed\n", stderr);
return 1;
}
if (batch % 10 == 0) {
std::printf("batch=%d completed=%lu\n", batch,
completed.load(std::memory_order_relaxed));
std::fflush(stdout);
}
}
std::printf("DONE completed=%lu\n", completed.load(std::memory_order_relaxed));
}Source: jemalloc/jemalloc