From 51810f27ad1cf284813706f18c7eafd11e0c13ac Mon Sep 17 00:00:00 2001 From: Florian Bernd Date: Tue, 19 Aug 2025 10:14:01 +0200 Subject: [PATCH] Fix race condition in `GetOrCreateBoundConfiguration` --- src/Elastic.Transport/DistributedTransport.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Elastic.Transport/DistributedTransport.cs b/src/Elastic.Transport/DistributedTransport.cs index 018ca33..99373b4 100644 --- a/src/Elastic.Transport/DistributedTransport.cs +++ b/src/Elastic.Transport/DistributedTransport.cs @@ -305,13 +305,20 @@ BoundConfiguration GetOrCreateBoundConfiguration(RequestConfiguration rc) return boundConfiguration; } +#if NET8_0_OR_GREATER boundConfiguration = new BoundConfiguration(Configuration, rc); -#if NET8_0_OR_GREATER cache.TryAdd(rc, boundConfiguration); #else lock (cache) { + if (cache.TryGetValue(rc, out boundConfiguration)) + { + return boundConfiguration; + } + + boundConfiguration = new BoundConfiguration(Configuration, rc); + cache.Add(rc, boundConfiguration); } #endif