-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Please add documentation for thread-safety guarantees of JsonDocument (and related types) #20563
Comments
FYI @tdykstra - hope to address this in the .NET 5 doc wave for JSON. |
I don't think documentation was adequately updated for this issue, ran into this in some code at my day job. With the fix PR (#24938) it now explicitly states |
Seconded, please amend this. I also just wasted days tracing a sporadic concurrency bug in my application code down to an immutable BCL type which is documented as thread-safe. |
@eiriktsarpalis @gewarren Please see the latest two comments on this issue. |
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 dotnet/runtime#17975 |
We probably don't want to sacrifice performance (added allocations) for thread safety, so until we have atomic APIs for 128-bit data we should probably just document that the type is not thread safe. |
Thanks for looking at this so speedily after our revival of this issue. I'm not sure without further clarification of your intent that it can be said to be closed though, as the PR only removes the mention of thread safety without explicitly stating that it is not thread safe, and thus we're just back to the same situation we were in when this issue was first raised - the OP requested:
|
Copying my response from the latest PR:
|
Description
Looking at the documentation for
JsonDocument
it is not obvious if it can be safely used by multiple threads or not.A cursory glance at the code suggests that the parsing is done up-front and, therefore, it is safe - however we tripped over the caching that is done via
_lastIndexAndString
(i.e., if multiple threads callGetString
on an element at the same time, then they may get back the wrong value).I want to be clear that this is a doc bug: I'm fine with having a string cache (since I assume that it provides noticeable perf benefits), but I would have liked clear guidance that
JsonDocument
and uses of anyJsonElement
from the same parent document cannot be done safely from multiple threads (without callingClone
on each element).Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: