csharp icon indicating copy to clipboard operation
csharp copied to clipboard

KubernetesClientConfiguration.LoadKubeConfig is not thread-safe due to YamlDotNet Deserializer

Open nathanwoctopusdeploy opened this issue 11 months ago • 0 comments

Describe the bug

KubernetesClientConfiguration.LoadKubeConfig is not thread-safe due to YamlDotNet Deserializer being used as a static

PR with a test showing the issue https://github.com/kubernetes-client/csharp/pull/1536

   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)

Kubernetes C# SDK Client Version master

Server Kubernetes Version n/a

Dotnet Runtime Version n/a

To Reproduce

Call KubernetesClientConfiguration.LoadKubeConfig from multiple threads concurrently

Expected behavior

KubernetesClientConfiguration.LoadKubeConfig should be thread-safe

KubeConfig If applicable, add a KubeConfig file with secrets redacted.

Where do you run your app with Kubernetes SDK (please complete the following information):

  • OS: Linux
  • Environment: Container
  • Cloud: Azure

Additional context

n/a

nathanwoctopusdeploy avatar Mar 05 '24 07:03 nathanwoctopusdeploy