-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
JsonDocument.GetString implementation is not thread safe #76440
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue Details
Looking at the code, I would guess that this can only happen because of torn reads/writes. It might be that we could fix this in the code using a regular Related to #17975 Originally posted by @eiriktsarpalis in dotnet/docs#20563 (comment)
|
Do we document JsonDocument as being thread-safe? We generally don't make any guarantees about thread-safety of instance members unless otherwise explicitly documented, and there are many places where instance members do non-thread-safe caching. What makes this one special enough to be worth adding cost? And, what is this particular cache used for? Is it really common to ask for the same string at the same index multiple times in a row with no intervening other requests? |
We do, the original issue in docs was opened to account for that discrepancy. I created this separate issue to track potentially making the type thread safe in the future. |
@stephentoub |
I don't believe that's warranted. |
Even though
JsonDocument
is nominally immutable (modulo disposability), the internalGetString
implementation employs a caching scheme that is not thread safe:runtime/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs
Line 30 in 5c8aade
Since the cached field is a struct that cannot be written to or read from atomically. It might be that we could fix this in the code using a regular
System.Tuple
, thus ensuring atomic access to cached entries. Obviously this has the downside of incurring one additional allocation, but at least we can guarantee immutability (modulo disposability of course).Related to #17975
cc @layomia @krwq
Originally posted by @eiriktsarpalis in dotnet/docs#20563 (comment)
The text was updated successfully, but these errors were encountered: