Skip to content

Fix derived array lifetime race#594

Merged
maleadt merged 1 commit into
mainfrom
tb/fix_derive
Jul 22, 2026
Merged

Fix derived array lifetime race#594
maleadt merged 1 commit into
mainfrom
tb/fix_derive

Conversation

@maleadt

@maleadt maleadt commented Jul 22, 2026

Copy link
Copy Markdown
Member

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

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.
@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/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

@maleadt
maleadt merged commit 593c3b1 into main Jul 22, 2026
5 checks passed
@maleadt
maleadt deleted the tb/fix_derive branch July 22, 2026 15:28
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.

Attempt to copy a freed reference when creating a second view into the same array

1 participant