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


##########
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:
   Thanks — the handle now keeps the slice length in a readonly field, and 
`Retain` uses it. Added `RetainRacingWithDisposeKeepsTheLength`, which races 
`Retain` against `Dispose` while another reference keeps the memory alive.



##########
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:
   Thanks — the loop now counts failures and asserts only after the workers 
have joined, so `IsBackground` is no longer needed.



-- 
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