From e1a4dab746c5e040782138c0cdfa1186bd1d7750 Mon Sep 17 00:00:00 2001 From: Noi Date: Mon, 5 Oct 2020 16:10:02 -0700 Subject: [PATCH] More thorough exception handling on dispose --- ShardInstance.cs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/ShardInstance.cs b/ShardInstance.cs index aefdbd9..c74b2ad 100644 --- a/ShardInstance.cs +++ b/ShardInstance.cs @@ -72,14 +72,24 @@ namespace BirthdayBot _background.Dispose(); try { - var logout = DiscordClient.LogoutAsync(); - logout.Wait(15000); - if (!logout.IsCompleted) Log("Instance", "Warning: Client has not yet logged out. Forcing its disposal."); + if (!DiscordClient.LogoutAsync().Wait(15000)) + Log("Instance", "Warning: Client has not yet logged out. Continuing cleanup."); } - finally + catch (Exception ex) { - DiscordClient.Dispose(); + Log("Instance", "Warning: Client threw an exception when logging out: " + ex.Message); } + try + { + if (!DiscordClient.StopAsync().Wait(5000)) + Log("Instance", "Warning: Client has not yet stopped. Continuing cleanup."); + } + catch (Exception ex) + { + Log("Instance", "Warning: Client threw an exception when stopping: " + ex.Message); + } + + DiscordClient.Dispose(); } public void Log(string source, string message) => Program.Log($"Shard {ShardId:00}] [{source}", message);