crossbeam-skiplist: fix RefRange iterator memory leak#19284
crossbeam-skiplist: fix RefRange iterator memory leak#19284ti-chi-bot[bot] merged 1 commit intotikv:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical memory leak in the crossbeam-skiplist crate's RefRange iterator implementation. The issue occurred because RefRange::next() and next_back() were using clone_from() to update internal state, which dropped old RefEntry objects without decrementing their reference counts, leading to memory leaks.
Changes:
- Fixed memory leak in
RefRange::next()by properly decrementing old entry refcount - Fixed memory leak in
RefRange::next_back()by properly decrementing old entry refcount - Added regression test
range_iter_refcount_leakto prevent future regressions
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| components/crossbeam-skiplist/src/base.rs | Fixed RefRange::next() and next_back() to use mem::replace() with explicit decrement() instead of clone_from() |
| components/crossbeam-skiplist/tests/map.rs | Added regression test that exercises range iteration to detect memory leaks |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
efe2500 to
e44ff00
Compare
e44ff00 to
d47fd3f
Compare
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>
d47fd3f to
043854d
Compare
overvenus
left a comment
There was a problem hiding this comment.
Should we impl Drop for RefRange and call drop_impl?
I suppose no because we cannot access |
|
[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 DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
/cherry-pick release-8.5 |
|
@ekexium: new pull request created to branch DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
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>
|
/cherry-pick release-8.5-20251208-v8.5.4 |
|
@ekexium: new pull request created to branch DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
close #19285 Fix RefRange::next() and next_back() to properly decrement old self.head/self.tail refcount. Signed-off-by: ekexium <eke@fastmail.com> Signed-off-by: lucasliang <nkcs_lykx@hotmail.com> Signed-off-by: you06 <you1474600@gmail.com> Co-authored-by: ekexium <eke@fastmail.com> Co-authored-by: lucasliang <nkcs_lykx@hotmail.com> Co-authored-by: you06 <you1474600@gmail.com>
close tikv#19285 Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
|
In response to a cherrypick label: new pull request created to branch |
close tikv#19285 Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
|
In response to a cherrypick label: new pull request created to branch |
close tikv#19285 Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
|
In response to a cherrypick label: new pull request created to branch |
…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>
…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>
What is changed and how it works?
Issue Number: Close #19285
What's Changed:
Fix a memory leak in
crossbeam-skiplist'sRefRangeiterator.RefRange::next()andnext_back()were usingclone_from()to updateself.head/self.tail. SinceRefEntryhas noDropimplementation (by design - callers must explicitly callrelease()), the old entry was dropped without decrementing its refcount, causing permanent memory leaks.Note that the the comment of
RefEntrysaysThis bug affects any code using
SkipMap::range()iterators. In TiKV, this manifests as memory leaks inLockTablewhencheck_range()orfind_first()are called.The fix matches the pattern already used in
RefIter::next().Stress test results (10 seconds, range iteration + insert/remove cycle):
Related changes
pingcap/docs/pingcap/docs-cn:Check List
Tests
Side effects
Release note