Skip to content

Thread safety fixes - #611

Merged
maleadt merged 3 commits into
mainfrom
tb/threadsafety
Aug 12, 2026
Merged

Thread safety fixes#611
maleadt merged 3 commits into
mainfrom
tb/threadsafety

Conversation

@maleadt

@maleadt maleadt commented Aug 12, 2026

Copy link
Copy Markdown
Member

Counterpart of JuliaGPU/OpenCL.jl#472

ZeKernel argument slots and group size are mutable state shared by cached HostKernel instances. Concurrent launches could interleave their zeKernelSetArgumentValue and zeKernelSetGroupSize calls, causing either command list to capture mixed arguments or the other launch’s group size. Give each kernel its own lock and hold it continuously from the first mutation through append_launch!, while leaving independent kernel handles concurrent.
The global context and compiler-configuration dictionaries used unsynchronized get! and check-then-insert paths. Concurrent cold calls could mutate a Dict simultaneously or construct duplicate handles and configurations. Protect each cache with a dedicated lock and keep lookup, construction, and insertion in one atomic get! transaction.
The process-wide device cache returned the same mutable RNG and state array to tasks that use independent queues. Concurrent seed! and generation calls could therefore update shared device state without queue ordering. Move the per-device RNG dictionary into task-local storage so repeated calls within one task reuse state while separate tasks receive independent RNGs.
@github-actions

Copy link
Copy Markdown
Contributor

Your PR requires formatting changes to meet the project's style guidelines.
Please consider running Runic (git runic main) to apply these changes.

Click here to view the suggested changes.
diff --git a/src/compiler/compilation.jl b/src/compiler/compilation.jl
index a8c117a..5f46fff 100644
--- a/src/compiler/compilation.jl
+++ b/src/compiler/compilation.jl
@@ -305,7 +305,7 @@ const _compiler_configs = Dict{UInt, oneAPICompilerConfig}()
 const compiler_config_lock = ReentrantLock()
 function compiler_config(dev; kwargs...)
     h = hash(dev.driver, hash(dev, hash(kwargs)))
-    Base.@lock compiler_config_lock begin
+    return Base.@lock compiler_config_lock begin
         get!(_compiler_configs, h) do
             _compiler_config(dev; kwargs...)
         end
diff --git a/src/compiler/execution.jl b/src/compiler/execution.jl
index 7cb37cf..18f9114 100644
--- a/src/compiler/execution.jl
+++ b/src/compiler/execution.jl
@@ -323,7 +323,7 @@ const _kernel_instances = Dict{UInt, Any}()
 
 @inline function onecall(kernel::ZeKernel, tt, args...; groups::ZeDim=1, items::ZeDim=1,
                          queue::ZeCommandQueue=global_queue(context(), device()))
-    Base.@lock kernel begin
+    return Base.@lock kernel begin
         for (i, arg) in enumerate(args)
             oneL0.arguments(kernel)[i] = arg
         end
diff --git a/src/gpuarrays.jl b/src/gpuarrays.jl
index a0a3816..2a58f72 100644
--- a/src/gpuarrays.jl
+++ b/src/gpuarrays.jl
@@ -3,9 +3,9 @@
 function GPUArrays.default_rng(::Type{<:oneArray})
     dev = device()
     rngs = get!(task_local_storage(), :oneAPI_GLOBAL_RNGs) do
-        Dict{ZeDevice,GPUArrays.RNG}()
+        Dict{ZeDevice, GPUArrays.RNG}()
     end
-    get!(rngs, dev) do
+    return get!(rngs, dev) do
         N = oneL0.compute_properties(dev).maxTotalGroupSize
         state = oneArray{NTuple{4, UInt32}}(undef, N)
         rng = GPUArrays.RNG(state)

@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.51%. Comparing base (315b088) to head (1cab516).

Files with missing lines Patch % Lines
lib/level-zero/module.jl 75.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #611   +/-   ##
=======================================
  Coverage   78.51%   78.51%           
=======================================
  Files          50       50           
  Lines        3490     3496    +6     
=======================================
+ Hits         2740     2745    +5     
- Misses        750      751    +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@maleadt
maleadt merged commit 3cc6293 into main Aug 12, 2026
2 of 4 checks passed
@maleadt
maleadt deleted the tb/threadsafety branch August 12, 2026 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant