Fix deadlock from Stop/HandleCommandAsync

This commit is contained in:
TheUbMunster 2022-12-24 10:50:19 -07:00
parent fb26986a58
commit 317a495f46
1 changed files with 7 additions and 2 deletions

View File

@ -21,11 +21,16 @@ public class DiscordBot
public DiscordBot() public DiscordBot()
{ {
CommandHandler.RegisterCommand("dscrestart", _ => CommandHandler.RegisterCommand("dscrestart", _ =>
{
//Task.Run is to fix deadlock (dispose can only be finalized if all discord callbacks are returned,
//and since this delegate is called directly from a callback, it would cause a deadlock).
Task.Run(() =>
{ {
Stop(); Stop();
#pragma warning disable CS4014 #pragma warning disable CS4014
Init(); Init();
#pragma warning restore CS4014 #pragma warning restore CS4014
});
return "Restarting Discord bot..."; return "Restarting Discord bot...";
}); });
logger.Info("Starting discord bot (ctor)"); logger.Info("Starting discord bot (ctor)");