mirror of
https://github.com/ppy/osu.git
synced 2025-01-28 08:55:35 +08:00
Merge pull request #18849 from peppy/fix-another-test-deadlock
Fix test deadlock due to TPL threadpool saturation
This commit is contained in:
commit
0bd230217b
@ -4,14 +4,14 @@
|
||||
#nullable disable
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Game.Database;
|
||||
using osu.Framework.Logging;
|
||||
using osu.Game.Beatmaps;
|
||||
using osu.Game.Database;
|
||||
using osu.Game.Online.API;
|
||||
using osu.Game.Online.Rooms;
|
||||
using osu.Game.Screens.OnlinePlay;
|
||||
@ -72,22 +72,18 @@ namespace osu.Game.Tests.Visual.OnlinePlay
|
||||
|
||||
((DummyAPIAccess)API).HandleRequest = request =>
|
||||
{
|
||||
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
|
||||
|
||||
// Because some of the handlers use realm, we need to ensure the game is still alive when firing.
|
||||
// If we don't, a stray `PerformAsync` could hit an `ObjectDisposedException` if running too late.
|
||||
Scheduler.Add(() =>
|
||||
try
|
||||
{
|
||||
bool result = handler.HandleRequest(request, API.LocalUser.Value, beatmapManager);
|
||||
tcs.SetResult(result);
|
||||
}, false);
|
||||
|
||||
#pragma warning disable RS0030
|
||||
// We can't GetResultSafely() here (will fail with "Can't use GetResultSafely from inside an async operation."), but Wait is safe enough due to
|
||||
// the task being a TaskCompletionSource.
|
||||
// Importantly, this doesn't deadlock because of the scheduler call above running inline where feasible (see the `false` argument).
|
||||
return tcs.Task.Result;
|
||||
#pragma warning restore RS0030
|
||||
return handler.HandleRequest(request, API.LocalUser.Value, beatmapManager);
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// These requests can be fired asynchronously, but potentially arrive after game components
|
||||
// have been disposed (ie. realm in BeatmapManager).
|
||||
// This only happens in tests and it's easiest to ignore them for now.
|
||||
Logger.Log($"Handled {nameof(ObjectDisposedException)} in test request handling");
|
||||
return true;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user