Fix deserializer cache race - #2219
Open
jnm2 wants to merge 4 commits into
Open
Conversation
One SQL string, one connection string and one parameter type resolve to a single Identity, so every caller shares one CacheInfo slot. Only the value of @mode changes the result shape - the same thing a branching stored procedure does in production. CacheInfo.Deserializer holds a multi-field struct published by a plain field write, so a reader can observe a Hash from one shape paired with the Func compiled for another shape. The guard then passes and the wrong deserializer runs, throwing InvalidCastException or silently returning wrong values. One test per site that reads the cached deserializer. All eight fail on this commit.
DeserializerState was a struct with an int Hash and a Func reference, assigned into CacheInfo.Deserializer by a plain property write. A struct that size is never written or read atomically, so concurrent callers could pair a Hash from one result shape with the Func compiled for another. The Hash then matched the reader in hand, the guard passed, and the wrong deserializer ran. Making it a sealed class turns publication into one reference store, which is atomic. Writes only happen on a guard miss, so reads get cheaper: one load instead of two.
Making DeserializerState a reference type fixed tearing within the primary deserializer, but multi-map kept its rest-set in a second CacheInfo field. Two fields cannot be assigned together, and the gap between the two writes spans a GenerateDeserializers call and a Skip(1).ToArray(), so a reader could easily take one shape's Deserializer and another shape's OtherDeserializers. The Hash matched, the guard passed, and the mapper ran a rest-set built for a different result shape. Folding it into DeserializerState makes the whole set move under the single reference write, and removes a field. The two new tests fail without this change: 15721 and 9752 corrupt reads respectively, versus a handful for the single-type paths - the window here is far wider.
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.
Fixes #2117. Analysis at #2117 (comment)
Likely fixes #1896.