Skip to content

fix: memory leak in dropping uninserted guard#120

Merged
arthurprs merged 3 commits into
arthurprs:masterfrom
hanabi1224:fix-guard-mem-leak
Jun 4, 2026
Merged

fix: memory leak in dropping uninserted guard#120
arthurprs merged 3 commits into
arthurprs:masterfrom
hanabi1224:fix-guard-mem-leak

Conversation

@hanabi1224
Copy link
Copy Markdown
Contributor

This PR attempts to fix a potential memory leak when dropping uninserted PlaceholderGuard instances. Basically, an uninserted PlaceholderGuard is not removed from CacheShard::entries and cannot be reused for the same key.

@hanabi1224 hanabi1224 marked this pull request as ready for review June 4, 2026 11:40
@arthurprs arthurprs requested a review from Copilot June 4, 2026 14:57
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to fix a memory leak / slot exhaustion scenario where dropping an uninserted PlaceholderGuard leaves the placeholder allocated in CacheShard::entries, preventing reuse of the freed slab slot for subsequent placeholders.

Changes:

  • Add a regression test (test_guard_leak) that asserts dropping an uninserted guard frees and reuses the same placeholder index.
  • Add a test-only accessor on PlaceholderGuard to expose its underlying shared placeholder (for test assertions).
  • Update CacheShard::remove_placeholder to also remove the placeholder from entries (in addition to map).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/sync.rs Adds a regression unit test to verify placeholder slot reuse after dropping an uninserted guard.
src/sync_placeholder.rs Adds a #[cfg(test)] accessor to expose the shared placeholder for assertions in unit tests.
src/shard.rs Attempts to fix the leak by removing placeholder slots from CacheShard::entries when removing placeholders.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/shard.rs
@hanabi1224
Copy link
Copy Markdown
Contributor Author

Hey @arthurprs, please let me know if there are any outstanding issues with this fix. I'd appreciate a patch release once this is accepted and merged. I have a service leaking ~10GiB RAM in ~10h due to this bug. Thanks!

Cover the two documented cases where a placeholder is removed/replaced
while a guard is still outstanding (overwrite insert, and remove + slot
reuse). Both panicked under the original unconditional entries.remove and
pass with the fix that only frees the slot when the placeholder is still
present.
@arthurprs
Copy link
Copy Markdown
Owner

@hanabi1224 release coming shortly!

@arthurprs arthurprs merged commit 52e42fb into arthurprs:master Jun 4, 2026
11 checks passed
@arthurprs
Copy link
Copy Markdown
Owner

Thank you for the fix @hanabi1224 , expect a release within the hour.

@hanabi1224 hanabi1224 deleted the fix-guard-mem-leak branch June 4, 2026 21:05
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.

3 participants