mirror of https://github.com/ppy/osu
Fix potential race condition
This commit is contained in:
parent
93a4229ba6
commit
951b95ff78
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue