Repository navigation
fix: Set an exported array's release callback only once the export has succeeded - #547
Merged
Merged
Conversation
…s succeeded The array exporter set each struct's release callback and private data as soon as it started converting it. If the export then failed, for example because one of the array's buffers had been disposed, the exporter freed what it had allocated but left the caller's struct with its release callback set and pointing at that memory, so releasing it afterwards read freed memory. Each failure also kept a GC handle. Convert the whole array first, then set the release callbacks and private data, children first. If the export fails, leave the caller's struct zeroed, which marks it as released. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012dFdcrBz22tRjiyNwBd7VB
There was a problem hiding this comment.
🟡 Changes recommended
If GCHandle.Alloc throws during SetRelease, previously installed child handles are leaked and permanently root the disposed owner, so rollback must free those handles before clearing the root.
1 open issue
What changed in this PR
This PR improves failure handling in Arrow’s C Data array and record batch exporters, preventing failed conversions from leaving dangling release callbacks.
Changes:
- Install release callbacks only after conversion completes.
- Clear the caller’s struct when export fails.
- Add regression tests for disposed children and columns, including ownership transfer.
| File | Description |
|---|---|
| test/Apache.Arrow.Tests/CDataInterfaceDataTests.cs | Tests failed exports leave structs released and safe to free. |
| src/Apache.Arrow/C/CArrowArrayExporter.cs | Defers callback installation and clears structs on failure. |
Review effort: Balanced
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012dFdcrBz22tRjiyNwBd7VB
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?
CArrowArrayExporterset each struct'sreleasecallback and private data as soon as it started converting it. If the export then failed, for example because one of the array's buffers had already been disposed, the exporter freed the memory it had allocated but left the caller's struct withreleaseset and pointing at that memory. Releasing the struct afterwards, asCArrowArray.Freedoes, then read freed memory. Each failure also kept a GC handle to the exporter's allocations.This covers
ExportArray,ExportRecordBatch, and the stream and device exporters that use them. The schema exporter already setsreleaselast, so it is unchanged.Testing
New tests in
CDataInterfaceDataTestsexport a struct array with a disposed child and a record batch with a disposed column (with and without ownership transfer), check that the struct is left released, and free it. Without the change they fail, and on net472 freeing the struct crashed the test host. All ofApache.Arrow.Tests, including the pyarrow C Data tests, passes on net8.0, net462 and net472.🤖 Generated with Claude Code
https://claude.ai/code/session_012dFdcrBz22tRjiyNwBd7VB