Skip to content

Commit

Permalink
fix: Fixes unresponsive console input (#1720)
Browse files Browse the repository at this point in the history
### Summary
Fixes an issue where the call priority of `Initialize` functions causes a dead lock because the console input handler was not initialized.
  • Loading branch information
kamronbatman committed Apr 5, 2024
1 parent 974062b commit 912eaab
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
9 changes: 2 additions & 7 deletions Projects/Server/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public int Compare(MethodInfo x, MethodInfo y)
return 0;
}

private int GetPriority(MethodInfo mi)
private static int GetPriority(MethodInfo mi)
{
var objs = mi.GetCustomAttributes(typeof(CallPriorityAttribute), true);

Expand All @@ -87,12 +87,7 @@ private int GetPriority(MethodInfo mi)
return 50;
}

if (objs[0] is not CallPriorityAttribute attr)
{
return 50;
}

return attr.Priority;
return (objs[0] as CallPriorityAttribute)?.Priority ?? 50;
}
}

Expand Down
9 changes: 9 additions & 0 deletions Projects/Server/Console/ConsoleInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,8 +82,11 @@ public static void Configure()
RegisterCommand(["help", "?"], "Displays this help screen.", DisplayHelp);
}

[CallPriority(0)]
public static void Initialize()
{
_initialized = true;

new Thread(ProcessConsoleInput)
{
IsBackground = true,
Expand Down Expand Up @@ -179,6 +183,11 @@ private static async void ProcessConsoleInput()

public static string ReadLine()
{
if (!_initialized)
{
return Console.ReadLine();
}

Volatile.Write(ref _expectUserInput, true);
_receivedUserInput.WaitOne();
var line = _input;
Expand Down
2 changes: 1 addition & 1 deletion Projects/Server/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private static void CurrentDomain_UnhandledException(object sender, UnhandledExc
if (!close)
{
Console.WriteLine("This exception is fatal, press return to exit");
Console.ReadLine();
ConsoleInputHandler.ReadLine();
}

Kill();
Expand Down
6 changes: 3 additions & 3 deletions Projects/UOContent/Misc/AccountPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ public static void Initialize()
Console.WriteLine("This server has no accounts.");
Console.Write("Do you want to create the owner account now? (y/n): ");

var answer = Console.ReadLine();
var answer = ConsoleInputHandler.ReadLine();
if (answer.InsensitiveEquals("y"))
{
Console.WriteLine();

Console.Write("Username: ");
var username = Console.ReadLine();
var username = ConsoleInputHandler.ReadLine();

Console.Write("Password: ");
var password = Console.ReadLine();
var password = ConsoleInputHandler.ReadLine();

var a = new Account(username, password)
{
Expand Down

0 comments on commit 912eaab

Please sign in to comment.