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


##########
src/Apache.Arrow/Arrays/ArrayDataConcatenator.cs:
##########
@@ -1596,12 +1596,12 @@ private IMemoryOwner<byte> AllocateBitmap(int 
bitLength, out Span<byte> destinat
                 return owner;
             }
 
-            private static ArrowBuffer BuildBuffer(IMemoryOwner<byte> owner, 
int byteLength)
+            private ArrowBuffer BuildBuffer(IMemoryOwner<byte> owner, int 
byteLength)
             {
                 // Allocators don't have to return zeroed memory, and may 
return more than was requested,
                 // so clear everything past the written bytes explicitly
                 owner.Memory.Span.Slice(byteLength).Clear();
-                return new ArrowBuffer(owner);
+                return new ArrowBuffer(owner, (_allocator ?? 
MemoryAllocator.Default.Value).Lifetime);

Review Comment:
   With a non-finalizing `CallerOwned` allocator, `Visit(FixedWidthType)` leaks 
its completed validity buffer if copying a too-short value buffer throws; only 
the value allocation is disposed. Dispose previously built buffers when 
subsequent concatenation steps fail. The same issue occurs in 
`ConcatenateVariableBinaryArrayData` and 
`ConcatenateLargeVariableBinaryArrayData`.



##########
src/Apache.Arrow/Ipc/ArrowStreamWriter.cs:
##########
@@ -425,7 +425,7 @@ private ArrowBuffer GetZeroBasedValueOffsets(ArrowBuffer 
valueOffsetsBuffer, int
                         newValueOffsets[i] = originalOffsets[i] - firstOffset;
                     }
 
-                    return new ArrowBuffer(newValueOffsetsBuffer);
+                    return new ArrowBuffer(newValueOffsetsBuffer, 
_allocator.Lifetime);

Review Comment:
   With a non-finalizing `CallerOwned` allocator, temporary offset buffers leak 
if writing fails or is cancelled before their write-loop entries are reached, 
such as during batch metadata output. 
`ArrowRecordBatchFlatBufferBuilder.Dispose` only cleans `_deferredDisposals`; 
make it also release remaining locally owned buffers, and ensure cleanup runs 
when preparation fails. The same issue affects the offset buffers created at 
lines 460, 529 and 580.



##########
src/Apache.Arrow/Ipc/DecompressingBufferCreator.cs:
##########
@@ -58,7 +58,7 @@ public ArrowBuffer CreateBuffer(ReadOnlyMemory<byte> source)
                 throw new Exception($"Expected to decompress 
{uncompressedLength} bytes, but got {decompressedLength} bytes");
             }
 
-            return new ArrowBuffer(outputData);
+            return new ArrowBuffer(outputData, _allocator.Lifetime);

Review Comment:
   With a non-finalizing `CallerOwned` allocator, a malformed later buffer or 
field causes the reader to leak earlier decompressed allocations without 
returning anything the caller can dispose. `LoadField` and `BuildArrays` 
abandon partial results, while this creator's `Dispose` only releases the 
codec. Add exception cleanup for partially constructed buffers, children and 
arrays.



##########
src/Apache.Arrow/Memory/AllocationLifetime.cs:
##########
@@ -0,0 +1,98 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+namespace Apache.Arrow.Memory
+{
+    /// <summary>
+    /// Describes what happens to a block of memory if the <see 
cref="ArrowBuffer"/> that owns it
+    /// is never disposed.
+    /// </summary>
+    /// <remarks>
+    /// <para>
+    /// The lifetime is stated by whoever creates the memory: by a <see 
cref="MemoryAllocator"/>
+    /// through <see cref="MemoryAllocator.Lifetime"/>, or by the caller when 
wrapping memory with
+    /// <see cref="ArrowBuffer(System.Buffers.IMemoryOwner{byte}, 
AllocationLifetime)"/>.
+    /// </para>
+    /// <para>
+    /// It describes a correctness contract, not a performance strategy. 
Disposing a buffer is
+    /// always at least as fast as not disposing it, whatever its lifetime.
+    /// </para>
+    /// <para>
+    /// Whatever the lifetime, a span over a buffer must not be used after the 
buffer is disposed:
+    /// disposal may free the memory or make it available for reuse. The 
values here describe only
+    /// what happens to memory whose buffer is <em>never</em> disposed.
+    /// </para>
+    /// <para>
+    /// Only <see cref="Finalized"/> causes Arrow to release memory from a 
finalizer. With the other
+    /// lifetimes, Arrow releases memory only when the last buffer using it is 
disposed.

Review Comment:
   Please clarify that these lifetimes do not disable the supplied owner's own 
finalizer. For example, wrapping an allocation from `NativeMemoryAllocator` 
with `CallerOwned` still leaves `NativeMemoryManager`'s finalizer active, so GC 
can invalidate a live span despite the guarantee below. The non-finalized 
lifetimes require the owner and its backing allocation not to free or recycle 
memory independently through finalization.



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