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

[Panic] Attempt to add with overflow during insert #21

Closed
JoshLind opened this issue Jan 7, 2024 · 3 comments · Fixed by #23
Closed

[Panic] Attempt to add with overflow during insert #21

JoshLind opened this issue Jan 7, 2024 · 3 comments · Fixed by #23
Assignees
Labels
bug Something isn't working
Milestone

Comments

@JoshLind
Copy link

JoshLind commented Jan 7, 2024

Hi, we recently started using mini-moka to cache and retrieve data, but we've run into an unusual panic during an insert call. The panic seems rare, but this is the cleaned up stacktrace:

details = \"panicked at 'attempt to add with overflow', /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/mini-moka-0.10.2/src/common/frequency_sketch.rs:183:9\"
backtrace = \"\"\"\n
...
9:     0x56217dd61123 - mini_moka::common::frequency_sketch::FrequencySketch::increment::h702da432b2c28c0d 
10:     0x56217d5d5a31 - mini_moka::sync::base_cache::Inner<K,V,S>::apply_reads::h89098547f143cebc
at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/mini-moka-0.10.2/src/sync/base_cache.rs:782:35. <mini_moka::sync::base_cache::Inner<K,V,S> as mini_moka::common::concurrent::housekeeper::InnerSync>::sync::h8c6497ded41ff4dd
at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/mini-moka-0.10.2/src/sync/base_cache.rs:664:17\n 
11:     0x56217d41c561 - mini_moka::common::concurrent::housekeeper::Housekeeper::try_sync::h45ebd0c867b7d29e\n                               
at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/mini-moka-0.10.2/src/common/concurrent/housekeeper.rs:61:17\n                           mini_moka::sync::base_cache::BaseCache<K,V,S>::apply_reads_writes_if_needed::hf2dc1456ae08dd31\n                               
at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/mini-moka-0.10.2/src/sync/base_cache.rs:211:17\n                           mini_moka::sync::cache::Cache<K,V,S>::schedule_write_op::he092ec6fa514b5e5\n                               
at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/mini-moka-0.10.2/src/sync/cache.rs:588:13\n                           mini_moka::sync::cache::Cache<K,V,S>::insert_with_hash::h07b1808c0524e791\n                               
at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/mini-moka-0.10.2/src/sync/cache.rs:447:9\n                           mini_moka::sync::cache::Cache<K,V,S>::insert::hff040ece82fbc7a4\n                               
at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/mini-moka-0.10.2/src/sync/cache.rs:441:9

It seems like the panic is occurring here: https://github.com/moka-rs/mini-moka/blob/main/src/common/frequency_sketch.rs#L183.

We're not really doing anything unusual. We're importing mini-moka:

mini-moka = { version = "0.10.2", features = ["sync"] }

Creating a new Cache in our code (with no overrides):

let cache = Cache::new(500);

And (on separate tokio threads) simply calling get and insert depending on reads/writes to the cache. Perhaps this is some type of race condition? Or maybe the addition needs to use wrapping_add, too?

Our rust version is: 1.72.1

Thanks for your help :)

@tatsuya6502
Copy link
Member

Thank you for reporting!

Or maybe the addition needs to use wrapping_add, too?

Ah, you are right. Sorry, the same bug was found and fixed in moka in last May, but I forgot to apply the fix to mini-moka.

I will fix mini-moka and publish a new point-release soon.

@tatsuya6502 tatsuya6502 self-assigned this Jan 7, 2024
@tatsuya6502 tatsuya6502 added the bug Something isn't working label Jan 7, 2024
@tatsuya6502 tatsuya6502 linked a pull request Jan 7, 2024 that will close this issue
2 tasks
@tatsuya6502 tatsuya6502 added this to the v0.10.3 milestone Jan 7, 2024
@tatsuya6502
Copy link
Member

Thanks again for reporting this bug. I fixed it via #23 and published v0.10.3 to crates.io.

@JoshLind
Copy link
Author

JoshLind commented Jan 9, 2024

Awesome, thanks so much @tatsuya6502 😄

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 a pull request may close this issue.

2 participants