diff --git a/tests/ImageSharp.Tests/TestFile.cs b/tests/ImageSharp.Tests/TestFile.cs index 730a5e91d..4429e93da 100644 --- a/tests/ImageSharp.Tests/TestFile.cs +++ b/tests/ImageSharp.Tests/TestFile.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Concurrent; using System.IO; -using System.Threading; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.PixelFormats; @@ -32,6 +31,11 @@ namespace SixLabors.ImageSharp.Tests /// private volatile Image image; + /// + /// Used to ensure image loading is threadsafe. + /// + private readonly object syncLock; + /// /// The image bytes /// @@ -70,22 +74,15 @@ namespace SixLabors.ImageSharp.Tests { get { - Image img = this.image; - if (img is null) + if (this.image is null) { - Image loadedImg = ImageSharp.Image.Load(this.Bytes); - img = Interlocked.CompareExchange(location1: ref this.image, value: loadedImg, comparand: null); - if (img is not null) - { - loadedImg.Dispose(); - } - else + lock (this.syncLock) { - img = loadedImg; + this.image ??= ImageSharp.Image.Load(this.Bytes); } } - return img; + return this.image; } }