Engine robustness re-review follow-ups: crash-log flush ring, cast refusal, dump hold cap

- The native crash logger keeps the last 16 first-chance records in a static ring
  (pushed in the VEH: no I/O, no allocation, no loader call). A flush writes the ones
  not yet written, oldest first, each record once. Flushed from the last-chance filter
  and, through the new export RC_FlushCrashLog, from the engine's ExitProcess /
  TerminateProcess / RtlExitUserProcess / NtTerminateProcess detours on a crash code
  (raw unmanaged function pointer, resolved and primed at install). Recovers the fatal
  repeat of an address seen before, which the dedupe skips and which never reaches the
  last-chance filter when a handler terminates the process.
- EnqueueCast refuses during a world transition (CastSlot, pure) and counts the
  refusal in TransitionRejectedCount. The cast slot now owns the stop-wait note the
  cast drain keeps per cast (CastSlot.StopWaitNoted, reset by each queued cast before
  the publishing Volatile.Write of Pending), and DrainCasts reads the queue time once.
- The launcher's dump hold is capped at 15 min per client (DumpInProgressRule.Decide);
  past it the kill checks resume.
- CrashExitRule: ExitProcess(-1) (0xFFFFFFFF) is no longer a crash exit; the rest of
  the 0xC0000000-and-up range is unchanged, so CLR/C++ unhandled-exception exits
  (0xE0434352, 0xE06D7363) still skip managed work.
- The heartbeat line carries qrej=<TransitionRejectedCount>.

Tests: EngineRobustnessTests (adds CastSlot stop-wait and ActionRing.TryPeekAt checks),
CrashLogFilterTests.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-10-10 21:53:42 -07:00
commit 636f5d307a
14 changed files with 585 additions and 60 deletions

View file

