Skip to content

fix: Set an exported array's release callback only once the export has succeeded - #547

Merged
CurtHagenlocher merged 2 commits into
apache:mainfrom
CurtHagenlocher:c-export-failure
Dec 5, 2026
Merged

CurtHagenlocher merged 2 commits into
apache:mainfrom
CurtHagenlocher:c-export-failure

Conversation

@CurtHagenlocher

Copy link
Copy Markdown
Contributor

What's changed?

CArrowArrayExporter 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 already been disposed, the exporter freed the memory it had allocated but left the caller's struct with release set and pointing at that memory. Releasing the struct afterwards, as CArrowArray.Free does, then read freed memory. Each failure also kept a GC handle to the exporter's allocations.

  • The array or record batch is converted in full first. Only then are the release callbacks and private data set, children first.
  • If the export fails, the caller's struct is left zeroed, which marks it as released, and the exception is rethrown.

This covers ExportArray, ExportRecordBatch, and the stream and device exporters that use them. The schema exporter already sets release last, so it is unchanged.

Testing

New tests in CDataInterfaceDataTests export 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 of Apache.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

…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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported, and the changes include cleanup handling with regression tests for failed exports.

No open issues 🎉

1 resolved since last review

Review effort: Lite

@CurtHagenlocher
CurtHagenlocher merged commit 38ce7ee into apache:main Dec 5, 2026
14 checks passed
@CurtHagenlocher
CurtHagenlocher deleted the c-export-failure branch December 5, 2026 14:24
CurtHagenlocher added a commit that referenced this pull request Dec 5, 2026
…s succeeded (#547) (#549)

Brings #547 to maint-23.x. maint-23.x has no transferOwnership overload
of ExportRecordBatch, so the record batch test calls the two-argument
overload.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants