From d8c7193eef9ff09110005d08cebbdca170b61ca4 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Mon, 15 Mar 2021 15:00:58 +0100 Subject: [PATCH] Fixed issue with multiple threads reading the appSettings file at the same time, by locking (Found when site has to upgrade) --- .../Configuration/JsonConfigManipulator.cs | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Umbraco.Infrastructure/Configuration/JsonConfigManipulator.cs b/src/Umbraco.Infrastructure/Configuration/JsonConfigManipulator.cs index 6057ce8a82..da83a7c913 100644 --- a/src/Umbraco.Infrastructure/Configuration/JsonConfigManipulator.cs +++ b/src/Umbraco.Infrastructure/Configuration/JsonConfigManipulator.cs @@ -5,12 +5,12 @@ using Microsoft.Extensions.Configuration.Json; using Microsoft.Extensions.FileProviders; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using Umbraco.Cms.Core.Configuration; namespace Umbraco.Cms.Core.Configuration { public class JsonConfigManipulator : IConfigManipulator { + private static readonly object s_locker = new object(); private readonly IConfiguration _configuration; public JsonConfigManipulator(IConfiguration configuration) @@ -162,22 +162,26 @@ namespace Umbraco.Cms.Core.Configuration token?.Parent?.Remove(); } + + private static void SaveJson(JsonConfigurationProvider provider, JObject json) { - if (provider.Source.FileProvider is PhysicalFileProvider physicalFileProvider) + lock (s_locker) { - var jsonFilePath = Path.Combine(physicalFileProvider.Root, provider.Source.Path); + if (provider.Source.FileProvider is PhysicalFileProvider physicalFileProvider) + { + var jsonFilePath = Path.Combine(physicalFileProvider.Root, provider.Source.Path); - using (var sw = new StreamWriter(jsonFilePath, false)) - using (var jsonTextWriter = new JsonTextWriter(sw) - { - Formatting = Formatting.Indented, - }) - { - json?.WriteTo(jsonTextWriter); + using (var sw = new StreamWriter(jsonFilePath, false)) + using (var jsonTextWriter = new JsonTextWriter(sw) + { + Formatting = Formatting.Indented, + }) + { + json?.WriteTo(jsonTextWriter); + } } } - } private static JObject GetJson(JsonConfigurationProvider provider)