Skip to content

Fix memory leak when replacing FFI schema names - #11932

Open
timmyb32r wants to merge 2 commits into
apache:mainfrom
timmyb32r:fix-ffi-memleak-with_name
Open

timmyb32r wants to merge 2 commits into
apache:mainfrom
timmyb32r:fix-ffi-memleak-with_name

Conversation

@timmyb32r

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

None — standalone bug fix.

Rationale for this change

Repeated calls to FFI_ArrowSchema::with_name leak the previous name allocation. For example, .with_name("a")?.with_name("b")? leaves the allocation for "a" unreleased even after the schema is dropped.

What changes are included in this PR?

Release the previous name allocation when replacing it.

Keep with_name safe by rejecting schemas that do not use this crate's original release callback, since ownership of their name allocation cannot be assumed.

Are these changes tested?

Added a regression test in src/ffi.rs using a test allocator to verify that the previous name is freed on replacement and the final name is freed when the schema is dropped.

The regression test fails before the fix and passes afterward in debug and release builds.

Additional tests cover rejection of empty schemas and foreign release callbacks, including cleanup through the original producer's callback.

Are there any significant user-facing changes?

with_name now returns an error for schemas without this crate's original release callback. This includes empty schemas and schemas whose release callback has been replaced or wrapped. The method remains safe.

AI assistance

Codex identified the issue, generated the fix and regression tests, ran validation, and drafted this PR description.

@github-actions github-actions Bot added the arrow-core Core crates related to representing arrow data label Oct 27, 2026
@Rich-T-kid Rich-T-kid added the bug label Oct 27, 2026

@Rich-T-kid Rich-T-kid left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

test_replacing_name_releases_previous_allocation() is failing locally for me

can u run this

cargo +nightly miri test -p arrow-schema --lib --features ffi \
  ffi::tests::test_replacing_name_releases_previous_allocation

Comment thread arrow-schema/src/ffi.rs Outdated
Comment on lines +196 to +199
std::ptr::fn_addr_eq(
release,
release_schema as unsafe extern "C" fn(*mut FFI_ArrowSchema),
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

fn_addr_eq(release, release_schema) can return false even for the same function. The Rust documentation explicitly permits this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this! I checked — there really is a problem here. Still thinking about the best way to fix it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Rich-T-kid I've thought this through and updated the fix to follow the same ownership model as with_metadata: the name is now owned by SchemaPrivateData, and with_name is unsafe. Could you take another look?

@Rich-T-kid
Rich-T-kid self-requested a review November 1, 2026 22:44

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-core Core crates related to representing arrow data bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants