Skip to content

crossbeam-skiplist: fix RefRange iterator memory leak (#19284)#19292

Merged
ti-chi-bot[bot] merged 2 commits intotikv:release-8.5from
ti-chi-bot:cherry-pick-19284-to-release-8.5
Jan 22, 2026
Merged

crossbeam-skiplist: fix RefRange iterator memory leak (#19284)#19292
ti-chi-bot[bot] merged 2 commits intotikv:release-8.5from
ti-chi-bot:cherry-pick-19284-to-release-8.5

Conversation

@ti-chi-bot
Copy link
Member

This is an automated cherry-pick of #19284

What is changed and how it works?

Issue Number: Close #19285

What's Changed:

Fix a memory leak in crossbeam-skiplist's RefRange iterator.

RefRange::next() and next_back() were using clone_from() to update self.head/self.tail. Since RefEntry has no Drop implementation (by design - callers must explicitly call release()), the old entry was dropped without decrementing its refcount, causing permanent memory leaks.
Note that the the comment of RefEntry says

/// You *must* call `release` to free this type, otherwise the node will be
/// leaked. This is because releasing the entry requires a `Guard`.

This bug affects any code using SkipMap::range() iterators. In TiKV, this manifests as memory leaks in LockTable when check_range() or find_first() are called.

// Before (buggy):
self.head.clone_from(&next_head);  // Old entry dropped without decrement!

// After (fixed):
if let Some(e) = mem::replace(&mut self.head, next_head.clone()) {
    unsafe { e.node.decrement(guard); }  // Properly decrement old entry
}

The fix matches the pattern already used in RefIter::next().

Stress test results (10 seconds, range iteration + insert/remove cycle):

before fix:
t=10s ops=11710 len=3422 alloc=613MB
after fix:
t=10s ops=11786 len=7530 alloc=3MB
Fix RefRange::next() and next_back() to properly decrement old self.head/self.tail refcount.

Related changes

  • PR to update pingcap/docs/pingcap/docs-cn:
  • Need to cherry-pick to the release branch

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Release note

Fix a memory leak in crossbeam-skiplist's RefRange iterator that caused LockTable memory to grow unboundedly when using range queries.

RefRange::next() and next_back() were using clone_from() to update
self.head/self.tail. Since RefEntry has no Drop implementation,
the old entry was dropped without decrementing its refcount, causing
permanent memory leaks.

Fix by using mem::replace() and manually calling decrement() on the
old entry, matching the pattern used in RefIter::next().

Signed-off-by: ekexium <eke@fastmail.com>
@ti-chi-bot ti-chi-bot added dco-signoff: yes Indicates the PR's author has signed the dco. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/cherry-pick-for-release-8.5 This PR is cherry-picked to release-8.5 from a source PR. labels Jan 19, 2026
@ekexium ekexium requested review from cfzjywxk and overvenus January 19, 2026 08:27
Signed-off-by: ekexium <eke@fastmail.com>
@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Jan 22, 2026
@ti-chi-bot ti-chi-bot bot added the lgtm label Jan 22, 2026
@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Jan 22, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfzjywxk, overvenus

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot removed the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Jan 22, 2026
@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Jan 22, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-01-22 04:57:56.57726718 +0000 UTC m=+642704.191224036: ☑️ agreed by overvenus.
  • 2026-01-22 07:31:28.806851316 +0000 UTC m=+651916.420808162: ☑️ agreed by cfzjywxk.

@ti-chi-bot ti-chi-bot bot added cherry-pick-approved Cherry pick PR approved by release team. and removed do-not-merge/cherry-pick-not-approved labels Jan 22, 2026
@ti-chi-bot ti-chi-bot bot merged commit b3ea164 into tikv:release-8.5 Jan 22, 2026
5 checks passed
@ti-chi-bot ti-chi-bot bot deleted the cherry-pick-19284-to-release-8.5 branch January 22, 2026 16:49
lcwangchao pushed a commit to lcwangchao/tikv that referenced this pull request Jan 23, 2026
…ikv#19292)

close tikv#19285

Fix RefRange::next() and next_back() to properly decrement old self.head/self.tail refcount.

Signed-off-by: ekexium <eke@fastmail.com>

Co-authored-by: ekexium <eke@fastmail.com>
Signed-off-by: Chao Wang <cclcwangchao@hotmail.com>
lcwangchao pushed a commit to lcwangchao/tikv that referenced this pull request Jan 23, 2026
…ikv#19292)

close tikv#19285

Fix RefRange::next() and next_back() to properly decrement old self.head/self.tail refcount.

Signed-off-by: ekexium <eke@fastmail.com>

Co-authored-by: ekexium <eke@fastmail.com>
Signed-off-by: Chao Wang <cclcwangchao@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved cherry-pick-approved Cherry pick PR approved by release team. dco-signoff: yes Indicates the PR's author has signed the dco. lgtm release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/cherry-pick-for-release-8.5 This PR is cherry-picked to release-8.5 from a source PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants