Repository navigation
fix: Dispose record batches exported through the C stream interface - #483
Merged
adamreeve merged 2 commits intoOct 22, 2026
Merged
Conversation
CArrowArrayStreamExporter and CArrowDeviceArrayStreamExporter read each batch from the managed stream, export it and then drop it without disposing it. The exported array holds its own references to the buffers, so when the consumer releases it only those references go away; the batch's references are left for the finalizer, which in a NativeAOT library may never run. The exported array now takes ownership of the batch: the ExportedAllocationOwner disposes it when the array is released, after it has unpinned the exported buffers. Exporting a batch directly with ExportRecordBatch is unchanged and still leaves the batch to the caller. Closes apache#464.
adamreeve
reviewed
Oct 21, 2026
adamreeve
left a comment
Contributor
There was a problem hiding this comment.
This looks good to me thanks @ump45nose, I just have one testing suggestion.
adamreeve
approved these changes
Oct 22, 2026
adamreeve
left a comment
Contributor
There was a problem hiding this comment.
This looks good to me thanks @ump45nose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's Changed
CArrowArrayStreamExporter.GetNextandCArrowDeviceArrayStreamExporter.GetNextread a batch from the managed stream, export it and never dispose it. The exported array takes its own references to the buffers, so releasing it on the consumer side doesn't free the batch's references, and withNativeMemoryAllocatorthe memory is only reclaimed by the finalizer.This follows the third option from the issue: the exported array takes ownership of the batch.
ExportedAllocationOwnergetsTakeOwnership(IDisposable). Owned objects are disposed inDispose, after the exported buffers have been unpinned and their shared handles released, so memory managers that throw when disposed while pinned are not affected. This also covers buffers exported by pinning rather than through a shared handle.CArrowArrayExporterandCArrowDeviceArrayExporterget internalExportRecordBatchoverloads with atransferOwnershipflag. The stream exporters passtrue, so a batch is disposed when the consumer releases the exported array (or right away if the export fails). The publicExportRecordBatchmethods still leave the batch to the caller.Batches returned by an
IArrowArrayStreamare owned by the caller, so streams that keep using a batch after handing it out would now see it disposed once the consumer releases it.Tests:
ExportedArrayStreamDisposesBatchesOnReleaseandExportedDeviceStreamDisposesBatchesOnReleaseexport a stream whose batch is built withTestMemoryAllocator, import it again, and check that disposing the imported batch returns every rented buffer. Both fail onmain(buffers stay rented). The fullApache.Arrow.Testssuite passes on net8.0 locally (the pythonnet tests were skipped since Python isn't set up here).This PR was prepared with AI assistance.
Closes #464.