diff --git a/osu.Game/Online/Spectator/SpectatorClient.cs b/osu.Game/Online/Spectator/SpectatorClient.cs index 7afceb41a4..428a4cdbdb 100644 --- a/osu.Game/Online/Spectator/SpectatorClient.cs +++ b/osu.Game/Online/Spectator/SpectatorClient.cs @@ -221,8 +221,7 @@ namespace osu.Game.Online.Spectator protected abstract Task BeginPlayingInternal(SpectatorState state); - protected abstract Task SendFramesInternal(FrameDataBundle data); - + protected abstract Task SendFramesInternal(FrameDataBundle bundle); protected abstract Task EndPlayingInternal(SpectatorState state); protected abstract Task WatchUserInternal(int userId); @@ -281,19 +280,27 @@ namespace osu.Game.Online.Spectator private void sendNextBundleIfRequired() { + Debug.Assert(ThreadSafety.IsUpdateThread); + if (lastSend?.IsCompleted == false) return; if (!pendingFrameBundles.TryPeek(out var bundle)) return; - lastSend = SendFramesInternal(bundle); - lastSend.ContinueWith(t => Schedule(() => + TaskCompletionSource tcs = new TaskCompletionSource(); + + lastSend = tcs.Task; + + SendFramesInternal(bundle).ContinueWith(t => Schedule(() => { + bool wasSuccessful = t.Exception == null; + // If the last bundle send wasn't successful, try again without dequeuing. - if (t.IsCompletedSuccessfully) + if (wasSuccessful) pendingFrameBundles.Dequeue(); + tcs.SetResult(wasSuccessful); sendNextBundleIfRequired(); })); }