@ -27,6 +27,14 @@ internal static partial class Program
OutOfMemoryBoxMatchesOnlyAcsBox();
LauncherHoldsTheKillDuringADump();
// The cast slot, the dump hold cap and the crash exits
CastRefusedDuringATransition();
RingPeekAtWalksWithoutRemoving();
EnqueueCastPassesTheTransitionState();
LauncherDumpHoldIsCapped();
CrashExitExcludesExitMinusOne();
CrashExitPathsFlushTheCrashLog();
}
// ---- the action ring (the real ActionRing, the one AcMainThreadQueue uses) --------
@ -329,4 +337,132 @@ internal static partial class Program
Check(!DumpInProgressRule.IsDumping("hb #1\n", now.AddMinutes(-5), now.AddMinutes(-2), now), "a dump file quiet for 2 min: no hold");
Check(!DumpInProgressRule.IsDumping(null, now, null, now), "nothing at all: no hold");
}
// ---- the cast slot refuses during a transition (the real CastSlot) -------------
private static void CastRefusedDuringATransition()
{
_case = "cast slot transition";
var slot = new CastSlot();
Check(slot.TryEnqueue(0x50000001, 1234, 1000, inTransition: true) == CastSlot.Admit.RejectedTransition,
"an empty slot refuses a cast during a transition");
Check(slot.Pending == 0 && slot.TargetId == 0 && slot.SpellId == 0 && slot.QueuedAt == 0,
"and stays untouched");
Check(slot.TryEnqueue(0x50000002, 99, 2000, inTransition: false) == CastSlot.Admit.Queued, "outside one, a cast is queued");
Check(slot.TryEnqueue(0x50000003, 77, 2500, inTransition: true) == CastSlot.Admit.RejectedTransition,
"a transition refuses even with a cast pending (counted as a transition refusal, not busy)");
Check(slot.Pending == 1 && slot.TargetId == 0x50000002 && slot.SpellId == 99 && slot.QueuedAt == 2000,
"the pending cast is unchanged");
Check(slot.TryEnqueue(0x50000004, 55, 2600, inTransition: false) == CastSlot.Admit.Busy, "busy outside a transition");
slot.Pending = 0;
Check(slot.TryEnqueue(0x50000005, 66, 9000, inTransition: false) == CastSlot.Admit.Queued
&& slot.SpellId == 66 && slot.QueuedAt == 9000, "after the transition, casts queue again");
// The drain's once-per-cast stop-wait note belongs to the cast: a new cast clears it, a
// refused one leaves the pending cast's note alone.
slot.StopWaitNoted = true;
Check(slot.TryEnqueue(0x50000006, 67, 9100, inTransition: false) == CastSlot.Admit.Busy && slot.StopWaitNoted,
"a busy refusal keeps the pending cast's stop-wait note");
slot.Pending = 0;
Check(slot.TryEnqueue(0x50000007, 68, 9200, inTransition: false) == CastSlot.Admit.Queued && !slot.StopWaitNoted,
"a newly queued cast starts with no stop-wait note");
}
// ---- The cast drain's walk of the ring (ActionRing.TryPeekAt) --------------------------
private static void RingPeekAtWalksWithoutRemoving()
{
_case = "ring peek-at";
var ring = new ActionRing<Act>(8, 2);
Check(!ring.TryPeekAt(0, out _), "an empty ring has nothing at 0");
// Wrap the ring once so the walk crosses the end of the storage.
for (int i = 0; i < 5; i++) ring.TryEnqueue(new Act("old" + i, i, false), false, false);
for (int i = 0; i < 5; i++) ring.TryDequeue(out _);
for (int i = 0; i < 6; i++) ring.TryEnqueue(new Act("e" + i, 100 + i, i == 4), i == 4, false);
var seen = new List<string>();
for (int i = 0; ring.TryPeekAt(i, out Act e); i++) seen.Add(e.What);
Check(string.Join(",", seen) == "e0,e1,e2,e3,e4,e5", $"oldest first across the wrap, got [{string.Join(",", seen)}]");
Check(ring.Count == 6, $"nothing removed (count {ring.Count})");
Check(!ring.TryPeekAt(6, out _) && !ring.TryPeekAt(-1, out _), "out of range is false");
Check(ring.TryDequeue(out Act head) && head.What == "e0" && ring.TryPeekAt(0, out Act next) && next.What == "e1",
"after a dequeue, offset 0 is the new oldest");
}
private static void EnqueueCastPassesTheTransitionState()
{
_case = "EnqueueCast wiring";
string? compat = FindRepoDir(Path.Combine("src", "RynthCore.Engine", "Compatibility"));
Check(compat != null, "found src\\RynthCore.Engine\\Compatibility by walking up");
if (compat == null) return;
string src = File.ReadAllText(Path.Combine(compat, "AcMainThreadQueue.cs"));
int start = src.IndexOf("public static bool EnqueueCast(", StringComparison.Ordinal);
int end = start < 0 ? -1 : src.IndexOf("\n }", start, StringComparison.Ordinal);
string body = start < 0 || end < 0 ? "" : src.Substring(start, end - start);
Check(body.Contains("_cast.TryEnqueue(targetId, spellId, NowMs, PlayerLifecycleLog.InWorldTransition)", StringComparison.Ordinal),
"EnqueueCast hands CastSlot the live transition state");
Check(Regex.IsMatch(body, @"RejectedTransition\)\s*Interlocked\.Increment\(ref _transitionRejected\)"),
"and counts a refusal in TransitionRejectedCount (the heartbeat's qrej)");
Check(!Regex.IsMatch(src, @"_cast\.(TargetId|SpellId|QueuedAt)\s*=[^=]"),
"nothing else writes the slot's cast fields");
}
// ---- the launcher's dump hold is capped per client --------------------------------
private static void LauncherDumpHoldIsCapped()
{
_case = "dump hold cap";
var now = new DateTime(2026, 10, 10, 22, 18, 30, DateTimeKind.Utc);
string open = $"hb #1\n[18:00:00] {DumpInProgressRule.MarkerStarted} pid=123 - ...\nhb #2\n";
DateTime freshLog = now.AddSeconds(-5);
Check(DumpInProgressRule.Decide(open, freshLog, null, null, now) == DumpInProgressRule.Hold.Holding,
"an open marker, first seen now: hold");
Check(DumpInProgressRule.Decide(open, freshLog, null, now.AddMinutes(-5), now) == DumpInProgressRule.Hold.Holding,
"held 5 min: still holding");
Check(DumpInProgressRule.Decide(open, freshLog, null, now.AddMinutes(-16), now) == DumpInProgressRule.Hold.Expired,
"fresh log, marker held over 15 min: the kill checks run");
Check(DumpInProgressRule.Decide("hb #1\n", freshLog, now.AddSeconds(-3), now.AddMinutes(-16), now) == DumpInProgressRule.Hold.Expired,
"a dump file still being written does not extend the cap either");
Check(DumpInProgressRule.Decide(open, freshLog, null, DateTime.MinValue, now) == DumpInProgressRule.Hold.Expired,
"the launcher's 'expired, logged' mark stays expired");
Check(DumpInProgressRule.Decide("hb #1\n", freshLog, null, now.AddMinutes(-16), now) == DumpInProgressRule.Hold.None,
"no sign of a dump: None (the launcher forgets the pid)");
}
// ---- which exit codes are crash exits ---------------------------------------------
private static void CrashExitExcludesExitMinusOne()
{
_case = "crash exit rule";
Check(CrashExitRule.IsCrashExit(0xC0000005u), "0xC0000005 access violation: crash");
Check(CrashExitRule.IsCrashExit(0xC0000409u), "0xC0000409 stack buffer overrun: crash");
Check(CrashExitRule.IsCrashExit(0xC0000374u), "0xC0000374 heap corruption: crash");
Check(CrashExitRule.IsCrashExit(0xC00000FDu), "0xC00000FD stack overflow: crash");
Check(CrashExitRule.IsCrashExit(0xE0434352u), "0xE0434352 CLR unhandled exception: crash (as before)");
Check(!CrashExitRule.IsCrashExit(0xFFFFFFFFu), "ExitProcess(-1): not a crash");
Check(!CrashExitRule.IsCrashExit(0u), "0: not a crash");
Check(!CrashExitRule.IsCrashExit(1u), "1: not a crash");
Check(!CrashExitRule.IsCrashExit(0x80000003u), "0x80000003 (warning severity): not a crash");
Check(!CrashExitRule.IsCrashExit(0x40010004u), "0x40010004 (informational): not a crash");
}
private static void CrashExitPathsFlushTheCrashLog()
{
_case = "crash exit flush";
string? compat = FindRepoDir(Path.Combine("src", "RynthCore.Engine", "Compatibility"));
Check(compat != null, "found src\\RynthCore.Engine\\Compatibility by walking up");
if (compat == null) return;
string src = File.ReadAllText(Path.Combine(compat, "ProcessExitHooks.cs"));
Check(src.Contains("private static bool IsCrashExit(uint code) => CrashExitRule.IsCrashExit(code);", StringComparison.Ordinal),
"ProcessExitHooks uses CrashExitRule");
foreach (string detour in new[] { "ExitProcessDetour", "TerminateProcessDetour", "RtlExitUserProcessDetour", "NtTerminateProcessDetour" })
{
int start = src.IndexOf($"private static {(detour.Contains("Terminate") ? (detour.StartsWith("Nt") ? "uint" : "bool") : "void")} {detour}(", StringComparison.Ordinal);
int end = start < 0 ? -1 : src.IndexOf("\n }", start, StringComparison.Ordinal);
string body = start < 0 || end < 0 ? "" : src.Substring(start, end - start);
int flush = body.IndexOf("SehTrampoline.FlushCrashLog(", StringComparison.Ordinal);
int orig = body.IndexOf("Original!(", StringComparison.Ordinal);
Check(flush >= 0 && orig > flush, $"{detour}: a crash exit flushes the crash log before the original");
}
}
}