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

[cherrypick] PR#20236 #20280

Merged
merged 1 commit into from
Nov 15, 2024
Merged

Conversation

arun-koshy
Copy link
Contributor

Description

pick writeback cache fix into next mainnet release

The previous version of this code was based on a misunderstanding of the
Moka Cache API. It assumed that:

    let entry = cache.entry(k).or_insert_with(|| v);
    entry.is_fresh()

could be used to detect whether there was a race with

    cache.insert(k, v)

In fact this is not the case! Both threads must use `or_insert_with` in
order to handle the race correctly.

The bug is instantly reproducible with the following code:

    use moka::sync::Cache;
    use std::sync::{Arc, Mutex};

fn monotonic_update(cache: Arc<Cache<u64, Arc<Mutex<u64>>>>, key: u64,
value: u64) {
        let entry = cache
            .entry(key)
            .or_insert_with(|| Arc::new(Mutex::new(value)));

        if !entry.is_fresh() {
            let mut entry = entry.value().lock().unwrap();
// only update if the new value is greater than the current value
            if value > *entry {
                *entry = value;
            }
        }
    }

fn blind_write(cache: Arc<Cache<u64, Arc<Mutex<u64>>>>, key: u64, value:
u64) {
        cache.insert(key, Arc::new(Mutex::new(value)));
    }

    fn main() {
        for _ in 0..1000 {
            let cache = Arc::new(Cache::new(1000));
            let cache1 = cache.clone();
            let cache2 = cache.clone();

            let handle1 = std::thread::spawn(move || {
                monotonic_update(cache1, 1, 1);
            });
            let handle2 = std::thread::spawn(move || {
                // Correct way to update the value
                // monotonic_update(cache2, 1, 2);

                // Incorrect way to update the value
                blind_write(cache2, 1, 2);
            });

            handle1.join().unwrap();
            handle2.join().unwrap();

            let entry = cache.get(&1).unwrap();
            let value = entry.lock().unwrap();
            assert_eq!(*value, 2);
        }
    }

## Description 

Describe the changes or additions included in this PR.

## Test plan 

How did you test the new or updated feature?

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
Copy link

vercel bot commented Nov 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sui-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 15, 2024 5:55am
3 Skipped Deployments
Name Status Preview Comments Updated (UTC)
multisig-toolkit ⬜️ Ignored (Inspect) Nov 15, 2024 5:55am
sui-kiosk ⬜️ Ignored (Inspect) Nov 15, 2024 5:55am
sui-typescript-docs ⬜️ Ignored (Inspect) Nov 15, 2024 5:55am

@arun-koshy arun-koshy enabled auto-merge (squash) November 15, 2024 06:01
@arun-koshy arun-koshy merged commit a5d8b0d into releases/sui-v1.37.0-release Nov 15, 2024
52 checks passed
@arun-koshy arun-koshy deleted the ak/cherrypick branch November 15, 2024 06:22
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