Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 79 additions & 4 deletions src/Apache.Arrow/C/CArrowArrayExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ public static unsafe void ExportArray(IArrowArray array, CArrowArray* cArray)
try
{
ConvertArray(allocationOwner, array.Data, cArray);
SetReleaseOrUndo(allocationOwner, cArray);
allocationOwner = null;
}
catch
{
// The struct may point to memory that is freed below: leave it released.
*cArray = default;
throw;
}
finally
{
allocationOwner?.Dispose();
Expand Down Expand Up @@ -109,8 +116,15 @@ public static unsafe void ExportRecordBatch(RecordBatch batch, CArrowArray* cArr
try
{
ConvertRecordBatch(allocationOwner, batch, cArray);
SetReleaseOrUndo(allocationOwner, cArray);
allocationOwner = null;
}
catch
{
// The struct may point to memory that is freed below: leave it released.
*cArray = default;
throw;
}
finally
{
allocationOwner?.Dispose();
Expand All @@ -122,8 +136,9 @@ private unsafe static void ConvertArray(ExportedAllocationOwner sharedOwner, Arr
cArray->length = array.Length;
cArray->offset = array.Offset;
cArray->null_count = array.NullCount; // The C Data interface allows the null count to be -1
cArray->release = ReleaseArrayPtr;
cArray->private_data = MakePrivateData(sharedOwner);
// Set by SetRelease once the whole array has been converted
cArray->release = default;
cArray->private_data = null;

cArray->n_buffers = array.Buffers?.Length ?? 0;
cArray->buffers = null;
Expand Down Expand Up @@ -186,8 +201,9 @@ private unsafe static void ConvertRecordBatch(ExportedAllocationOwner sharedOwne
cArray->length = batch.Length;
cArray->offset = 0;
cArray->null_count = 0;
cArray->release = ReleaseArrayPtr;
cArray->private_data = MakePrivateData(sharedOwner);
// Set by SetRelease once the whole batch has been converted
cArray->release = default;
cArray->private_data = null;

cArray->n_buffers = 1;
cArray->buffers = (byte**)sharedOwner.Allocate(IntPtr.Size);
Expand Down Expand Up @@ -231,6 +247,65 @@ private unsafe static void ReleaseArray(CArrowArray* cArray)
cArray->release = default;
}

/// <summary>
/// Sets the release callbacks with <see cref="SetRelease"/>, and if that fails partway,
/// frees the handles it made before the caller disposes the owner, so they don't keep it
/// alive.
/// </summary>
private unsafe static void SetReleaseOrUndo(ExportedAllocationOwner sharedOwner, CArrowArray* cArray)
{
try
{
SetRelease(sharedOwner, cArray);
}
catch
{
UndoSetRelease(cArray);
throw;
}
}

/// <summary>
/// Frees the handles that <see cref="SetRelease"/> made and clears the release callbacks.
/// The owner's references aren't released one by one: the caller disposes it.
/// </summary>
private unsafe static void UndoSetRelease(CArrowArray* cArray)
{
for (long i = 0; i < cArray->n_children; i++)
{
UndoSetRelease(cArray->children[i]);
}
if (cArray->dictionary != null)
{
UndoSetRelease(cArray->dictionary);
}
if (cArray->private_data != null)
{
GCHandle.FromIntPtr((IntPtr)cArray->private_data).Free();
cArray->private_data = null;
}
cArray->release = default;
}

/// <summary>
/// Sets the release callback of <paramref name="cArray"/> and everything under it, once
/// conversion has succeeded. Until then nothing refers to the owner, so a failed export
/// leaves nothing for a consumer to release.
/// </summary>
private unsafe static void SetRelease(ExportedAllocationOwner sharedOwner, CArrowArray* cArray)
{
for (long i = 0; i < cArray->n_children; i++)
{
SetRelease(sharedOwner, cArray->children[i]);
}
if (cArray->dictionary != null)
{
SetRelease(sharedOwner, cArray->dictionary);
}
cArray->private_data = MakePrivateData(sharedOwner);
cArray->release = ReleaseArrayPtr;
}

private unsafe static CArrowArray* MakeArray(ExportedAllocationOwner sharedOwner)
{
var array = (CArrowArray*)sharedOwner.Allocate(sizeof(CArrowArray));
Expand Down
65 changes: 65 additions & 0 deletions test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,71 @@ public unsafe void ExportArrayPreservesSource()
array.Dispose();
}

// A struct whose first child exports and whose second has been disposed, so export fails
// after part of the array has been converted.
private static StructArray StructWithADisposedSecondChild()
{
Int32Array first = new Int32Array.Builder().AppendRange(new[] { 1, 2, 3 }).Build();
Int32Array second = new Int32Array.Builder().AppendRange(new[] { 4, 5, 6 }).Build();
var type = new StructType(new[]
{
new Field("first", Int32Type.Default, true),
new Field("second", Int32Type.Default, true),
});
var array = new StructArray(type, 3, new IArrowArray[] { first, second }, ArrowBuffer.Empty, 0);
second.Dispose();
return array;
}

[Fact]
public unsafe void FailedArrayExportLeavesTheArrayReleased()
{
using StructArray array = StructWithADisposedSecondChild();
CArrowArray* cArray = CArrowArray.Create();
try
{
Assert.Throws<ObjectDisposedException>(() => CArrowArrayExporter.ExportArray(array, cArray));

// Nothing is left for a consumer to release, or to read
Assert.True(cArray->release == default);
Assert.True(cArray->children == null);
Assert.True(cArray->private_data == null);
}
finally
{
CArrowArray.Free(cArray);
}
}

[Fact]
public unsafe void FailedRecordBatchExportLeavesTheArrayReleased()
{
Int32Array first = new Int32Array.Builder().AppendRange(new[] { 1, 2, 3 }).Build();
Int32Array second = new Int32Array.Builder().AppendRange(new[] { 4, 5, 6 }).Build();
var schema = new Schema.Builder()
.Field(f => f.Name("first").DataType(Int32Type.Default))
.Field(f => f.Name("second").DataType(Int32Type.Default))
.Build();
var batch = new RecordBatch(schema, new IArrowArray[] { first, second }, 3);
second.Dispose();

CArrowArray* cArray = CArrowArray.Create();
try
{
Assert.Throws<ObjectDisposedException>(
() => CArrowArrayExporter.ExportRecordBatch(batch, cArray));

Assert.True(cArray->release == default);
Assert.True(cArray->children == null);
Assert.True(cArray->private_data == null);
}
finally
{
CArrowArray.Free(cArray);
batch.Dispose();
}
}

[Fact]
public unsafe void ExportRecordBatch_LargerThan2GB_Succeeds()
{
Expand Down
Loading