Skip to content

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

@khalatepradnya

Description

@khalatepradnya

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)

Metadata

Metadata

Assignees

Labels

stale-notifiedStale notification has already fired for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions