From 4ece07d64d39b1cfb9c0ef707f8c3c11311b9770 Mon Sep 17 00:00:00 2001 From: barelyprofessional <150058423+barelyprofessional@users.noreply.github.com> Date: Thu, 27 Jun 2024 11:15:41 +0800 Subject: [PATCH] Dispose of Shuffle properly before rebuilding it as otherwise you end up with random Shuffle ping tasks waking up and trying to ping a dead connection --- KfChatDotNetKickBot/KickBot.cs | 1 + KfChatDotNetKickBot/Services/Shuffle.cs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/KfChatDotNetKickBot/KickBot.cs b/KfChatDotNetKickBot/KickBot.cs index 2a733e0..e32d16f 100644 --- a/KfChatDotNetKickBot/KickBot.cs +++ b/KfChatDotNetKickBot/KickBot.cs @@ -114,6 +114,7 @@ public class KickBot { if (e.Type == DisconnectionType.ByServer) { + _shuffle.Dispose(); BuildShuffle(); } } diff --git a/KfChatDotNetKickBot/Services/Shuffle.cs b/KfChatDotNetKickBot/Services/Shuffle.cs index a7e9317..0c62e22 100644 --- a/KfChatDotNetKickBot/Services/Shuffle.cs +++ b/KfChatDotNetKickBot/Services/Shuffle.cs @@ -8,7 +8,7 @@ using Websocket.Client; namespace KfChatDotNetKickBot.Services; -public class Shuffle +public class Shuffle : IDisposable { private Logger _logger = LogManager.GetCurrentClassLogger(); private WebsocketClient _wsClient; @@ -20,6 +20,7 @@ public class Shuffle public event OnLatestBetUpdatedEventHandler OnLatestBetUpdated; public event OnWsDisconnectionEventHandler OnWsDisconnection; private CancellationToken _cancellationToken = CancellationToken.None; + private CancellationTokenSource _pingCts = new(); public Shuffle(string? proxy = null, CancellationToken? cancellationToken = null) { @@ -79,7 +80,7 @@ public class Shuffle private async Task PeriodicPing() { using var timer = new PeriodicTimer(TimeSpan.FromSeconds(10)); - while (await timer.WaitForNextTickAsync(_cancellationToken)) + while (await timer.WaitForNextTickAsync(_pingCts.Token)) { if (_wsClient == null) { @@ -217,4 +218,11 @@ public class Shuffle } public class ShuffleUserNotFoundException : Exception; + + public void Dispose() + { + _pingCts.Cancel(); + _wsClient.Dispose(); + GC.SuppressFinalize(this); + } } \ No newline at end of file