common: report crashes that reach no handler #409

Closed
github-bot wants to merge 1 commit from gh-pr/409/head into gh-pr/409/base
Owner

@⁠MehmetCambaz on GitHub · 2026-08-29 22:02 UTC · original
MehmetCambaz:fix/crash-report → main · +179 −0 · 6 files · 1 commits · closed without merging

<html>

Problem

KytyExceptionHandler reports access violations and illegal instructions, which is why a null dereference already produces a stack trace. Nothing else does. HostException's filter returns EXCEPTION_CONTINUE_SEARCH for every other exception code, so those faults never reach that handler, and an uncaught throw never goes near it at all — it goes to std::terminate.

The result is a class of session that ends with no record of why. #307 describes it exactly: "The process terminates before Kyty can flush a crash record to the log", and that reporter had to inspect a minidump to find RIP=0.

Measured on Tetris Forever with a fault injected at a fixed point, before and after:

  | before | after -- | -- | -- access violation | reported | unchanged stack overflow | nothing | reported integer divide by zero | nothing | reported uncaught throw | nothing | reported .at() out of range | nothing | reported

The bottom two are the ones I would worry about. The tree has one catch site against 129 new expressions and 52 .at() calls, so a bad_alloc or an out_of_range is a realistic way to lose a session silently.

The fix

Three hooks, because no single one covers the ground:

  • SetUnhandledExceptionFilter takes the faults the guest page-fault handler declined.
  • std::terminate covers an uncaught throw — but the Microsoft CRT keeps that handler per thread, so on Windows it only covers the thread that installed it. I found this by testing, not by reading: the first version reported nothing when the throw happened on a guest thread.
  • SIGABRT is process-wide and is what actually catches a throw on a guest or graphics thread.

All three build the same report EXIT() builds, and hand the fault back to the default handling afterwards, so Windows Error Reporting and dump collection are unchanged. Reporting is latched so a fault inside the reporter cannot loop.

The terminate and SIGABRT hooks are portable and run everywhere; on POSIX std::terminate is process-wide, so uncaught throws are covered there without the per-thread caveat. Only the fault hook is Windows-only — SIGSEGV, SIGBUS and SIGILL already carry the guest page-fault handler, so a second handler there has to chain rather than replace it, and I have no POSIX machine to verify that on. SysInstallCrashHandler returns false until someone does.

What this does not cover

If the fault happens inside the logger itself, no report is produced. I tested that case specifically by faulting inside RawFormatter::format while the sink mutex was held: nothing is written, with or without this change, because the handler does not run. That is pre-existing and unchanged here, not something this patch introduces.

I also checked whether re-entering the logger from a crashing thread could hang, since that would be worse than the silence it replaces. It cannot: MSVC's std::mutex detects recursive acquisition and throws resource deadlock would occur rather than blocking, and spdlog swallows it.

Test plan

Windows 11 25H2, clang-cl 20.1.8, RTX 2060.

  • ctest: 35/35.
  • Each row of the table above produced by injecting that fault into a real run and reading the log, before and after.
  • Normal run unaffected: 1531 flips in 25 s, zero spurious reports, log size unchanged.
  • No new test — this only produces diagnostic output on a dying process, which the suite has no way to observe.

AI use

Written with AI assistance (Claude). It traced the existing fault paths, found that access violations were already covered and the gap was elsewhere, wrote the change, and ran the builds, injected-fault runs and measurements on my machine. I reviewed the diff and the results.

</html>Problem

KytyExceptionHandler reports access violations and illegal instructions, which is why a null dereference already produces a stack trace. Nothing else does. HostException's filter returns EXCEPTION_CONTINUE_SEARCH for every other exception code, so those faults never reach that handler, and an uncaught throw never goes near it at all — it goes to std::terminate.

The result is a class of session that ends with no record of why. #307 describes it exactly: "The process terminates before Kyty can flush a crash record to the log", and that reporter had to inspect a minidump to find RIP=0.

Measured on Tetris Forever with a fault injected at a fixed point, before and after:

