Hi.
I am working on an application that writes to EGL textures from cuda using the graphics interoperability api provided by cudaGL.h
I have ran into a problem where everything seems to work fine at first but when the thread that first called any functions in cudaGL.h is closed, the application segfaults.
After long time tracing the issue i noticed that on first call to any of the interoperability functions, libGLX_nvidia.so gets loaded. If i remove the library file so that it can not be loaded, the bug is fixed.
Everything else in the application uses EGL so to me its weird that those are not used by default as they are already loaded when the interoperability functions are called.
The setup that this has be reproduced with:
- Ubuntu 24
- Nvidia driver: 580.126.09 open source driver
- Cuda version: 13.0
- Nvcc: 13.3
- application linked against cuda driver api
On another setup the bug does not reproduce where the only difference is that it uses closed source Nvidia driver 580.159.03
Am i missing something on the other setup or could this be a driver bug in the opensource drivers?
Here is a minimal test application that reproduces the issue:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.17)
project(test_cuda_ctx CXX)
set(CMAKE_CXX_STANDARD 17)
find_package(CUDAToolkit REQUIRED)
find_package(OpenGL REQUIRED COMPONENTS OpenGL EGL)
add_executable(test_cuda_ctx test_cuda_ctx.cpp)
target_link_libraries(test_cuda_ctx PRIVATE
CUDA::cuda_driver
OpenGL::OpenGL
OpenGL::EGL
)
test_cuda_ctx.cpp
#include <cuda.h>
#include <cudaGL.h>
#include <EGL/egl.h>
#include <GL/gl.h>
#include <cstdio>
#include <thread>
static CUcontext g_ctx = nullptr;
static CUstream stream = nullptr;
static void egl_thread_func()
{
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
EGLint major, minor;
eglInitialize(display, &major, &minor);
printf("EGL version: %d.%d\n", major, minor);
eglBindAPI(EGL_OPENGL_API);
EGLint configAttribs[] = {
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE
};
EGLConfig config;
EGLint numConfigs;
eglChooseConfig(display, configAttribs, &config, 1, &numConfigs);
EGLContext eglCtx = eglCreateContext(display, config, EGL_NO_CONTEXT, nullptr);
EGLint pbufAttribs[] = { EGL_WIDTH, 64, EGL_HEIGHT, 64, EGL_NONE };
EGLSurface surface = eglCreatePbufferSurface(display, config, pbufAttribs);
eglMakeCurrent(display, surface, surface, eglCtx);
printf("EGL context current\n");
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
printf("GL texture created: %u\n", tex);
CUresult res = cuCtxPushCurrent(g_ctx);
printf("cuCtxPushCurrent in EGL thread: %d\n", res);
CUgraphicsResource cudaRes;
res = cuGraphicsGLRegisterImage(&cudaRes, tex, GL_TEXTURE_2D, CU_GRAPHICS_REGISTER_FLAGS_NONE);
printf("cuGraphicsGLRegisterImage: %d\n", res);
res = cuGraphicsUnregisterResource(cudaRes);
printf("cuGraphicsUnregisterResource: %d\n", res);
cuCtxPopCurrent(nullptr);
glDeleteTextures(1, &tex);
eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(display, eglCtx);
eglDestroySurface(display, surface);
eglTerminate(display);
printf("EGL thread done\n");
}
int main() {
cuInit(0);
CUdevice dev;
cuDeviceGet(&dev, 0);
cuCtxCreate(&g_ctx, 0, 0, dev);
cuStreamCreate(&stream, 0);
printf("Context created: %p\n", (void*)g_ctx);
std::thread eglThread(egl_thread_func);
eglThread.join();
printf("EGL thread joined\n");
cuCtxDestroy(g_ctx);
return 0;
}