ModernUO/Projects/Server.Tests/Tests/Console/ConsoleInputPumpTests.cs
Kamron Batman 7434ed7ee1
fix(console): stop headless servers from pegging a CPU core (#2535)
## Problem

On headless Linux deployments (systemd service, Docker without a TTY, `nohup`), the ModernUO process pegs a full CPU core even when idle. It does not reproduce on Windows because that runs with an interactive console.

## Root cause

`ConsoleInputHandler` runs a background thread (named "Console Input Handler") that loops on `Console.ReadLine()`. When stdin is **not** an interactive terminal, `Console.ReadLine()` returns `null` at end-of-stream **immediately** on every call, so the loop `continue`s in a tight spin — one core at 100%.

Reproduced in a container running the actual distribution: the "Console Input Handler" thread sat at ~90% CPU on a headless boot; with a blocking stdin it dropped to idle.

## Fix

1. **Detect headless once at startup:** `Core.Headless = Console.IsInputRedirected`.
2. **Extract a testable `ConsoleInputPump`** that owns the input stream: per line read, it *atomically* (under one lock) either delivers the line to a waiting prompt or dispatches a console command, and it **ends on EOF instead of spinning**. Cleanup runs unconditionally in a `finally`, so a pending prompt is always released (never hangs). Replaces the old `async void` loop and the fragile `_expectUserInput` / two-`AutoResetEvent` / `_input` handshake.
3. **`ConsoleInputHandler` becomes a thin headless-aware facade** over the pump. Headless: the reader thread never starts (`Console input disabled (headless: stdin is not a TTY).`), and `ReadLine()` throws a fatal `HeadlessConsoleInputException`.
4. **Data-gating and first-boot prompts** (deserialization "delete bad types? y/n", save-conflict, config/expansion setup) now route through `ConsoleInputHandler.ReadLine()`, so a headless server crashes fatal with a clear message instead of reading `null` (previously an NRE or a silent wrong branch).

Design decision (model b): headless servers are expected to be supplied with configuration/save data (including the owner account); interactive prompts when headless are fatal by design.

## Testing

- New `ConsoleInputPumpTests` (5 tests): EOF ends the loop without spinning; command dispatch; a pending prompt receives the next line; EOF while a prompt is pending completes it with `null` (no hang); a throwing command lookup does not hang a pending prompt. The tests synchronize on real pump state (no `Thread.Sleep`), so they are deterministic on slow CI.
- Full `Server.Tests`: no new failures introduced.

## End-to-end verification (Docker, real distribution)

| | Console Input Handler thread | Container CPU |
|---|---|---|
| Before fix (headless boot) | ~90% | ~199% (2 cores) |
| After fix (headless boot) | **not started** | **~11%** |

After the fix, a headless boot logs `Console input disabled (headless: stdin is not a TTY).`, loads the world normally, and idles instead of spinning.
2026-07-16 18:52:43 -07:00

139 lines
5 KiB
C#

using System;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Server;
using Xunit;
namespace Server.Tests;
// These tests exercise the pump's cross-thread rendezvous directly, so they block on
// bounded-timeout Task.Wait() calls by design rather than using async/await.
#pragma warning disable xUnit1031
public class ConsoleInputPumpTests
{
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(5);
// Deterministic synchronization: spin until a real condition holds (the pump's
// rendezvous state or the reader's progress) instead of sleeping a fixed interval,
// which races on slow/loaded CI runners.
private static void WaitUntil(Func<bool> condition, string message) =>
Assert.True(SpinWait.SpinUntil(condition, Timeout), message);
// A TextReader whose ReadLine() blocks until a line is fed, and returns null after Complete().
private sealed class BlockingTextReader : TextReader
{
private readonly BlockingCollection<string> _lines = new();
private int _readCalls;
// Number of times ReadLine() has been entered — lets a test wait until the pump's
// reader loop is blocked waiting for the next line (e.g. after consuming a command).
public int ReadCallCount => Volatile.Read(ref _readCalls);
public void Feed(string line) => _lines.Add(line);
public void Complete() => _lines.CompleteAdding();
public override string ReadLine()
{
Interlocked.Increment(ref _readCalls);
try
{
return _lines.Take();
}
catch (InvalidOperationException)
{
return null; // CompleteAdding + empty => EOF
}
}
}
[Fact]
public void Eof_ends_loop_without_spinning()
{
var pump = new ConsoleInputPump(new StringReader(""), _ => null);
var ran = Task.Run(pump.Run);
Assert.True(ran.Wait(Timeout), "Run() did not return on EOF");
Assert.False(pump.Running);
}
[Fact]
public void Recognized_line_dispatches_command()
{
var invokedWith = new TaskCompletionSource<string>();
Action<string> onPing = arg => invokedWith.TrySetResult(arg);
var pump = new ConsoleInputPump(
new StringReader("ping hello\n"),
cmd => cmd == "ping" ? onPing : null
);
Task.Run(pump.Run);
Assert.True(invokedWith.Task.Wait(Timeout), "command not dispatched");
Assert.Equal("hello", invokedWith.Task.Result);
}
[Fact]
public void Pending_prompt_receives_next_line()
{
var reader = new BlockingTextReader();
var pump = new ConsoleInputPump(reader, _ => null);
Task.Run(pump.Run);
// Register the prompt, then wait until it is actually pending before feeding a
// line — otherwise the line could be consumed as a command before registration.
var prompt = Task.Run(pump.ReadLine);
WaitUntil(() => pump.HasPendingPrompt, "prompt did not register");
reader.Feed("the answer");
Assert.True(prompt.Wait(Timeout), "prompt did not receive a line");
Assert.Equal("the answer", prompt.Result);
reader.Complete();
}
[Fact]
public void Eof_while_prompt_pending_completes_prompt_with_null()
{
var reader = new BlockingTextReader();
var pump = new ConsoleInputPump(reader, _ => null);
Task.Run(pump.Run);
var prompt = Task.Run(pump.ReadLine);
WaitUntil(() => pump.HasPendingPrompt, "prompt did not register");
reader.Complete(); // EOF while prompt is waiting
Assert.True(prompt.Wait(Timeout), "prompt hung on EOF");
Assert.Null(prompt.Result);
Assert.False(pump.Running);
}
[Fact]
public void Throwing_lookup_does_not_hang_pending_prompt()
{
var reader = new BlockingTextReader();
var pump = new ConsoleInputPump(reader, _ => throw new InvalidOperationException("boom"));
Task.Run(pump.Run);
// Wait until the reader loop is blocked on its first read, then feed a bad command.
WaitUntil(() => reader.ReadCallCount >= 1, "reader did not start");
reader.Feed("badcommand");
// Wait until the loop has consumed that command (throwing lookup swallowed) and is
// blocked waiting for the NEXT line — a second ReadLine() entry. This guarantees
// the command was dispatched (not delivered to a prompt) before we register one.
WaitUntil(() => reader.ReadCallCount >= 2, "bad command was not consumed");
var prompt = Task.Run(pump.ReadLine);
WaitUntil(() => pump.HasPendingPrompt, "prompt did not register");
reader.Complete(); // EOF while prompt is waiting
Assert.True(prompt.Wait(Timeout), "prompt hung after throwing lookup");
Assert.Null(prompt.Result);
Assert.False(pump.Running);
}
}
#pragma warning restore xUnit1031