Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/tls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ function priority!(f::Function, p::Symbol)
end
end

@inline function prepare_state(state = task_local_state!())
@inline function prepare_state(state = nothing)
# Finalizers run on whichever task GC interrupted and pick their own context
# with `HIP.context!`, so leave that task's state and sticky error alone.
GC.in_finalizer() && return
isnothing(state) && (state = task_local_state!())
HIP.clear_last_error() # Drain any sticky HIP error left by a prior kernel failure
hip_ctx = Ref{HIP.hipCtx_t}()
HIP.hipCtxGetCurrent(hip_ctx)
Expand Down
13 changes: 13 additions & 0 deletions test/core/tls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ using AMDGPU: ROCArray, HIPDevice, HIPStream
@test AMDGPU.device(x) ≡ d1
end

@testset "GC finalizers leave task-local state alone" begin
# GC runs finalizers on whichever task it interrupts. Freeing an array makes
# HIP calls, which must not create state on that task.
weak_array() = WeakRef(ROCArray{Float32}(undef, 16))
w = weak_array()
no_state, collected = fetch(@async begin
GC.gc(true)
(AMDGPU.task_local_state() ≡ nothing, w.value ≡ nothing)
end)
@test collected
@test no_state
end

@testset "Stream" begin
s1 = @inferred AMDGPU.stream()
@test s1 isa AMDGPU.HIPStream
Expand Down
Loading