The simulator we are currently building on JAX+XLA deadlocks in cuStreamSynchronize after cuGraphLaunch under very specific conditions when using XLA’s concurrent scheduler. After a week of painful debugging I’ve managed to reproduce the issue in a standalone CUDA program.
I apologize in advance for the quality, the file is generated by an LLM after bisecting the (much larger) graph generated by our production code base.
#include <atomic>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cuda_runtime.h>
#include <thread>
#include <vector>
#define CK(c) \
do { \
cudaError_t e = (c); \
if (e != cudaSuccess) { \
fprintf(stderr, " err L%d: %s -> %s\n", __LINE__, #c, \
cudaGetErrorString(e)); \
return -1; \
} \
} while (0)
// Sets this loop's trip-count buffer to a fixed N and enters (handle=1). No
// reads.
__global__ void arm(int *ctr, int N, cudaGraphConditionalHandle h) {
*ctr = N;
cudaGraphSetConditional(h, 1);
}
// One iteration: decrement the (initialized) trip-count, set handle for next
// check.
__global__ void tick(int *ctr, cudaGraphConditionalHandle h) {
int v = --(*ctr);
cudaGraphSetConditional(h, v > 0 ? 1 : 0);
}
static int NMEM = 1, NCOND = 2;
static int watchdog(cudaGraphExec_t exec, double secs) {
cudaStream_t s;
if (cudaStreamCreate(&s) != cudaSuccess)
return -1;
std::atomic<int> done{0};
std::thread w([&] {
if (cudaGraphLaunch(exec, s) != cudaSuccess) {
done.store(-1);
return;
}
done.store(cudaStreamSynchronize(s) == cudaSuccess ? 1 : -1);
});
auto t0 = std::chrono::steady_clock::now();
while (done.load() == 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(20));
if (std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
.count() > secs) {
fprintf(stderr, " >>> HANG (%.1fs) <<<\n", secs);
w.detach();
return 1;
}
}
w.join();
return done.load() == 1 ? 0 : -1;
}
// Add a nested WHILE(N iters) into `g` after `arm_dep`; return its conditional
// node.
static int addNestedWhile(cudaGraph_t g, cudaGraphNode_t armDep, int N,
cudaGraphNode_t *out) {
int *ctr;
CK(cudaMalloc(&ctr, sizeof(int)));
CK(cudaMemset(ctr, 0, sizeof(int))); // explicit init
cudaGraphConditionalHandle h;
CK(cudaGraphConditionalHandleCreate(&h, g, 1, cudaGraphCondAssignDefault));
cudaGraphNode_t armN;
int n = N;
{
void *args[] = {&ctr, &n, &h};
cudaKernelNodeParams kp = {};
kp.func = (void *)arm;
kp.gridDim = dim3(1);
kp.blockDim = dim3(1);
kp.kernelParams = args;
cudaGraphNode_t deps[1] = {armDep};
int nd = armDep ? 1 : 0;
CK(cudaGraphAddKernelNode(&armN, g, armDep ? deps : nullptr, nd, &kp));
}
cudaGraphNode_t cond;
cudaGraph_t body;
{
cudaGraphNodeParams np = {};
np.type = cudaGraphNodeTypeConditional;
np.conditional.handle = h;
np.conditional.type = cudaGraphCondTypeWhile;
np.conditional.size = 1;
cudaGraphNode_t d[1] = {armN};
CK(cudaGraphAddNode(&cond, g, d, nullptr, 1, &np));
body = np.conditional.phGraph_out[0];
}
{
void *args[] = {&ctr, &h};
cudaKernelNodeParams kp = {};
kp.func = (void *)tick;
kp.gridDim = dim3(1);
kp.blockDim = dim3(1);
kp.kernelParams = args;
cudaGraphNode_t tk;
CK(cudaGraphAddKernelNode(&tk, body, nullptr, 0, &kp));
}
*out = cond;
return 0;
}
static int run() {
fprintf(stderr, "\n=== NMEM=%d NCOND=%d ===\n", NMEM, NCOND);
int *src;
CK(cudaMalloc(&src, sizeof(int)));
CK(cudaMemset(src, 0, sizeof(int))); // memcpy source, init
int *octr;
CK(cudaMalloc(&octr, sizeof(int)));
CK(cudaMemset(octr, 0, sizeof(int))); // outer trip-count, init
cudaGraph_t top;
CK(cudaGraphCreate(&top, 0));
cudaGraphConditionalHandle oh;
CK(cudaGraphConditionalHandleCreate(&oh, top, 1,
cudaGraphCondAssignDefault));
cudaGraphNode_t oarm;
int three = getenv("OUTER") ? atoi(getenv("OUTER")) : 3;
{
void *args[] = {&octr, &three, &oh};
cudaKernelNodeParams kp = {};
kp.func = (void *)arm;
kp.gridDim = dim3(1);
kp.blockDim = dim3(1);
kp.kernelParams = args;
CK(cudaGraphAddKernelNode(&oarm, top, nullptr, 0, &kp));
}
cudaGraphNode_t ocond;
cudaGraph_t body;
{
cudaGraphNodeParams np = {};
np.type = cudaGraphNodeTypeConditional;
np.conditional.handle = oh;
np.conditional.type = cudaGraphCondTypeWhile;
np.conditional.size = 1;
cudaGraphNode_t d[1] = {oarm};
CK(cudaGraphAddNode(&ocond, top, d, nullptr, 1, &np));
body = np.conditional.phGraph_out[0];
}
// body band: NMEM concurrent MEMCPYs + NCOND concurrent nested WHILEs,
// edgeless, all fanning into the outer predicate (tick on octr).
std::vector<cudaGraphNode_t> leaves, memNodes;
for (int i = 0; i < NMEM; ++i) {
int *dst;
CK(cudaMalloc(&dst, sizeof(int)));
CK(cudaMemset(dst, 0, sizeof(int)));
cudaGraphNode_t m;
CK(cudaGraphAddMemcpyNode1D(&m, body, nullptr, 0, dst, src, sizeof(int),
cudaMemcpyDeviceToDevice));
leaves.push_back(m);
memNodes.push_back(m);
}
// SERIAL: 1 = memcpy->each conditional (conds still concurrent w/ each
// other);
// 2 = full chain memcpy->cond0->cond1->... (nothing concurrent).
// Same nodes/buffers/handles/loops — only ordering edges differ.
int serialize = getenv("SERIAL") ? atoi(getenv("SERIAL")) : 0;
cudaGraphNode_t prev = (!memNodes.empty()) ? memNodes[0] : nullptr;
for (int j = 0; j < NCOND; ++j) {
cudaGraphNode_t cond;
cudaGraphNode_t dep =
serialize == 2
? prev
: (serialize == 1 ? (memNodes.empty() ? nullptr : memNodes[0])
: nullptr);
if (addNestedWhile(body, dep, 2, &cond) != 0)
return -1;
leaves.push_back(cond);
prev = cond;
}
{
void *args[] = {&octr, &oh};
cudaKernelNodeParams kp = {};
kp.func = (void *)tick;
kp.gridDim = dim3(1);
kp.blockDim = dim3(1);
kp.kernelParams = args;
cudaGraphNode_t otick;
CK(cudaGraphAddKernelNode(&otick, body, leaves.data(),
(int)leaves.size(), &kp));
}
cudaGraphExec_t exec = nullptr;
CK(cudaGraphInstantiateWithFlags(&exec, top, 0));
double secs = 8.0; // synchronize timeout before calling a hang
int r = watchdog(exec, secs);
fprintf(stderr, " result: %s\n",
r == 0 ? "OK" : (r == 1 ? "HANG" : "ERROR"));
return r;
}
int main() {
CK(cudaSetDevice(0));
cudaDeviceProp p;
cudaGetDeviceProperties(&p, 0);
if (getenv("NMEM"))
NMEM = atoi(getenv("NMEM"));
if (getenv("NCOND"))
NCOND = atoi(getenv("NCOND"));
fprintf(stderr, "device: %s cuda-rt %d\n", p.name, CUDART_VERSION);
run();
fprintf(stderr, "done.\n");
return 0;
}
cuGraphLaunch runs without issue but cuStreamSynchronize never returns. Reproduced on three machines:
Driver Version: 580.82.07
CUDA Version: 13.0
NVIDIA GeForce RTX 5090
Driver Version: 595.84
CUDA Version: 13.2
NVIDIA GeForce RTX 4090
Driver Version: 595.71.05
CUDA Version: 13.2
NVIDIA GH200 144G HBM3e
Utilization is pretty much 0% while the program hangs.
Running with NMEM=0, NCOND=1, OUTER=1 or SERIAL=2 makes the program pass.
Here is the failing graph dot dump:
