fix: Fixes unresponsive console input (#1720)

### Summary
Fixes an issue where the call priority of `Initialize` functions causes a dead lock because the console input handler was not initialized.
This commit is contained in:
Kamron Batman 2024-04-05 15:07:43 -07:00 committed by GitHub
parent 974062b0d4
commit 912eaab460
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 11 deletions

View file

@ -78,7 +78,7 @@ public class CallPriorityComparer : IComparer<MethodInfo>
return 0;
}
private int GetPriority(MethodInfo mi)
private static int GetPriority(MethodInfo mi)
{
var objs = mi.GetCustomAttributes(typeof(CallPriorityAttribute), true);
@ -87,12 +87,7 @@ public class CallPriorityComparer : IComparer<MethodInfo>
return 50;
}
if (objs[0] is not CallPriorityAttribute attr)
{
return 50;
}
return attr.Priority;
return (objs[0] as CallPriorityAttribute)?.Priority ?? 50;
}
}

View file

@ -25,6 +25,7 @@ public static class ConsoleInputHandler
{
private static readonly AutoResetEvent _receivedUserInput = new(false);
private static readonly AutoResetEvent _endUserInput = new(false);
private static bool _initialized;
private static bool _expectUserInput;
private static readonly Dictionary<string, ConsoleCommand> _inputCommands = new();
private static string[] _commandDescriptions;
@ -81,8 +82,11 @@ public static class ConsoleInputHandler
RegisterCommand(["help", "?"], "Displays this help screen.", DisplayHelp);
}
[CallPriority(0)]
public static void Initialize()
{
_initialized = true;
new Thread(ProcessConsoleInput)
{
IsBackground = true,
@ -179,6 +183,11 @@ public static class ConsoleInputHandler
public static string ReadLine()
{
if (!_initialized)
{
return Console.ReadLine();
}
Volatile.Write(ref _expectUserInput, true);
_receivedUserInput.WaitOne();
var line = _input;

View file

@ -339,7 +339,7 @@ public static class Core
if (!close)
{
Console.WriteLine("This exception is fatal, press return to exit");
Console.ReadLine();
ConsoleInputHandler.ReadLine();
}
Kill();