Skip to content

Commit 5f796cd

Browse files
Merge pull request #2225 from SixLabors/defect/2718
Make TestFile Image threadsafe.
2 parents ea97bac + 2515d80 commit 5f796cd

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

tests/ImageSharp.Tests/TestFile.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ public sealed class TestFile
2929
/// <summary>
3030
/// The image (lazy initialized value)
3131
/// </summary>
32-
private Image<Rgba32> image;
32+
private volatile Image<Rgba32> image;
33+
34+
/// <summary>
35+
/// Used to ensure image loading is threadsafe.
36+
/// </summary>
37+
private readonly object syncLock = new();
3338

3439
/// <summary>
3540
/// The image bytes
@@ -65,7 +70,21 @@ public sealed class TestFile
6570
/// <summary>
6671
/// Gets the image with lazy initialization.
6772
/// </summary>
68-
private Image<Rgba32> Image => this.image ??= ImageSharp.Image.Load<Rgba32>(this.Bytes);
73+
private Image<Rgba32> Image
74+
{
75+
get
76+
{
77+
if (this.image is null)
78+
{
79+
lock (this.syncLock)
80+
{
81+
this.image ??= ImageSharp.Image.Load<Rgba32>(this.Bytes);
82+
}
83+
}
84+
85+
return this.image;
86+
}
87+
}
6988

7089
/// <summary>
7190
/// Gets the input image directory.

0 commit comments

Comments
 (0)