Fix derived array lifetime race#594
Merged
Merged
Conversation
Keep the parent oneArray alive while deriving views so its finalizer cannot mark the shared GPUArrays DataRef as freed before the new array retains it. Add a GC-stress regression for nested ephemeral views, which frequently triggered "Attempt to copy a freed reference" in AcceleratedKernels.
Contributor
|
Your PR requires formatting changes to meet the project's style guidelines. Click here to view the suggested changes.diff --git a/src/array.jl b/src/array.jl
index 3fd539b..e0f5ec1 100644
--- a/src/array.jl
+++ b/src/array.jl
@@ -570,10 +570,10 @@ function GPUArrays.derive(::Type{T}, a::oneArray, dims::Dims{N}, offset::Int) wh
Base.elsize(a) == 0 || error("Cannot derive a singleton array from non-singleton inputs")
end
offset = a.offset + offset * sizeof(T)
- # The derived array constructor copies `a.data`, but merely loading that field does not
- # keep `a` alive. Without this preserve, `a` may be finalized between the field load and
- # the DataRef copy, causing its finalizer to mark the reference as freed.
- GC.@preserve a oneArray{T,N}(a.data, dims; a.maxsize, offset)
+ # The derived array constructor copies `a.data`, but merely loading that field does not
+ # keep `a` alive. Without this preserve, `a` may be finalized between the field load and
+ # the DataRef copy, causing its finalizer to mark the reference as freed.
+ return GC.@preserve a oneArray{T, N}(a.data, dims; a.maxsize, offset)
end
diff --git a/test/array.jl b/test/array.jl
index 1ca6645..1a462f2 100644
--- a/test/array.jl
+++ b/test/array.jl
@@ -44,38 +44,38 @@ end
end
@testset "derived array lifetime" begin
- parent = oneArray{UInt8}(undef, 1)
-
- # Construct through an ephemeral derived array, whose finalizer shares the same
- # reference-counted allocation with the returned view.
- function ephemeral_derived_view(parent)
- intermediate = @view parent[:]
- @view intermediate[:]
- end
-
- derived = ephemeral_derived_view(parent)
- @test derived isa oneArray{UInt8}
-
- # Exercise finalization while deriving. Without preserving the immediate parent in
- # GPUArrays.derive, its finalizer can mark the DataRef as freed before it is copied.
- if Threads.nthreads() > 1
- stop_gc = Threads.Atomic{Bool}(false)
- gc_task = Threads.@spawn while !stop_gc[]
- GC.gc(false)
- yield()
+ parent = oneArray{UInt8}(undef, 1)
+
+ # Construct through an ephemeral derived array, whose finalizer shares the same
+ # reference-counted allocation with the returned view.
+ function ephemeral_derived_view(parent)
+ intermediate = @view parent[:]
+ @view intermediate[:]
end
- try
- Threads.@threads for _ in 1:min(Threads.nthreads(), 4)
- for _ in 1:100
- a = ephemeral_derived_view(parent)
- oneAPI.unsafe_free!(a)
+
+ derived = ephemeral_derived_view(parent)
+ @test derived isa oneArray{UInt8}
+
+ # Exercise finalization while deriving. Without preserving the immediate parent in
+ # GPUArrays.derive, its finalizer can mark the DataRef as freed before it is copied.
+ if Threads.nthreads() > 1
+ stop_gc = Threads.Atomic{Bool}(false)
+ gc_task = Threads.@spawn while !stop_gc[]
+ GC.gc(false)
+ yield()
+ end
+ try
+ Threads.@threads for _ in 1:min(Threads.nthreads(), 4)
+ for _ in 1:100
+ a = ephemeral_derived_view(parent)
+ oneAPI.unsafe_free!(a)
+ end
+ end
+ finally
+ stop_gc[] = true
+ wait(gc_task)
end
- end
- finally
- stop_gc[] = true
- wait(gc_task)
end
- end
end
@testset "reinterpret of view with non-aligned offset" begin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Keep the parent oneArray alive while deriving views so its finalizer cannot mark the shared GPUArrays DataRef as freed before the new array retains it. Add a GC-stress regression for nested ephemeral views, which frequently triggered "Attempt to copy a freed reference" in AcceleratedKernels.
Fixes #484