From e9d32fca18151036b7d2ba95fc7bae8f771671d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Wed, 28 Dec 2022 07:28:18 +0100 Subject: [PATCH 1/4] Fix various failures in initial statistics fetch - If the local user is restricted, then attempting to fetch their data from the `/users` endpoint would result in an empty response. - Even if the user was successfully fetched, their `RulesetsStatistics` may not be populated (and instead be `null`). Curiously this was not picked up by static analysis until the first issue was fixed. Closes #21839. --- osu.Game/Online/Solo/SoloStatisticsWatcher.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/osu.Game/Online/Solo/SoloStatisticsWatcher.cs b/osu.Game/Online/Solo/SoloStatisticsWatcher.cs index 33344044b9ab..b2f371fd7494 100644 --- a/osu.Game/Online/Solo/SoloStatisticsWatcher.cs +++ b/osu.Game/Online/Solo/SoloStatisticsWatcher.cs @@ -75,13 +75,25 @@ private void onUserChanged(APIUser? localUser) => Schedule(() => return; var userRequest = new GetUsersRequest(new[] { localUser.OnlineID }); - userRequest.Success += response => Schedule(() => + userRequest.Success += initialiseUserStatistics; + api.Queue(userRequest); + }); + + private void initialiseUserStatistics(GetUsersResponse response) => Schedule(() => + { + var user = response.Users.SingleOrDefault(); + + // possible if the user is restricted or similar. + if (user == null) + return; + + latestStatistics = new Dictionary(); + + if (user.RulesetsStatistics != null) { - latestStatistics = new Dictionary(); - foreach (var rulesetStats in response.Users.Single().RulesetsStatistics) + foreach (var rulesetStats in user.RulesetsStatistics) latestStatistics.Add(rulesetStats.Key, rulesetStats.Value); - }); - api.Queue(userRequest); + } }); private void userScoreProcessed(int userId, long scoreId) From a0a26b1e8c257b40dc66ad62be754afcd8a7edda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Wed, 28 Dec 2022 07:37:52 +0100 Subject: [PATCH 2/4] Ignore statistics update subscriptions with invalid score ID If score submission fails, the score will not receive a correct online ID from web, but will still be passed on to the solo statistics watcher on the results screen. This could lead to the watcher subscribing to changes with score ID equal to the default of -1. If this happened more than once, that would cause a crash due to duplicate keys in the `callbacks` dictionary. Closes #21837. --- osu.Game/Online/Solo/SoloStatisticsWatcher.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Online/Solo/SoloStatisticsWatcher.cs b/osu.Game/Online/Solo/SoloStatisticsWatcher.cs index b2f371fd7494..235abde068f1 100644 --- a/osu.Game/Online/Solo/SoloStatisticsWatcher.cs +++ b/osu.Game/Online/Solo/SoloStatisticsWatcher.cs @@ -51,7 +51,7 @@ public void RegisterForStatisticsUpdateAfter(ScoreInfo score, Action Date: Wed, 28 Dec 2022 07:49:09 +0100 Subject: [PATCH 3/4] Convert `SoloResultsScreen` to NRT --- osu.Game/Screens/Ranking/SoloResultsScreen.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/osu.Game/Screens/Ranking/SoloResultsScreen.cs b/osu.Game/Screens/Ranking/SoloResultsScreen.cs index 110e813e049b..94d333c2da5d 100644 --- a/osu.Game/Screens/Ranking/SoloResultsScreen.cs +++ b/osu.Game/Screens/Ranking/SoloResultsScreen.cs @@ -1,8 +1,6 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -#nullable disable - using System; using System.Collections.Generic; using System.Linq; @@ -26,15 +24,15 @@ public partial class SoloResultsScreen : ResultsScreen /// public bool ShowUserStatistics { get; init; } - private GetScoresRequest getScoreRequest; + private GetScoresRequest? getScoreRequest; [Resolved] - private RulesetStore rulesets { get; set; } + private RulesetStore rulesets { get; set; } = null!; [Resolved] - private SoloStatisticsWatcher soloStatisticsWatcher { get; set; } + private SoloStatisticsWatcher soloStatisticsWatcher { get; set; } = null!; - private readonly Bindable statisticsUpdate = new Bindable(); + private readonly Bindable statisticsUpdate = new Bindable(); public SoloResultsScreen(ScoreInfo score, bool allowRetry) : base(score, allowRetry) @@ -62,7 +60,7 @@ protected override StatisticsPanel CreateStatisticsPanel() return base.CreateStatisticsPanel(); } - protected override APIRequest FetchScores(Action> scoresCallback) + protected override APIRequest? FetchScores(Action>? scoresCallback) { if (Score.BeatmapInfo.OnlineID <= 0 || Score.BeatmapInfo.Status <= BeatmapOnlineStatus.Pending) return null; From 3c0b8af8f1dbf19dbd9496c85d3555354fff7e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Wed, 28 Dec 2022 07:50:10 +0100 Subject: [PATCH 4/4] Allow unsubscribing from solo statistics updates This is more of a safety item. To avoid potential duplicate key in dictionary errors (and also avoid being slightly memory-leaky), allow `SoloStatisticsWatcher` consumers to dispose of the subscriptions they take out. --- .../Online/TestSceneSoloStatisticsWatcher.cs | 24 +++++++++++++- osu.Game/Online/Solo/SoloStatisticsWatcher.cs | 32 +++++++++++-------- osu.Game/Screens/Ranking/SoloResultsScreen.cs | 4 ++- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/osu.Game.Tests/Visual/Online/TestSceneSoloStatisticsWatcher.cs b/osu.Game.Tests/Visual/Online/TestSceneSoloStatisticsWatcher.cs index b1badc628220..e62e53bd0219 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneSoloStatisticsWatcher.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneSoloStatisticsWatcher.cs @@ -35,6 +35,8 @@ public partial class TestSceneSoloStatisticsWatcher : OsuTestScene private Action? handleGetUsersRequest; private Action? handleGetUserRequest; + private IDisposable? subscription; + private readonly Dictionary<(int userId, string rulesetName), UserStatistics> serverSideStatistics = new Dictionary<(int userId, string rulesetName), UserStatistics>(); [SetUpSteps] @@ -246,6 +248,26 @@ public void TestIgnoredScoreUpdateIsMergedIntoNextOne() AddAssert("values after are correct", () => update!.After.TotalScore, () => Is.EqualTo(6_000_000)); } + [Test] + public void TestStatisticsUpdateNotFiredAfterSubscriptionDisposal() + { + int userId = getUserId(); + setUpUser(userId); + + long scoreId = getScoreId(); + var ruleset = new OsuRuleset().RulesetInfo; + + SoloStatisticsUpdate? update = null; + registerForUpdates(scoreId, ruleset, receivedUpdate => update = receivedUpdate); + AddStep("unsubscribe", () => subscription!.Dispose()); + + feignScoreProcessing(userId, ruleset, 5_000_000); + + AddStep("signal score processed", () => ((ISpectatorClient)spectatorClient).UserScoreProcessed(userId, scoreId)); + AddWaitStep("wait a bit", 5); + AddAssert("update not received", () => update == null); + } + private int nextUserId = 2000; private long nextScoreId = 50000; @@ -266,7 +288,7 @@ private void setUpUser(int userId) } private void registerForUpdates(long scoreId, RulesetInfo rulesetInfo, Action onUpdateReady) => - AddStep("register for updates", () => watcher.RegisterForStatisticsUpdateAfter( + AddStep("register for updates", () => subscription = watcher.RegisterForStatisticsUpdateAfter( new ScoreInfo(Beatmap.Value.BeatmapInfo, new OsuRuleset().RulesetInfo, new RealmUser()) { Ruleset = rulesetInfo, diff --git a/osu.Game/Online/Solo/SoloStatisticsWatcher.cs b/osu.Game/Online/Solo/SoloStatisticsWatcher.cs index 235abde068f1..46449fea7307 100644 --- a/osu.Game/Online/Solo/SoloStatisticsWatcher.cs +++ b/osu.Game/Online/Solo/SoloStatisticsWatcher.cs @@ -46,24 +46,30 @@ protected override void LoadComplete() /// /// The score to listen for the statistics update for. /// The callback to be invoked once the statistics update has been prepared. - public void RegisterForStatisticsUpdateAfter(ScoreInfo score, Action onUpdateReady) => Schedule(() => + /// An representing the subscription. Disposing it is equivalent to unsubscribing from future notifications. + public IDisposable RegisterForStatisticsUpdateAfter(ScoreInfo score, Action onUpdateReady) { - if (!api.IsLoggedIn) - return; + Schedule(() => + { + if (!api.IsLoggedIn) + return; - if (!score.Ruleset.IsLegacyRuleset() || score.OnlineID <= 0) - return; + if (!score.Ruleset.IsLegacyRuleset() || score.OnlineID <= 0) + return; - var callback = new StatisticsUpdateCallback(score, onUpdateReady); + var callback = new StatisticsUpdateCallback(score, onUpdateReady); - if (lastProcessedScoreId == score.OnlineID) - { - requestStatisticsUpdate(api.LocalUser.Value.Id, callback); - return; - } + if (lastProcessedScoreId == score.OnlineID) + { + requestStatisticsUpdate(api.LocalUser.Value.Id, callback); + return; + } - callbacks.Add(score.OnlineID, callback); - }); + callbacks.Add(score.OnlineID, callback); + }); + + return new InvokeOnDisposal(() => Schedule(() => callbacks.Remove(score.OnlineID))); + } private void onUserChanged(APIUser? localUser) => Schedule(() => { diff --git a/osu.Game/Screens/Ranking/SoloResultsScreen.cs b/osu.Game/Screens/Ranking/SoloResultsScreen.cs index 94d333c2da5d..c8920a734d6d 100644 --- a/osu.Game/Screens/Ranking/SoloResultsScreen.cs +++ b/osu.Game/Screens/Ranking/SoloResultsScreen.cs @@ -32,6 +32,7 @@ public partial class SoloResultsScreen : ResultsScreen [Resolved] private SoloStatisticsWatcher soloStatisticsWatcher { get; set; } = null!; + private IDisposable? statisticsSubscription; private readonly Bindable statisticsUpdate = new Bindable(); public SoloResultsScreen(ScoreInfo score, bool allowRetry) @@ -44,7 +45,7 @@ protected override void LoadComplete() base.LoadComplete(); if (ShowUserStatistics) - soloStatisticsWatcher.RegisterForStatisticsUpdateAfter(Score, update => statisticsUpdate.Value = update); + statisticsSubscription = soloStatisticsWatcher.RegisterForStatisticsUpdateAfter(Score, update => statisticsUpdate.Value = update); } protected override StatisticsPanel CreateStatisticsPanel() @@ -75,6 +76,7 @@ protected override void Dispose(bool isDisposing) base.Dispose(isDisposing); getScoreRequest?.Cancel(); + statisticsSubscription?.Dispose(); } } }