mirror of
https://github.com/ppy/osu.git
synced 2025-02-15 04:52:57 +08:00
Merge pull request #4364 from smoogipoo/fix-potential-crossthread
Fix race condition during game load + disposal
This commit is contained in:
commit
ace83da1c8
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using osu.Framework.Configuration;
|
using osu.Framework.Configuration;
|
||||||
using osu.Framework.Screens;
|
using osu.Framework.Screens;
|
||||||
using osu.Game.Configuration;
|
using osu.Game.Configuration;
|
||||||
@ -628,10 +629,24 @@ namespace osu.Game
|
|||||||
{
|
{
|
||||||
Logger.Log($"Loading {d}...", level: LogLevel.Debug);
|
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)
|
if (IsDisposed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
await LoadComponentAsync(d, add);
|
Debug.Assert(task != null);
|
||||||
|
|
||||||
|
await task;
|
||||||
|
|
||||||
Logger.Log($"Loaded {d}!", level: LogLevel.Debug);
|
Logger.Log($"Loaded {d}!", level: LogLevel.Debug);
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
|
Loading…
Reference in New Issue
Block a user