before	after

access violation reported unchanged
stack overflow nothing reported
integer divide by zero nothing reported
uncaught throw nothing reported
.at() out of range nothing reported

The bottom two are the ones I would worry about. The tree has one catch site against 129 new expressions and 52 .at() calls, so a bad_alloc or an out_of_range is a realistic way to lose a session silently.

The fix

Three hooks, because no single one covers the ground:

SetUnhandledExceptionFilter takes the faults the guest page-fault handler declined.
std::terminate covers an uncaught throw — but the Microsoft CRT keeps that handler per thread, so on Windows it only covers the thread that installed it. I found this by testing, not by reading: the first version reported nothing when the throw happened on a guest thread.
SIGABRT is process-wide and is what actually catches a throw on a guest or graphics thread.

All three build the same report EXIT() builds, and hand the fault back to the default handling afterwards, so Windows Error Reporting and dump collection are unchanged. Reporting is latched so a fault inside the reporter cannot loop.

The terminate and SIGABRT hooks are portable and run everywhere; on POSIX std::terminate is process-wide, so uncaught throws are covered there without the per-thread caveat. Only the fault hook is Windows-only — SIGSEGV, SIGBUS and SIGILL already carry the guest page-fault handler, so a second handler there has to chain rather than replace it, and I have no POSIX machine to verify that on. SysInstallCrashHandler returns false until someone does.

What this does not cover

If the fault happens inside the logger itself, no report is produced. I tested that case specifically by faulting inside RawFormatter::format while the sink mutex was held: nothing is written, with or without this change, because the handler does not run. That is pre-existing and unchanged here, not something this patch introduces.

I also checked whether re-entering the logger from a crashing thread could hang, since that would be worse than the silence it replaces. It cannot: MSVC's std::mutex detects recursive acquisition and throws resource deadlock would occur rather than blocking, and spdlog swallows it.

Test plan

Windows 11 25H2, clang-cl 20.1.8, RTX 2060.

ctest: 35/35.
Each row of the table above produced by injecting that fault into a real run and reading the log, before and after.
Normal run unaffected: 1531 flips in 25 s, zero spurious reports, log size unchanged.
No new test — this only produces diagnostic output on a dying process, which the suite has no way to observe.
AI use

Written with AI assistance (Claude). It traced the existing fault paths, found that access violations were already covered and the gap was elsewhere, wrote the change, and ran the builds, injected-fault runs and measurements on my machine. I reviewed the diff and the results.

**@⁠MehmetCambaz** on GitHub · 2026-08-29 22:02 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/409) `MehmetCambaz:fix/crash-report` → `main` · +179 −0 · 6 files · 1 commits · closed without merging <html> <body> <!--StartFragment--><h3 dir="ltr">Problem</h3><p dir="ltr"><code>KytyExceptionHandler</code> reports access violations and illegal instructions, which is why a null dereference already produces a stack trace. Nothing else does. <code>HostException</code>'s filter returns <code>EXCEPTION_CONTINUE_SEARCH</code> for every other exception code, so those faults never reach that handler, and an uncaught throw never goes near it at all — it goes to <code>std::terminate</code>.</p><p dir="ltr">The result is a class of session that ends with no record of why. <a href="https://git.gtahub.st/github/KytyPS5--KytyPS5/issues/307" target="_blank" rel="noreferrer">#307</a> describes it exactly: <em>"The process terminates before Kyty can flush a crash record to the log"</em>, and that reporter had to inspect a minidump to find <code>RIP=0</code>.</p><p dir="ltr">Measured on Tetris Forever with a fault injected at a fixed point, before and after:</p><div style="">   | before | after -- | -- | -- access violation | reported | unchanged stack overflow | nothing | reported integer divide by zero | nothing | reported uncaught throw | nothing | reported .at() out of range | nothing | reported </div><p dir="ltr">The bottom two are the ones I would worry about. The tree has one <code>catch</code> site against 129 <code>new</code> expressions and 52 <code>.at()</code> calls, so a <code>bad_alloc</code> or an <code>out_of_range</code> is a realistic way to lose a session silently.</p><h3 dir="ltr">The fix</h3><p dir="ltr">Three hooks, because no single one covers the ground:</p><ul dir="ltr"><li style=""><code>SetUnhandledExceptionFilter</code> takes the faults the guest page-fault handler declined.</li><li style=""><code>std::terminate</code> covers an uncaught throw — but the Microsoft CRT keeps that handler <strong>per thread</strong>, so on Windows it only covers the thread that installed it. I found this by testing, not by reading: the first version reported nothing when the throw happened on a guest thread.</li><li style=""><code>SIGABRT</code> is process-wide and is what actually catches a throw on a guest or graphics thread.</li></ul><p dir="ltr">All three build the same report <code>EXIT()</code> builds, and hand the fault back to the default handling afterwards, so Windows Error Reporting and dump collection are unchanged. Reporting is latched so a fault inside the reporter cannot loop.</p><p dir="ltr">The terminate and SIGABRT hooks are portable and run everywhere; on POSIX <code>std::terminate</code> is process-wide, so uncaught throws are covered there without the per-thread caveat. Only the fault hook is Windows-only — <code>SIGSEGV</code>, <code>SIGBUS</code> and <code>SIGILL</code> already carry the guest page-fault handler, so a second handler there has to chain rather than replace it, and I have no POSIX machine to verify that on. <code>SysInstallCrashHandler</code> returns false until someone does.</p><h3 dir="ltr">What this does not cover</h3><p dir="ltr">If the fault happens <em>inside the logger itself</em>, no report is produced. I tested that case specifically by faulting inside <code>RawFormatter::format</code> while the sink mutex was held: nothing is written, with or without this change, because the handler does not run. That is pre-existing and unchanged here, not something this patch introduces.</p><p dir="ltr">I also checked whether re-entering the logger from a crashing thread could hang, since that would be worse than the silence it replaces. It cannot: MSVC's <code>std::mutex</code> detects recursive acquisition and throws <code>resource deadlock would occur</code> rather than blocking, and spdlog swallows it.</p><h3 dir="ltr">Test plan</h3><p dir="ltr">Windows 11 25H2, clang-cl 20.1.8, RTX 2060.</p><ul dir="ltr"><li style=""><input disabled="" readonly="" type="checkbox" checked=""> <code>ctest</code>: 35/35.</li><li style=""><input disabled="" readonly="" type="checkbox" checked=""> Each row of the table above produced by injecting that fault into a real run and reading the log, before and after.</li><li style=""><input disabled="" readonly="" type="checkbox" checked=""> Normal run unaffected: 1531 flips in 25 s, zero spurious reports, log size unchanged.</li><li style=""><input disabled="" readonly="" type="checkbox" checked=""> No new test — this only produces diagnostic output on a dying process, which the suite has no way to observe.</li></ul><h3 dir="ltr">AI use</h3><p dir="ltr">Written with AI assistance (Claude). It traced the existing fault paths, found that access violations were already covered and the gap was elsewhere, wrote the change, and ran the builds, injected-fault runs and measurements on my machine. I reviewed the diff and the results.</p><!--EndFragment--> </body> </html>Problem KytyExceptionHandler reports access violations and illegal instructions, which is why a null dereference already produces a stack trace. Nothing else does. HostException's filter returns EXCEPTION_CONTINUE_SEARCH for every other exception code, so those faults never reach that handler, and an uncaught throw never goes near it at all — it goes to std::terminate. The result is a class of session that ends with no record of why. [#307](https://git.gtahub.st/github/KytyPS5--KytyPS5/issues/307) describes it exactly: "The process terminates before Kyty can flush a crash record to the log", and that reporter had to inspect a minidump to find RIP=0. Measured on Tetris Forever with a fault injected at a fixed point, before and after: before after access violation reported unchanged stack overflow nothing reported integer divide by zero nothing reported uncaught throw nothing reported .at() out of range nothing reported The bottom two are the ones I would worry about. The tree has one catch site against 129 new expressions and 52 .at() calls, so a bad_alloc or an out_of_range is a realistic way to lose a session silently. The fix Three hooks, because no single one covers the ground: SetUnhandledExceptionFilter takes the faults the guest page-fault handler declined. std::terminate covers an uncaught throw — but the Microsoft CRT keeps that handler per thread, so on Windows it only covers the thread that installed it. I found this by testing, not by reading: the first version reported nothing when the throw happened on a guest thread. SIGABRT is process-wide and is what actually catches a throw on a guest or graphics thread. All three build the same report EXIT() builds, and hand the fault back to the default handling afterwards, so Windows Error Reporting and dump collection are unchanged. Reporting is latched so a fault inside the reporter cannot loop. The terminate and SIGABRT hooks are portable and run everywhere; on POSIX std::terminate is process-wide, so uncaught throws are covered there without the per-thread caveat. Only the fault hook is Windows-only — SIGSEGV, SIGBUS and SIGILL already carry the guest page-fault handler, so a second handler there has to chain rather than replace it, and I have no POSIX machine to verify that on. SysInstallCrashHandler returns false until someone does. What this does not cover If the fault happens inside the logger itself, no report is produced. I tested that case specifically by faulting inside RawFormatter::format while the sink mutex was held: nothing is written, with or without this change, because the handler does not run. That is pre-existing and unchanged here, not something this patch introduces. I also checked whether re-entering the logger from a crashing thread could hang, since that would be worse than the silence it replaces. It cannot: MSVC's std::mutex detects recursive acquisition and throws resource deadlock would occur rather than blocking, and spdlog swallows it. Test plan Windows 11 25H2, clang-cl 20.1.8, RTX 2060. ctest: 35/35. Each row of the table above produced by injecting that fault into a real run and reading the log, before and after. Normal run unaffected: 1531 flips in 25 s, zero spurious reports, log size unchanged. No new test — this only produces diagnostic output on a dying process, which the suite has no way to observe. AI use Written with AI assistance (Claude). It traced the existing fault paths, found that access violations were already covered and the gap was elsewhere, wrote the change, and ran the builds, injected-fault runs and measurements on my machine. I reviewed the diff and the results.
KytyExceptionHandler already reports access violations and illegal
instructions, which is why a null dereference produces a stack trace today.
Every other way the process can die produces nothing at all: the fault never
reaches that handler, no report is written, and the tail of the log is still
sitting in the file buffer when the process goes.

