Let bootstrap shutdown exit standalone server#930
Conversation
|
fix, 1.6, standalone server |
There was a problem hiding this comment.
Pull request overview
Makes the standalone server's main loop responsive to server.running becoming false (e.g., after bootstrap writes save.zip and requests shutdown), so the process exits cleanly instead of blocking forever inside Console.ReadLine(). The fix replaces the blocking read with a polling loop driven by Console.KeyAvailable and Thread.Sleep(50).
Changes:
- Add
using System.Threading;. - Wrap
Console.ReadLine()in aConsole.KeyAvailablecheck so the loop can re-evaluateserver.runningperiodically. - Sleep 50 ms between polls when no input is pending.
Comments suppressed due to low confidence (1)
Source/Server/Server.cs:134
Console.KeyAvailablethrowsInvalidOperationExceptionwhenever standard input is redirected (e.g., the server is run with piped stdin, launched as a background/detached process, run under a service manager, or under a CI/test harness). The previousConsole.ReadLine()worked in those scenarios — it simply blocked or returnednullon EOF. As written, this loop will crash on the very first iteration in any non-interactive deployment, which is a regression for headless server use.
Consider gating the polling on Console.IsInputRedirected (falling back to a blocking read, or skipping console command handling entirely) so redirected/non-interactive stdin doesn't break the main loop. A blocking ReadLine on a dedicated input thread that enqueues commands is another common pattern that avoids both the busy-wait and the redirection problem.
if (Console.KeyAvailable)
{
var cmd = Console.ReadLine();
if (cmd != null)
server.Enqueue(() => server.HandleChatCmd(consoleSource, cmd));
if (cmd == stopCmd)
break;
}
else
{
Thread.Sleep(50);
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| while (server.running) | ||
| { | ||
| var cmd = Console.ReadLine(); | ||
| if (cmd != null) | ||
| server.Enqueue(() => server.HandleChatCmd(consoleSource, cmd)); | ||
| if (Console.KeyAvailable) | ||
| { | ||
| var cmd = Console.ReadLine(); | ||
| if (cmd != null) | ||
| server.Enqueue(() => server.HandleChatCmd(consoleSource, cmd)); | ||
|
|
||
| if (cmd == stopCmd) | ||
| break; | ||
| if (cmd == stopCmd) | ||
| break; | ||
| } | ||
| else | ||
| { | ||
| Thread.Sleep(50); | ||
| } | ||
| } |
mibac138
left a comment
There was a problem hiding this comment.
ideally we'd probably want to read the console in another thread and the main thread can then only block until the server is not running anymore, but this is fine for a simple fix
Summary
Testing