Skip to content
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

Is the IDeserializer thread safe? #896

Closed
jackfiled opened this issue Jan 22, 2024 · 2 comments
Closed

Is the IDeserializer thread safe? #896

jackfiled opened this issue Jan 22, 2024 · 2 comments

Comments

@jackfiled
Copy link

Describe the bug
I write a code trying to deserialize with Parallel.ForEach but it ran into an exception that:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
   at YamlDotNet.Serialization.ObjectFactories.DefaultObjectFactory.GetStateMethods(Type attributeType, Type valueType)
   at YamlDotNet.Serialization.ObjectFactories.DefaultObjectFactory.ExecuteState(Type attributeType, Object value)
   at YamlDotNet.Serialization.ObjectFactories.DefaultObjectFactory.ExecuteOnDeserializing(Object value)
   at YamlDotNet.Serialization.NodeDeserializers.ObjectNodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)

To Reproduce
This is a minimal Program.cs to reproduce this question:

using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

DeserializerBuilder builder = new DeserializerBuilder()
    .WithNamingConvention(CamelCaseNamingConvention.Instance)
    .IgnoreUnmatchedProperties();
IDeserializer deserializer = builder.Build();

List<int> array = [1,2,3,4,5,6,7,8];
const string content = "title: 123123123";

Parallel.ForEach(array, _ =>
{
    Model model = deserializer.Deserialize<Model>(content);
    Console.WriteLine(model.Title);
});

public class Model
{
    public string? Title { get; set; }
}
@filzrev
Copy link
Contributor

filzrev commented Jan 22, 2024

I have previously asked a similar question.
The answer is that it is not thread-safe. #844

@jackfiled
Copy link
Author

I have previously asked a similar question. The answer is that it is not thread-safe. #844

Fine, thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants