Duplicate the cached value before writing it in the local cache#37587
Merged
rafaelfranca merged 1 commit intorails:masterfrom Oct 29, 2019
Merged
Duplicate the cached value before writing it in the local cache#37587rafaelfranca merged 1 commit intorails:masterfrom
rafaelfranca merged 1 commit intorails:masterfrom
Conversation
e3294bb to
9b09187
Compare
Edouard-chin
approved these changes
Oct 29, 2019
eljojo
reviewed
Oct 29, 2019
| def write_entry(key, value, **options) | ||
| @data[key] = value | ||
| def write_entry(key, entry, **options) | ||
| entry.dup_value! |
Member
There was a problem hiding this comment.
It does a Marshal.load
Member
|
I'm pretty sure we have a PR for this already. |
Member
|
Ah, it was #36656 |
Member
|
Yeah #36656 fixed a similar issue but only protecting when returning a value |
casperisfine
pushed a commit
to Shopify/rails
that referenced
this pull request
Apr 19, 2021
The local cache need to protect against mutations of both the initial received value, and the value returned. See: - rails#36656 - rails#37587 Because of this, the overhead of the local cache end up being quite significant. On a single read, the value will be deep duped twice. So unless the value is one of the type benefiting from a fast path, we'll have to do two `Marshal.load(Marshal.dump(value))` roundtrips, which for large values can be very expensive. By using a specialized `LocalEntry` type instead, we can store the `Marshal` payload rather than the original value. This way the overhead is reduced to a single `Marshal.dump` on writes and a single `Marshal.load` on reads.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Discovered by @eljojo.
I think the test case showcase the bug somewhat properly. In short it was already protecting itself from mutation of returned values, but not from mutation of the original value, e.g.:
@rafaelfranca @camilo @etiennebarrie @Edouard-chin