Measured on Tetris Forever with a deliberate fault injected, before and after:

  access violation        reported (KytyExceptionHandler)   unchanged
  stack overflow          nothing                           reported
  integer divide by zero  nothing                           reported
  uncaught throw          nothing                           reported
  .at() out of range      nothing                           reported

The last two matter most. The tree has one catch site against 129 new
expressions and 52 .at() calls, so a bad_alloc or an out_of_range is a
realistic way to lose a session with no record of it.

Three hooks, because no single one covers the ground. SetUnhandledExceptionFilter
takes the faults the guest page-fault handler declined. std::terminate covers the
uncaught case, but the Microsoft CRT keeps that handler per thread, so on its own
it would only cover the thread that installed it; SIGABRT is process-wide and is
what actually catches it on a guest or graphics thread.

All three build the same report EXIT() builds and hand the fault back to the
default handling afterwards, so Windows Error Reporting and dump collection are
unchanged. Reporting is latched, so a fault inside the reporter cannot loop.

The report does not name the exception. utils.cmake builds with -fno-exceptions
on clang, so reading it back would not compile there, and by the time SIGABRT
arrives there is no active exception to describe in any case.

The terminate and SIGABRT hooks are portable and run on every platform. Only the
fault hook is Windows-only: SIGSEGV, SIGBUS and SIGILL already carry the guest
page-fault handler, so a second handler there has to chain rather than replace
it, and that needs a POSIX machine to verify. SysInstallCrashHandler returns
false there until then.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:19:27 +00:00
github-bot closed this pull request 2026-11-26 09:19:27 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.