From 89a92ab7eb15c86130a7127aaa7e79b75c48d0dd Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 6 Sep 2018 13:27:53 +0900 Subject: [PATCH] Fix potential thread-safety issue --- osu.Game/Beatmaps/WorkingBeatmap.cs | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/osu.Game/Beatmaps/WorkingBeatmap.cs b/osu.Game/Beatmaps/WorkingBeatmap.cs index 85b98413e5..a2b44aab52 100644 --- a/osu.Game/Beatmaps/WorkingBeatmap.cs +++ b/osu.Game/Beatmaps/WorkingBeatmap.cs @@ -199,7 +199,7 @@ namespace osu.Game.Beatmaps private readonly Func valueFactory; private readonly Func stillValidFunction; - private readonly object initLock = new object(); + private readonly object fetchLock = new object(); public RecyclableLazy(Func valueFactory, Func stillValidFunction = null) { @@ -223,29 +223,17 @@ namespace osu.Game.Beatmaps { get { - recreateIfInvalid(); - return lazy.Value; + lock (fetchLock) + { + if (!stillValid) + recreate(); + return lazy.Value; + } } } private bool stillValid => lazy.IsValueCreated && (stillValidFunction?.Invoke(lazy.Value) ?? true); - private void recreateIfInvalid() - { - lock (initLock) - { - if (!lazy.IsValueCreated) - // we have not yet been initialised or haven't run the task. - return; - - if (stillValid) - // we are still in a valid state. - return; - - recreate(); - } - } - private void recreate() => lazy = new Lazy(valueFactory, LazyThreadSafetyMode.ExecutionAndPublication); } }