Return a copy of the cache entry when local_cache exists:#36656
Merged
Conversation
Contributor
There was a problem hiding this comment.
How could dup_entry ever be nil?
Member
Author
There was a problem hiding this comment.
in case you try to access a key that doesn't exist
Contributor
There was a problem hiding this comment.
Nevermind, I'm stuck in the past, turns out nil.dup actually exist on modern rubies TIL.
Member
Author
There was a problem hiding this comment.
No because dup_value! does a Marshal.load
- When the local cache exists (during the request lifecycle), the entry returned from the LocalStore is passed as a reference which means mutable object can accidentaly get modified. This behaviour seems unnecessarily unsafe and is prone to issues like it happened in our application. This patch dup the `Entry` returned from the cache and dup it's internal value.
913f53d to
f08bf72
Compare
| def fetch_entry(key, options = nil) # :nodoc: | ||
| @data.fetch(key) { @data[key] = yield } | ||
| entry = @data.fetch(key) { @data[key] = yield } | ||
| dup_entry = entry.dup |
Member
There was a problem hiding this comment.
Why you need to dup the entry itself?
Member
Author
There was a problem hiding this comment.
the @data hash keeps a reference to the entry, if I don't dup it, user can modify the underlying value which would affect the cache.
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.

Return a copy of the cache entry when local_cache exists:
When the local cache exists (during the request lifecycle), the
entry returned from the LocalStore is passed as a reference which
means mutable object can accidentaly get modified.
This behaviour seems unnecessarily unsafe and is prone to
issues like it happened in our application.
This patch dup the
Entryreturned from the cache and dup it'sinternal value.
cc/ @rafaelfranca @casperisfine