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

Lock usage of YamlDotNet Serializer and Deserializer #1544

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions src/KubernetesClient/KubernetesYaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace k8s
/// </summary>
public static class KubernetesYaml
{
private static readonly object DeserializerLockObject = new object();
private static readonly object SerializerLockObject = new object();

private static DeserializerBuilder CommonDeserializerBuilder =>
new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
Expand Down Expand Up @@ -156,8 +159,11 @@ public static List<object> LoadAllFromString(string content, IDictionary<string,
parser.Consume<StreamStart>();
while (parser.Accept<DocumentStart>(out _))
{
var dict = GetDeserializer(strict).Deserialize<Dictionary<object, object>>(parser);
types.Add(mergedTypeMap[dict["apiVersion"] + "/" + dict["kind"]]);
lock (DeserializerLockObject)
{
var dict = GetDeserializer(strict).Deserialize<Dictionary<object, object>>(parser);
types.Add(mergedTypeMap[dict["apiVersion"] + "/" + dict["kind"]]);
}
}

parser = new MergingParser(new Parser(new StringReader(content)));
Expand All @@ -167,8 +173,11 @@ public static List<object> LoadAllFromString(string content, IDictionary<string,
while (parser.Accept<DocumentStart>(out _))
{
var objType = types[ix++];
var obj = GetDeserializer(strict).Deserialize(parser, objType);
results.Add(obj);
lock (DeserializerLockObject)
{
var obj = GetDeserializer(strict).Deserialize(parser, objType);
results.Add(obj);
}
}

return results;
Expand Down Expand Up @@ -204,13 +213,19 @@ public static string SaveToString<T>(T value)
public static TValue Deserialize<TValue>(string yaml, bool strict = false)
{
using var reader = new StringReader(yaml);
return GetDeserializer(strict).Deserialize<TValue>(new MergingParser(new Parser(reader)));
lock (DeserializerLockObject)
{
return GetDeserializer(strict).Deserialize<TValue>(new MergingParser(new Parser(reader)));
}
}

public static TValue Deserialize<TValue>(Stream yaml, bool strict = false)
{
using var reader = new StreamReader(yaml);
return GetDeserializer(strict).Deserialize<TValue>(new MergingParser(new Parser(reader)));
lock (DeserializerLockObject)
{
return GetDeserializer(strict).Deserialize<TValue>(new MergingParser(new Parser(reader)));
}
}

public static string SerializeAll(IEnumerable<object> values)
Expand All @@ -231,7 +246,11 @@ public static string SerializeAll(IEnumerable<object> values)
if (value != null)
{
emitter.Emit(new DocumentStart());
Serializer.SerializeValue(emitter, value, value.GetType());
lock (SerializerLockObject)
{
Serializer.SerializeValue(emitter, value, value.GetType());
}

emitter.Emit(new DocumentEnd(true));
}
}
Expand All @@ -252,7 +271,10 @@ public static string Serialize(object value)

emitter.Emit(new StreamStart());
emitter.Emit(new DocumentStart());
Serializer.SerializeValue(emitter, value, value.GetType());
lock (SerializerLockObject)
{
Serializer.SerializeValue(emitter, value, value.GetType());
}

return stringBuilder.ToString();
}
Expand Down
Loading