csharp icon indicating copy to clipboard operation
csharp copied to clipboard

YamlDotNot is not thread-safe

Open nathanwoctopusdeploy opened this issue 1 year ago • 5 comments

This PR demonstrates that KubernetesClientConfiguration.LoadKubeConfig is not thread-safe due to YamlDotNot Deserializers.

   Exception during deserialization
   
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.ValueDeserializers.AliasValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)
   at YamlDotNet.Serialization.Deserializer.Deserialize(IParser parser, Type type)
   at YamlDotNet.Serialization.Deserializer.Deserialize[T](IParser parser)
   at k8s.KubernetesYaml.Deserialize[TValue](String yaml, Boolean strict) in F:\Code\kubernetes-client\src\KubernetesClient\KubernetesYaml.cs:line 207
   at k8s.KubernetesYaml.<LoadFromStreamAsync>d__11`1.MoveNext() in F:\Code\kubernetes-client\src\KubernetesClient\KubernetesYaml.cs:line 181
   at k8s.KubernetesClientConfiguration.<LoadKubeConfigAsync>d__31.MoveNext() in F:\Code\kubernetes-client\src\KubernetesClient\KubernetesClientConfiguration.ConfigFile.cs:line 639
   at k8s.KubernetesClientConfiguration.<LoadKubeConfigAsync>d__36.MoveNext() in F:\Code\kubernetes-client\src\KubernetesClient\KubernetesClientConfiguration.ConfigFile.cs:line 713
   at k8s.KubernetesClientConfiguration.LoadKubeConfig(FileInfo[] kubeConfigs, Boolean useRelativePaths) in F:\Code\kubernetes-client\src\KubernetesClient\KubernetesClientConfiguration.ConfigFile.cs:line 695
   at k8s.Tests.KubernetesClientConfigurationTests.<>c__DisplayClass48_0.<LoadKubeConfigShouldBeThreadSafe>g__LoadKubeConfig|3() in F:\Code\kubernetes-client\tests\KubernetesClient.Tests\KubernetesClientConfigurationTests.cs:line 691

   # INNER EXCEPTION

   Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.

      at System.ThrowHelper.ThrowInvalidOperationException_ConcurrentOperationsNotSupported()
   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)

nathanwoctopusdeploy avatar Mar 05 '24 06:03 nathanwoctopusdeploy

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: nathanwoctopusdeploy Once this PR has been reviewed and has the lgtm label, please assign tg123 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment Approvers can cancel approval by writing /approve cancel in a comment

k8s-ci-robot avatar Mar 05 '24 06:03 k8s-ci-robot

https://github.com/kubernetes-client/csharp/issues/1537

nathanwoctopusdeploy avatar Mar 05 '24 07:03 nathanwoctopusdeploy

oh seems some state inside what about a cheap solution by adding a lock?

tg123 avatar Mar 05 '24 09:03 tg123

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

:exclamation: No coverage uploaded for pull request base (master@f78a516). Click here to learn what that means.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #1536   +/-   ##
=========================================
  Coverage          ?   62.07%           
=========================================
  Files             ?      102           
  Lines             ?     3014           
  Branches          ?      636           
=========================================
  Hits              ?     1871           
  Misses            ?     1143           
  Partials          ?        0           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov-commenter avatar Mar 05 '24 10:03 codecov-commenter

oh seems some state inside what about a cheap solution by adding a lock?

Hi @tg123

I'm not familiar enough with the code base and YamlDotNet library to know what the best direction would be to fix this. The PR was created to provide an easy reproduction of the issue.

nathanwoctopusdeploy avatar Mar 07 '24 23:03 nathanwoctopusdeploy