NVIDIA / NVIDIA/cuda-quantum

`make_kernel` segfaults when called concurrently from async dispatch loop; needs guard or thread-safety

Open
#4,359 0 comments 0 reactions 1 assignee View on GitHub

@sacpis is already working on this.

Since Apr 23, 2026.

stale-notified
Dominant language
C++
Stars
1.1k
Forks
455
Avg merge
1d 22h
Merged PRs (30d)
165

Description

Context

Spun off from #2821. When cudaq.make_kernel() is called inside a loop that also dispatches observe_async / sample_async, the MLIR context is accessed from multiple threads without synchronization, producing a bare segfault with no actionable error message.

The workaround (build the kernel before the loop) was confirmed by the reporter, but the failure mode — a silent segfault inside kernel_builder.py_func_ops_ext.py — is hostile to users.

Reproducer
import cudaq

cudaq.set_target("nvidia", option="mqpu")

def build_kernel():
    k, params = cudaq.make_kernel(list[float])
    qubits = k.qalloc(5)
    for i in range(5):
        k.rx(params[i], qubits[i])
    return k

# This segfaults because make_kernel is not thread-safe
for i in range(100):
    ker = build_kernel()  # <-- called inside async dispatch
    cudaq.sample_async(ker, [0.1]*5, qpu_id=i % 4)
Expected behavior

Either:

  1. Preferred: make_kernel() is thread-safe and works correctly when called concurrently, or
  2. Minimum: A clear RuntimeError is raised (e.g., "Kernel construction is not thread-safe; build kernels before dispatching async work") instead of a segfault.
Additionally

The thread-safety constraint should be documented in the multi-GPU workflows guide and in the make_kernel API docstring.

Environment

Originally reported on CUDA-Q 0.10.0
Workaround confirmed on 0.10.0+

Related

#2821 (original report, being closed with workaround)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.