-
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
NullabilityInfoContext is not thread-safe #100254
Comments
I think that's by design, ideally you should try to create an ephemeral cc @buyaa-n |
Yes, it was by design, it is mentioned in the approved API #29723 (comment)
|
You folks mean that the correct way to use this API in this case is as follows: Parallel.ForEach(properties, property =>
{
NullabilityInfo nullabilityInfo = new NullabilityInfoContext().Create(property);
Console.WriteLine($"Property: {property.Name}, Getter nullability: {nullabilityInfo.ReadState}");
}); ? |
Yes, or ideally reuse the same context for all members in a type/type graph. |
Description
Found while looking into #100144.
When caching the
NullabilityInfoContext
to use it in the reflection-based System.Text.Json serializer, I noticed that the context is not thread-safe, It would be nice that the type would implement a thread-safety mechanism like a Concurrent Dictionary for the MemberInfos that it caches.Reproduction Steps
Here's a simple repro that shows the failure by just trying to parallelize
NullabilityInfo
creation of the properties in a type.Expected behavior
Not to throw on parallelized scenarios.
Actual behavior
Regression?
No
Known Workarounds
Create a
new NullabilityInfoContext()
inside Parallel.ForEach but this will slow-down the execution.Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: