YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Make DefaultObjectFactory thread safe

Open alxmitch opened this issue 1 year ago • 5 comments

https://github.com/kubernetes-client/csharp/issues/1537 raised that deserialization (and serialization) is not currently thread-safe, due to concurrent access to the non-thread-safe _stateMethods dictionary in DefaultObjectFactory.

This PR changes the nested inner Dictionary<Type, MethodInfo[]>s in _stateMethods to ConcurrentDictionarys to make concurrent access safe.

Testing

I've added tests to concurrently serialize and deserialize simple YAML. These reproduced the issue before the fix most of the time, but were not 100% reliable (i.e. occasionally suffered false-positive results). I'm open to suggestions for a better way of testing this for thread-safety.

alxmitch avatar Apr 02 '24 06:04 alxmitch

Given the answer to https://github.com/aaubry/YamlDotNet/discussions/844 (which I was not previously aware of), this change may not be relevant, if ISerializer / IDeserializer are not intended to be thread-safe.

alxmitch avatar Apr 03 '24 01:04 alxmitch

To be able to use the same serializer/deserializer work in multiple threads it’s a lot more than just that dictionary. There’s tons of instance fields that are used through the classes like the emitter and parser.

EdwardCooke avatar Apr 10 '24 23:04 EdwardCooke

This library is more thread safe than I originally thought. I’m on my phone but going through the classes real quick the anchor related classes seem to be the remaining non-thread safe areas. I didn’t do much research, like where they’re newed up to see if it even matters, or any other digging into the guts of the library. It would be interesting to run your tests with a much larger yaml payload. Like what’s in the benchmark project.

EdwardCooke avatar Apr 11 '24 00:04 EdwardCooke

Could you look at the other classes and make sure that they are using concurrent lists and dictionaries as well?

EdwardCooke avatar May 11 '24 15:05 EdwardCooke

Can you fix the serializationtest conflict? I'll merge in the pr in a couple of weeks.

EdwardCooke avatar Jul 14 '24 13:07 EdwardCooke