Copilot commented on code in PR #538:
URL: https://github.com/apache/arrow-dotnet/pull/538#discussion_r4803078962


##########
test/Apache.Arrow.Tests/ArrowBufferTests.cs:
##########
@@ -286,6 +286,75 @@ public void ConcurrentRetainAndDispose()
                 Assert.Equal(0, errors);
                 original.Dispose();
             }
+
+            [Fact]
+            public void ConcurrentDisposeOfCopiesReleasesOneReference()
+            {
+                // Copies of an ArrowBuffer share one reference, so disposing 
several copies at once
+                // must release it only once, leaving memory that another 
buffer retains intact.
+                const int threadCount = 4;
+                const int iterations = 2000;
+                ArrowBuffer shared = default;
+                int errors = 0;
+
+                using var barrier = new Barrier(threadCount + 1);
+                var threads = new Thread[threadCount];
+                for (int t = 0; t < threadCount; t++)
+                {
+                    threads[t] = new Thread(() =>
+                    {
+                        for (int i = 0; i < iterations; i++)
+                        {
+                            barrier.SignalAndWait();
+                            try
+                            {
+                                ArrowBuffer copy = shared;
+                                copy.Dispose();
+                            }
+                            catch
+                            {
+                                Interlocked.Increment(ref errors);
+                            }
+                            barrier.SignalAndWait();
+                        }
+                    })
+                    {
+                        // Don't let a failed assertion leave the process 
waiting at the barrier.
+                        IsBackground = true,
+                    };
+                    threads[t].Start();
+                }
+
+                for (int i = 0; i < iterations; i++)
+                {
+                    var owner = new CountingMemoryOwner(64);
+                    shared = new ArrowBuffer(owner, 
AllocationLifetime.CallerOwned);
+                    ArrowBuffer retained = shared.Retain();
+
+                    barrier.SignalAndWait();
+                    barrier.SignalAndWait();
+
+                    Assert.Equal(0, owner.DisposeCount);
+                    retained.Dispose();
+                    Assert.Equal(1, owner.DisposeCount);

Review Comment:
   If either assertion fails before the final iteration, the joins are skipped 
and the barrier is disposed while workers may still call `SignalAndWait()` 
outside the catch block. `IsBackground` does not prevent an unhandled worker 
exception from terminating the test host. Record failures in `errors` and 
assert only after all workers have joined.



##########
src/Apache.Arrow/Memory/SharedMemoryHandle.cs:
##########
@@ -61,11 +62,13 @@ public Memory<byte> Memory
 
         public SharedMemoryHandle Retain()
         {
-            if (_owner == null)
+            // Read the owner once, so a concurrent release can't make it null 
after the check.
+            SharedMemoryOwner owner = _owner;
+            if (owner == null)
             {
                 ThrowDisposed();
             }
-            return _owner.Retain(_start, _memory.Length);
+            return owner.Retain(_start, _memory.Length);

Review Comment:
   Capturing the owner does not preserve the retained range: after line 66, 
another thread can dispose this handle and clear `_memory`. If another retained 
buffer keeps the owner alive, `owner.Retain(_start, _memory.Length)` then 
succeeds with length zero, silently returning an empty buffer instead of the 
original contents or throwing. Preserve the slice length in an immutable field 
and use it here, and cover retention racing with source disposal while another 
reference remains alive.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to