Skip to content

Fix memory leak in extent_hooks split failure path (#2815) - #2971

Open
NITIN9181 wants to merge 4 commits into
jemalloc:devfrom
NITIN9181:fix-issue-2815
Open

NITIN9181 wants to merge 4 commits into
jemalloc:devfrom
NITIN9181:fix-issue-2815

Conversation

@NITIN9181

Copy link
Copy Markdown
Contributor

Fixes #2815

Description

This PR addresses a memory leak that occurs when custom extent hooks are used, specifically when the custom split hook returns an error (failure to split).

Previously, if extent_recycle_split or extent_grow_retained requested a split via the split hook and it failed, the code would handle the failure by calling extents_abandon_vm on the un-splittable region. While extents_abandon_vm properly cleans up internal extent metadata, it leaks the underlying virtual memory mapping. The user's custom dalloc hook was never invoked to clean up the original allocation.

This PR modifies the failure paths in extent_recycle_split and extent_grow_retained to explicitly call extent_dalloc_wrapper instead of extents_abandon_vm. This ensures that jemalloc appropriately delegates the un-splittable region back to the custom deallocation hook (dalloc), completely preventing the virtual memory leak.

Changes Made

  • src/extent.c: extent_recycle_split: Replaced calls to extents_abandon_vm with extent_dalloc_wrapper for the to_salvage and to_leak segments when a split operation fails.
  • src/extent.c: extent_grow_retained: Replaced the call to extents_abandon_vm with extent_dalloc_wrapper on the split failure path.
  • src/extent.c: extent_grow_retained: Added extent_gdump_add(tsdn, to_salvage) and extent_gdump_add(tsdn, to_leak) right before their respective deallocations to ensure heap profiling metadata remains balanced during the failure path cleanup.

Verification

  • Verified by creating a test program where custom alloc, split, and dalloc hooks were registered.
  • Hardcoded the split hook to always return true (failure).
  • Verified that previously this would leak memory, but with this PR applied, custom_dalloc is successfully invoked to clean up the allocation after the split failure.
  • Compiled and verified on an Ubuntu Linux Docker environment.

@NITIN9181

Copy link
Copy Markdown
Contributor Author

I've pushed a fix for the AppVeyor test failure (: Should own 0 locks of rank >= 14: extent_grow(17)). The error paths in extent_grow_retained and extent_recycle_split were calling extent_dalloc_wrapper while holding pac->grow_mtx (rank 17), which caused a lock rank violation since extent_dalloc_wrapper requires no core locks to be held. I've updated the code to drop the lock before deallocating.

@NITIN9181

Copy link
Copy Markdown
Contributor Author

@guangli-dai please review

@guangli-dai

guangli-dai commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Some CIs are failing including windows, mac, and more. Can you fix? Also, please rebase to dev and this commit can be removed.

Comment thread src/extent.c
edata_cache_put(tsdn, pac->edata_cache, edata);
}

return err;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

`return NULL);``

@nikhilkrishnan-ai nikhilkrishnan-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.

Files changed

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.

Memory leak: arena_extent_dalloc() is not called for extents for which arena_extent_split() failed

3 participants