1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 22:07:25 +08:00

Merge pull request #4364 from smoogipoo/fix-potential-crossthread

Fix race condition during game load + disposal
This commit is contained in:
Dean Herbert 2019-02-28 20:06:22 +09:00 committed by GitHub
commit ace83da1c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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 @@ namespace osu.Game
{
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)