Skip to content

Collecting a context together with its memory segfaults #714

Closed

Description

@maleadt

Every Level Zero object is destroyed by its own finalizer, and Julia runs the finalizers of objects that become unreachable in the same collection in no particular order. A ZeContext can thus be destroyed before memory allocated in it, and freeing that memory afterwards crashes in NEO:

using oneAPI, oneAPI.oneL0

function work()
    ctx = ZeContext(oneAPI.driver())
    for _ in 1:3; GC.gc(true); end   # the context is old by the time the array is allocated
    old = context()
    context!(ctx)
    a = oneArray{Float32}(undef, 1_000_000)
    context!(old)
    return
end
work()
GC.gc(true)
[19895] signal 11 (1): Segmentation fault
_ZN2L07Context7freeMemEPKvb at libze_intel_gpu.so.1
zeMemFree at ...

This fails every time on an Iris Xe with NEO 26.35. Without the collections before the allocation the finalizers happen to run in a working order. A context in which a kernel was launched, or small (pooled) arrays were allocated, stays reachable from oneAPI.jl's caches and is never collected, which is probably why this hasn't come up.

Forcing other parent-before-child orders (a queue before its fences, an event pool before its events, a module before its kernels, a command list while it is executing) does not crash on NEO, though the Level Zero spec doesn't allow them either. The context is the one that matters: destroying it frees the allocations made in it.

CUDA.jl avoids this by not destroying contexts from a finalizer at all (CuContext has no finalizer, see also JuliaGPU/CUDA.jl#3404 for releases after an explicit destroy).

Activity

  1. added theissue type on Oct 19, 2026
  2. added
    runtimeHost runtime: devices, queues/streams, synchronization, memory management, multi-device, profiling.
    and removed on Oct 19, 2026
  3. added 2 commits that reference this issue on Oct 19, 2026
    7361f35
    f8b88ff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    runtimeHost runtime: devices, queues/streams, synchronization, memory management, multi-device, profiling.

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions