Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak when dropping a sync or future cache. #177

Merged
merged 4 commits into from Sep 3, 2022

Conversation

tatsuya6502
Copy link
Member

@tatsuya6502 tatsuya6502 commented Sep 3, 2022

Fixes #176

Problem and Cause

When a sync or future cache was dropped, value part of cache entries were left allocated (leaked). This was due to a bug in the Drop::drop method of moka::cht::SegmentedHashMap; it was using bucket::defer_acquire_destroy function to drop any entry (a bucket struct), but that function only drops the key part of the entry.

Fix

Fixed the bug by updating the drop method of moka::cht::SegmentedCache to use the followings:

  • For a live bucket (having both the key and value), use bucket::defer_destroy_bucket function to drop both the key and value.
  • For a dead bucket (having the tombstone bit set and having only the key), continue using bucket::defer_acquire_destroy function to drop only the key.

Verification

Verified the fix by the following methods:

  • Ran the reproducing code using valgrind and confirmed that the memory leak was fixed.
    • "definitely lost: 0 bytes in 0 blocks"
    • "indirectly lost: 0 bytes in 0 blocks"
  • Added a unit test moka::cht::segment::test::drop_map_after_concurrent_updates to ensure that the drop method of both the key and value types are called when the map is dropped.

Update the `drop` method of `cht::SegmentedHashMap` to drop the values in buckets if
they are live entries.
@tatsuya6502 tatsuya6502 added this to the v0.9.4 milestone Sep 3, 2022
@tatsuya6502 tatsuya6502 self-assigned this Sep 3, 2022
@tatsuya6502 tatsuya6502 added the bug Something isn't working label Sep 3, 2022
@tatsuya6502 tatsuya6502 marked this pull request as ready for review September 3, 2022 03:44
Copy link
Member Author

@tatsuya6502 tatsuya6502 left a comment

Choose a reason for hiding this comment

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

Merging.

@tatsuya6502 tatsuya6502 merged commit 870fd8d into master Sep 3, 2022
@tatsuya6502 tatsuya6502 deleted the fix-mem-leak-on-drop branch September 3, 2022 08:10
@tatsuya6502
Copy link
Member Author

Note: I have updated mokabench, the load generator we use for pre-release testing, to track heap allocations (commit: moka-rs/mokabench@bec3976). I ran it against Moka v0.9.3 (without the fix in this PR) and v0.9.4 (with the fix). I confirmed that v0.9.4 has nothing to indicate any other memory leaks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory leak after moka::sync::Cache is dropped
1 participant