Segfault using cuda-gdb 12 with cusparseCreate() in a thread

Running this with cuda-gdb throws a segfault originating from a debugger lib libcudadebugger.so.1. What causes this?


// test.cu


#include <iostream>
#include <thread>
#include <cusparse.h>

#define CUSPARSE_CHECK(call) \
do { \
    cusparseStatus_t status = call; \
    if (status != CUSPARSE_STATUS_SUCCESS) { \
        fprintf(stderr, "cuSPARSE error at %s:%d: %d\n", \
                __FILE__, __LINE__, status); \
        exit(EXIT_FAILURE); \
    } \
} while(0)

int main() {
    const int num_threads = 4; // Number of threads
    std::thread threads[num_threads]; // Array to hold threads

    for (int i = 0; i < num_threads; ++i) {
        threads[i] = std::thread([i]() {
            // Create cuSPARSE handle
            cusparseHandle_t h;
            CUSPARSE_CHECK(cusparseCreate(&h));

            // Create CUDA stream
            cudaStream_t stream;
            cudaStreamCreate(&stream);

            // Use the cuSPARSE handle and CUDA stream in this thread
            printf("Thread %d: cuSPARSE handle: %p, CUDA stream: %p\n", i, h, stream);

            // Destroy cuSPARSE handle and CUDA stream after use
            CUSPARSE_CHECK(cusparseDestroy(h));
            cudaStreamDestroy(stream);
        });
    }

    // Join all threads
    for (int i = 0; i < num_threads; ++i) {
        threads[i].join();
    }

    return 0;
}

I didn’t have any trouble on CUDA 12.2. Full test case:

# cat t145.cu
// test.cu


#include <iostream>
#include <thread>
#include <cusparse.h>

#define CUSPARSE_CHECK(call) \
do { \
    cusparseStatus_t status = call; \
    if (status != CUSPARSE_STATUS_SUCCESS) { \
        fprintf(stderr, "cuSPARSE error at %s:%d: %d\n", \
                __FILE__, __LINE__, status); \
        exit(EXIT_FAILURE); \
    } \
} while(0)

int main() {
    const int num_threads = 4; // Number of threads
    std::thread threads[num_threads]; // Array to hold threads

    for (int i = 0; i < num_threads; ++i) {
        threads[i] = std::thread([i]() {
            // Create cuSPARSE handle
            cusparseHandle_t h;
            CUSPARSE_CHECK(cusparseCreate(&h));

            // Create CUDA stream
            cudaStream_t stream;
            cudaStreamCreate(&stream);

            // Use the cuSPARSE handle and CUDA stream in this thread
            printf("Thread %d: cuSPARSE handle: %p, CUDA stream: %p\n", i, h, stream);

            // Destroy cuSPARSE handle and CUDA stream after use
            CUSPARSE_CHECK(cusparseDestroy(h));
            cudaStreamDestroy(stream);
        });
    }

    // Join all threads
    for (int i = 0; i < num_threads; ++i) {
        threads[i].join();
    }

    return 0;
}
# nvcc -g -G -o t145 t145.cu -lcusparse
# cuda-gdb ./t145
NVIDIA (R) CUDA Debugger
CUDA Toolkit 12.2 release
Portions Copyright (C) 2007-2023 NVIDIA Corporation
GNU gdb (GDB) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./t145...
(cuda-gdb) run
Starting program: /root/bobc/t145
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffe49ff000 (LWP 203487)]
[New Thread 0x7fffe41fe000 (LWP 203488)]
[New Thread 0x7fffe1dbe000 (LWP 203490)]
[New Thread 0x7fffe15bd000 (LWP 203491)]
[New Thread 0x7fffdbfff000 (LWP 203492)]
[Detaching after fork from child process 203493]
[New Thread 0x7fffdb55d000 (LWP 203511)]
[New Thread 0x7fffdad5c000 (LWP 203512)]
Thread 3: cuSPARSE handle: 0x7fffa0000de0, CUDA stream: 0x7fffa00032a0
Thread 0: cuSPARSE handle: 0x7fffdcb72df0, CUDA stream: 0x7fffdcba3d40
Thread 2: cuSPARSE handle: 0x7fffc0000de0, CUDA stream: 0x5555556c7700
Thread 1: cuSPARSE handle: 0x7fffa8000de0, CUDA stream: 0x7fffa80017a0
[Thread 0x7fffe15bd000 (LWP 203491) exited]
[Thread 0x7fffe1dbe000 (LWP 203490) exited]
[Thread 0x7fffe41fe000 (LWP 203488) exited]
[Thread 0x7fffe49ff000 (LWP 203487) exited]
[Thread 0x7fffdbfff000 (LWP 203492) exited]
[Thread 0x7fffdad5c000 (LWP 203512) exited]
[Thread 0x7fffdb55d000 (LWP 203511) exited]
[Inferior 1 (process 203484) exited normally]
(cuda-gdb)

Thank you for testing it out. I cleaned my system and reinstalled cuda-12.3. It works now.