1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 10:52:53 +08:00

Merge pull request #25931 from peppy/fix-thread-safety

Fix thread safety of `OnlineMetadataClient.UserStates`
This commit is contained in:
Bartłomiej Dach 2023-12-20 12:55:31 +01:00 committed by GitHub
commit 83e885192b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,8 +97,11 @@ namespace osu.Game.Online.Metadata
{
if (!connected.NewValue)
{
isWatchingUserPresence.Value = false;
userStates.Clear();
Schedule(() =>
{
isWatchingUserPresence.Value = false;
userStates.Clear();
});
return;
}
@ -187,13 +190,13 @@ namespace osu.Game.Online.Metadata
public override Task UserPresenceUpdated(int userId, UserPresence? presence)
{
lock (userStates)
Schedule(() =>
{
if (presence != null)
userStates[userId] = presence.Value;
else
userStates.Remove(userId);
}
});
return Task.CompletedTask;
}
@ -205,7 +208,7 @@ namespace osu.Game.Online.Metadata
Debug.Assert(connection != null);
await connection.InvokeAsync(nameof(IMetadataServer.BeginWatchingUserPresence)).ConfigureAwait(false);
isWatchingUserPresence.Value = true;
Schedule(() => isWatchingUserPresence.Value = true);
}
public override async Task EndWatchingUserPresence()
@ -215,14 +218,14 @@ namespace osu.Game.Online.Metadata
if (connector?.IsConnected.Value != true)
throw new OperationCanceledException();
// must happen synchronously before any remote calls to avoid misordering.
userStates.Clear();
// must be scheduled before any remote calls to avoid mis-ordering.
Schedule(() => userStates.Clear());
Debug.Assert(connection != null);
await connection.InvokeAsync(nameof(IMetadataServer.EndWatchingUserPresence)).ConfigureAwait(false);
}
finally
{
isWatchingUserPresence.Value = false;
Schedule(() => isWatchingUserPresence.Value = false);
}
}