diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index 03e59d34b2..067257132f 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Net.Http; using SixLabors.ImageSharp.Formats; @@ -78,7 +79,7 @@ public int MaxDegreeOfParallelism /// Gets a set of properties for the Congiguration. /// /// This can be used for storing global settings and defaults to be accessable to processors. - public IDictionary Properties { get; } = new Dictionary(); + public IDictionary Properties { get; } = new ConcurrentDictionary(); /// /// Gets the currently registered s. diff --git a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs index d1581a00eb..80cdfd153e 100644 --- a/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs +++ b/src/ImageSharp/GraphicOptionsDefaultsExtensions.cs @@ -71,11 +71,9 @@ public static GraphicsOptions GetGraphicsOptions(this IImageProcessingContext co return go; } - var configOptions = context.Configuration.GetGraphicsOptions(); - // do not cache the fall back to config into the the processing context // in case someone want to change the value on the config and expects it re trflow thru - return configOptions; + return context.Configuration.GetGraphicsOptions(); } /// diff --git a/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs b/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs index 4f47545fa3..7ed73d5176 100644 --- a/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs +++ b/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System.Collections.Concurrent; using System.Collections.Generic; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors; @@ -41,7 +42,7 @@ public DefaultImageProcessorContext(Configuration configuration, Image s public Configuration Configuration { get; } /// - public IDictionary Properties { get; } = new Dictionary(); + public IDictionary Properties { get; } = new ConcurrentDictionary(); /// public Image GetResultImage() diff --git a/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs b/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs index 72ec355385..21f26cd01c 100644 --- a/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs +++ b/tests/ImageSharp.Tests/GraphicOptionsDefaultsExtensionsTests.cs @@ -1,7 +1,9 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System.Threading.Tasks; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Tests.Processing; using SixLabors.ImageSharp.Tests.TestUtilities; using Xunit; @@ -168,5 +170,18 @@ public void GetDefaultOptionsFromProcessingContext_IgnoreIncorectlyTypesDictionE Assert.NotNull(options); Assert.IsType(options); } + + [Theory] + [WithBlankImages(100, 100, PixelTypes.Rgba32)] + public void CanGetGraphicsOptionsMultiThreaded(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + // Could not get fake operations to trigger #1230 so using a real image. + Parallel.For(0, 10, _ => + { + using Image image = provider.GetImage(); + image.Mutate(x => x.BackgroundColor(Color.White)); + }); + } } } diff --git a/tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs b/tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs index 5f673b9d29..03fa6dfc2e 100644 --- a/tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs +++ b/tests/ImageSharp.Tests/Processing/FakeImageOperationsProvider.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using SixLabors.ImageSharp.PixelFormats; @@ -56,7 +57,7 @@ public FakeImageOperations(Configuration configuration, Image source, bo public Configuration Configuration { get; } - public IDictionary Properties { get; } = new Dictionary(); + public IDictionary Properties { get; } = new ConcurrentDictionary(); public Image GetResultImage() {