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

AccountsIndex::get_cloned() *must* add entry to in-mem cache #35322

Merged
merged 1 commit into from
Feb 26, 2024

Conversation

brooksprumo
Copy link
Contributor

Problem

AccountsIndex::get_cloned() currently does not add the entry to the in-mem cache, but that could lead to lost modifications.

If the entry is only on-disk, returning the cloned entry would allow the caller to modify the entry (i.e. make it 'dirty'). Since the entry is still not in-mem, when the entry gets dropped, those modifications would not get written back to disk, thus losing any modification.

Summary of Changes

Always add the index entry to the in-mem cache when using get_cloned().

@brooksprumo brooksprumo self-assigned this Feb 26, 2024
@brooksprumo brooksprumo marked this pull request as ready for review February 26, 2024 21:45
@brooksprumo
Copy link
Contributor Author

@jeffwashington @HaoranYi I'm requesting your review here before CI finishes because I wanted to get your eyes/thoughts on the comments I added. If there are any requested changes to the text, it'd be nice to not have to wait for all of CI to complete first.

Copy link
Contributor

@jeffwashington jeffwashington left a comment

Choose a reason for hiding this comment

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

lgtm. This is required to avoid race conditions.

// We *must* add the index entry to the in-mem cache!
// If the index entry is only on-disk, returning a clone would allow the entry
// to be modified, but those modifications would be lost on drop!
self.get_and_then(pubkey, |entry| (true, entry.cloned()))
Copy link
Contributor

Choose a reason for hiding this comment

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

What if the caller is only reading the entry? I wonder if we should add a bool arg to the fn and let the caller decide whether the entry need to be added to in-mem cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have a follow-up PR in the works that'll enable this safely. Right now, the caller can use get_and_then() to clone and decide to add to the in-mem cache or not. If they do this wrong, it's still unsafe.

Copy link
Contributor

@HaoranYi HaoranYi left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link

codecov bot commented Feb 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.7%. Comparing base (8143fc3) to head (7158178).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #35322   +/-   ##
=======================================
  Coverage    81.7%    81.7%           
=======================================
  Files         834      834           
  Lines      224232   224232           
=======================================
+ Hits       183351   183377   +26     
+ Misses      40881    40855   -26     

@brooksprumo brooksprumo merged commit bf2e8ee into solana-labs:master Feb 26, 2024
35 checks passed
@brooksprumo brooksprumo deleted the self-ref/fix-get-cloned branch February 26, 2024 23:20
jeffwashington pushed a commit to jeffwashington/solana that referenced this pull request Feb 27, 2024
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