Skip to content

Commit

Permalink
fix yaml race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
stakira committed Jul 27, 2024
1 parent af9f1d8 commit 75d2621
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions OpenUtau.Core/Util/Yaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OpenUtau.Classic;
using System.IO;
using OpenUtau.Core.Ustx;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
Expand All @@ -7,19 +7,51 @@
using YamlDotNet.Serialization.NamingConventions;

namespace OpenUtau.Core {
public static class Yaml {
public static ISerializer DefaultSerializer = new SerializerBuilder()
public class Yaml {
public static Yaml DefaultSerializer => instance;
public static Yaml DefaultDeserializer => instance;

private static readonly Yaml instance = new Yaml();

private readonly ISerializer serializer = new SerializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
.WithEventEmitter(next => new FlowEmitter(next))
.DisableAliases()
.WithQuotingNecessaryStrings()
.Build();

public static IDeserializer DefaultDeserializer = new DeserializerBuilder()
private readonly IDeserializer deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.IgnoreUnmatchedProperties()
.Build();

private readonly object serializerLock = new object();
private readonly object deserializerLock = new object();

public string Serialize(object? graph) {
lock (serializerLock) {
return serializer.Serialize(graph);
}
}

public void Serialize(TextWriter writer, object? graph) {
lock (serializerLock) {
serializer.Serialize(writer, graph);
}
}

public T Deserialize<T>(string input) {
lock (deserializerLock) {
return deserializer.Deserialize<T>(input);
}
}

public T Deserialize<T>(TextReader input) {
lock (deserializerLock) {
return deserializer.Deserialize<T>(input);
}
}
}

public class FlowEmitter : ChainedEventEmitter {
Expand Down

0 comments on commit 75d2621

Please sign in to comment.