Fix potential race condition

This commit is contained in:
smoogipoo 2019-02-28 17:17:51 +09:00
parent 93a4229ba6
commit 951b95ff78
1 changed files with 16 additions and 1 deletions

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using osu.Framework.Configuration;
using osu.Framework.Screens;
using osu.Game.Configuration;
@ -628,10 +629,24 @@ private void loadComponentSingleFile<T>(T d, Action<T> add)
{
Logger.Log($"Loading {d}...", level: LogLevel.Debug);
// Since this is running in a separate thread, it is possible for OsuGame to be disposed after LoadComponentAsync has been called
// throwing an exception. To avoid this, the call is scheduled on the update thread, which does not run if IsDisposed = true
Task task = null;
var del = new ScheduledDelegate(() => task = LoadComponentAsync(d, add));
Scheduler.Add(del);
// The delegate won't complete if OsuGame has been disposed in the meantime
while (!IsDisposed && !del.Completed)
await Task.Delay(10);
// Either we're disposed or the load process has started successfully
if (IsDisposed)
return;
await LoadComponentAsync(d, add);
Debug.Assert(task != null);
await task;
Logger.Log($"Loaded {d}!", level: LogLevel.Debug);
}
catch (OperationCanceledException)