GameWindow.Dispose() (via Program.cs's `using var window = ...`) runs
unconditionally even when invoked mid-unwind of an exception that escaped
Run()'s Silk.NET frame loop. Resource teardown itself can converge
cleanly regardless, so CompleteShutdown had no way to tell "normal Run()
return" from "a crash is propagating through me right now" and always
wrote the hardcoded exited{code:0,reason:"graceful"} — exactly the
symptom #406 observed against a real 0xE0434352 crash. Fixed by latching
_runFailure in Run()'s existing catch block (before the pre-existing
throw) and consulting it from a new ReportExited method, the one call
site for the terminal status write: crashed(1)/graceful(0)/
shutdown-incomplete(1) as appropriate. No wire-contract amendment needed
— §LA1 pins the exited event NAME, and reason is already free text that
StatusEventParser round-trips unchanged.
Sibling gap fixed in the same commit: the launcher discarded the child's
stdout/stderr entirely, which is why diagnosing this exact crash required
a manual console re-run. Added BoundedProcessOutputCapture, a 2 MiB-capped
sink mirroring SessionStatusWriter's open-append-flush-close-per-write
posture (a long-lived write handle is not actually concurrently readable
on Windows even with FileShare.Read — confirmed by isolated repro), wired
into both SystemChildProcess (ProcessStartInfo.RedirectStandardError;
Linux + Windows graphical children, i.e. this bug's own scenario) and
WindowsSystemChildProcess (a real native pipe via CreateChildOutputPipe,
mirroring the existing stdin pipe; Windows console-capable/Headless
children). Opt-in via LauncherProcessSpec.StderrLogPath (null = unchanged
behavior), threaded through SessionConfigComposer -> client.err.log
beside status.jsonl -> LauncherExecutableSet -> LauncherOrchestrator.
Tests: GameWindowCrashStatusTests (source-shape, matching the existing
GameWindow test pattern — the class cannot be constructed without a live
GPU/window), BoundedProcessOutputCaptureTests (10 unit tests), and three
new LauncherProcessSupervisorTests spawning real child processes through
both capture code paths.
Launcher.Core.Tests: 337/0 (was 324/0). Launcher.Tests: 67/0 (unchanged).
Full solution build green.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
240 lines
6.9 KiB
C#
240 lines
6.9 KiB
C#
using System.Text;
|
|
using AcDream.Launcher.Core.Launching;
|
|
|
|
namespace AcDream.Launcher.Core.Tests.Launching;
|
|
|
|
/// <summary>Fix #406 sibling gap: the launcher previously discarded a
|
|
/// supervised child's stderr entirely, so diagnosing a crash (including
|
|
/// exactly the #406 crash) required re-running the identical binary by
|
|
/// hand. These tests cover <see cref="BoundedProcessOutputCapture"/> in
|
|
/// isolation — the real-child-process end-to-end capture tests live in
|
|
/// <c>LauncherProcessSupervisorTests</c> alongside the existing real-process
|
|
/// coverage.</summary>
|
|
public sealed class BoundedProcessOutputCaptureTests
|
|
{
|
|
[Fact]
|
|
public void AppendLineWritesEachLineWithATrailingNewline()
|
|
{
|
|
string path = TempPath();
|
|
try
|
|
{
|
|
using var capture = new BoundedProcessOutputCapture(path);
|
|
|
|
capture.AppendLine("first");
|
|
capture.AppendLine("second");
|
|
capture.Dispose();
|
|
|
|
Assert.Equal("first\nsecond\n", File.ReadAllText(path));
|
|
}
|
|
finally
|
|
{
|
|
TryDelete(path);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ANullLineFromTheEndOfStreamSentinelIsANoOp()
|
|
{
|
|
string path = TempPath();
|
|
try
|
|
{
|
|
using var capture = new BoundedProcessOutputCapture(path);
|
|
|
|
capture.AppendLine("kept");
|
|
capture.AppendLine(null);
|
|
capture.Dispose();
|
|
|
|
Assert.Equal("kept\n", File.ReadAllText(path));
|
|
}
|
|
finally
|
|
{
|
|
TryDelete(path);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void WritesBeyondTheCapAreDroppedAndAOneTimeTruncationMarkerIsAppended()
|
|
{
|
|
string path = TempPath();
|
|
try
|
|
{
|
|
using var capture = new BoundedProcessOutputCapture(path, maxBytes: 16);
|
|
|
|
capture.AppendLine("0123456789"); // 11 bytes incl. newline
|
|
capture.AppendLine("this line is dropped entirely");
|
|
capture.AppendLine("so is this one");
|
|
|
|
Assert.True(capture.IsDone);
|
|
string written = File.ReadAllText(path);
|
|
Assert.StartsWith("0123456789\n", written, StringComparison.Ordinal);
|
|
Assert.Contains("truncated at 16 bytes", written, StringComparison.Ordinal);
|
|
// The cap is a hard ceiling: nothing past it EVER lands on disk,
|
|
// even the marker's own text does not push the file arbitrarily
|
|
// far past the configured bound.
|
|
Assert.True(
|
|
written.Length < 200,
|
|
$"expected a small bounded file, got {written.Length} bytes");
|
|
}
|
|
finally
|
|
{
|
|
TryDelete(path);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ALogSpammingChildCannotGrowTheFileUnboundedly()
|
|
{
|
|
string path = TempPath();
|
|
try
|
|
{
|
|
using var capture = new BoundedProcessOutputCapture(
|
|
path,
|
|
maxBytes: BoundedProcessOutputCapture.DefaultMaxBytes);
|
|
|
|
// Far more than the 2 MiB default cap.
|
|
string spamLine = new('x', 4096);
|
|
for (int i = 0; i < 4096; i++)
|
|
{
|
|
capture.AppendLine(spamLine);
|
|
if (capture.IsDone)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
Assert.True(capture.IsDone);
|
|
long fileLength = new FileInfo(path).Length;
|
|
Assert.True(
|
|
fileLength < BoundedProcessOutputCapture.DefaultMaxBytes + 256,
|
|
$"expected the file to stay near the {BoundedProcessOutputCapture.DefaultMaxBytes}-byte "
|
|
+ $"cap, got {fileLength} bytes");
|
|
}
|
|
finally
|
|
{
|
|
TryDelete(path);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void AppendCreatesTheSessionDirectoryOnFirstWrite()
|
|
{
|
|
string directory = Path.Combine(
|
|
Path.GetTempPath(),
|
|
"acdream-406-capture-" + Guid.NewGuid().ToString("N"));
|
|
string path = Path.Combine(directory, "client.err.log");
|
|
Assert.False(Directory.Exists(directory));
|
|
|
|
try
|
|
{
|
|
using var capture = new BoundedProcessOutputCapture(path);
|
|
capture.AppendLine("hello");
|
|
capture.Dispose();
|
|
|
|
Assert.True(File.Exists(path));
|
|
}
|
|
finally
|
|
{
|
|
try
|
|
{
|
|
Directory.Delete(directory, recursive: true);
|
|
}
|
|
catch (IOException)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void RawByteAppendsAreConcatenatedWithoutAnImpliedLineBoundary()
|
|
{
|
|
string path = TempPath();
|
|
try
|
|
{
|
|
using var capture = new BoundedProcessOutputCapture(path);
|
|
|
|
capture.Append(Encoding.UTF8.GetBytes("abc"));
|
|
capture.Append(Encoding.UTF8.GetBytes("def"));
|
|
capture.Dispose();
|
|
|
|
Assert.Equal("abcdef", File.ReadAllText(path));
|
|
}
|
|
finally
|
|
{
|
|
TryDelete(path);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void EmptyAppendsAreNoOps()
|
|
{
|
|
string path = TempPath();
|
|
try
|
|
{
|
|
using var capture = new BoundedProcessOutputCapture(path);
|
|
|
|
capture.Append(ReadOnlySpan<byte>.Empty);
|
|
capture.AppendLine(string.Empty);
|
|
capture.Dispose();
|
|
|
|
// An empty string line still gets its trailing newline —
|
|
// only a genuinely zero-length byte span (or a null line) is
|
|
// a true no-op.
|
|
Assert.Equal("\n", File.ReadAllText(path));
|
|
}
|
|
finally
|
|
{
|
|
TryDelete(path);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void AppendAfterDisposeIsASilentNoOp()
|
|
{
|
|
string path = TempPath();
|
|
try
|
|
{
|
|
var capture = new BoundedProcessOutputCapture(path);
|
|
capture.AppendLine("before");
|
|
capture.Dispose();
|
|
|
|
capture.AppendLine("after — must not throw or reopen the file");
|
|
|
|
Assert.Equal("before\n", File.ReadAllText(path));
|
|
}
|
|
finally
|
|
{
|
|
TryDelete(path);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ConstructorRejectsANonPositiveMaxBytes()
|
|
{
|
|
Assert.Throws<ArgumentOutOfRangeException>(
|
|
() => new BoundedProcessOutputCapture(TempPath(), maxBytes: 0));
|
|
Assert.Throws<ArgumentOutOfRangeException>(
|
|
() => new BoundedProcessOutputCapture(TempPath(), maxBytes: -1));
|
|
}
|
|
|
|
[Fact]
|
|
public void ConstructorRejectsANullOrBlankPath()
|
|
{
|
|
Assert.Throws<ArgumentException>(() => new BoundedProcessOutputCapture(""));
|
|
Assert.Throws<ArgumentException>(() => new BoundedProcessOutputCapture(" "));
|
|
}
|
|
|
|
private static string TempPath() => Path.Combine(
|
|
Path.GetTempPath(),
|
|
"acdream-406-capture-" + Guid.NewGuid().ToString("N") + ".log");
|
|
|
|
private static void TryDelete(string path)
|
|
{
|
|
try
|
|
{
|
|
File.Delete(path);
|
|
}
|
|
catch (IOException)
|
|
{
|
|
}
|
|
}
|
|
}
|