You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by fl...@apache.org on 2023/07/31 10:51:32 UTC
[tinkerpop] branch TINKERPOP-2976 created (now 01c6571a32)
This is an automated email from the ASF dual-hosted git repository.
florianhockmann pushed a change to branch TINKERPOP-2976
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
at 01c6571a32 TINKERPOP-2976 Fix modification while iterating
This branch includes the following new commits:
new 01c6571a32 TINKERPOP-2976 Fix modification while iterating
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[tinkerpop] 01/01: TINKERPOP-2976 Fix modification while iterating
Posted by fl...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
florianhockmann pushed a commit to branch TINKERPOP-2976
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
commit 01c6571a32b7881aa6e41b26f105629e62c06323
Author: Florian Hockmann <fh...@florian-hockmann.de>
AuthorDate: Mon Jul 31 12:30:46 2023 +0200
TINKERPOP-2976 Fix modification while iterating
In rare cases users could get an InvalidOperationException if the
dictionary of `_serializerByType` was modified while another thread in
parallel iterated over the keys of this dictionary.
Just creating a copy of the supported types and then iterating over that
should fix this.
The problem was reported for GraphBinary, but the GraphSONWriter had the
same problem so I also fixed it there.
---
.../IO/GraphBinary/TypeSerializerRegistry.cs | 20 ++++++++++----------
.../Structure/IO/GraphSON/GraphSONWriter.cs | 6 +++---
2 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphBinary/TypeSerializerRegistry.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphBinary/TypeSerializerRegistry.cs
index 1fc863e26d..a6fda07ee3 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphBinary/TypeSerializerRegistry.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphBinary/TypeSerializerRegistry.cs
@@ -159,11 +159,11 @@ namespace Gremlin.Net.Structure.IO.GraphBinary
/// <exception cref="InvalidOperationException">Thrown when no serializer can be found for the type.</exception>
public ITypeSerializer GetSerializerFor(Type valueType)
{
- if (_serializerByType.ContainsKey(valueType))
+ if (_serializerByType.TryGetValue(valueType, out var serializerForType))
{
- return _serializerByType[valueType];
+ return serializerForType;
}
-
+
if (IsDictionaryType(valueType, out var dictKeyType, out var dictValueType))
{
var serializerType = typeof(MapSerializer<,>).MakeGenericType(dictKeyType, dictValueType);
@@ -199,7 +199,7 @@ namespace Gremlin.Net.Structure.IO.GraphBinary
return serializer;
}
- foreach (var supportedType in _serializerByType.Keys)
+ foreach (var supportedType in new List<Type>(_serializerByType.Keys))
{
if (supportedType.IsAssignableFrom(valueType))
{
@@ -245,7 +245,7 @@ namespace Gremlin.Net.Structure.IO.GraphBinary
implementedInterface.GetGenericTypeDefinition() ==
typeof(IList<>));
}
-
+
/// <summary>
/// Gets a serializer for the given GraphBinary type.
/// </summary>
@@ -255,10 +255,10 @@ namespace Gremlin.Net.Structure.IO.GraphBinary
{
if (type == DataType.Custom)
throw new ArgumentException("Custom type serializers can not be retrieved using this method");
-
+
return _serializerByDataType[type];
}
-
+
/// <summary>
/// Gets a serializer for the given custom type name.
/// </summary>
@@ -270,14 +270,14 @@ namespace Gremlin.Net.Structure.IO.GraphBinary
? serializer
: throw new InvalidOperationException($"No serializer found for type '{typeName}'.");
}
-
+
/// <summary>
/// The builder of a <see cref="TypeSerializerRegistry"/>.
/// </summary>
public class Builder
{
private readonly List<CustomTypeRegistryEntry> _list = new List<CustomTypeRegistryEntry>();
-
+
/// <summary>
/// Creates the <see cref="TypeSerializerRegistry"/>.
/// </summary>
@@ -293,7 +293,7 @@ namespace Gremlin.Net.Structure.IO.GraphBinary
if (serializer == null) throw new ArgumentNullException(nameof(serializer));
if (serializer.TypeName == null)
throw new ArgumentException("serializer custom type name can not be null");
-
+
_list.Add(new CustomTypeRegistryEntry(type, serializer));
return this;
}
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
index e453efc792..a7d4d01128 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
@@ -134,11 +134,11 @@ namespace Gremlin.Net.Structure.IO.GraphSON
private IGraphSONSerializer TryGetSerializerFor(Type type)
{
- if (Serializers.ContainsKey(type))
+ if (Serializers.TryGetValue(type, out var serializer))
{
- return Serializers[type];
+ return serializer;
}
- foreach (var supportedType in Serializers.Keys)
+ foreach (var supportedType in new List<Type>(Serializers.Keys))
if (supportedType.IsAssignableFrom(type))
{
return Serializers[supportedType];