RT-HANG-DUMP: LEONIDA_HANG_DUMP writes every thread's stack after N seconds #576

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

@⁠CLHelper on GitHub · 2026-11-24 08:49 UTC · original
CLHelper:runtime/hang-dump → main · +72 −0 · 3 files · 1 commits · closed without merging

Claimed in the coordination issue as: RT-HANG-DUMP (#14)

What and why

A retail boot that faults leaves a last exception in the trace; one that hangs leaves nothing to look at. With
LEONIDA_HANG_DUMP=<seconds>, gta6rt waits that long and then writes the stack of every thread in the process to the
startup trace (each thread is suspended briefly while its context is read, then resumed), so a stalled boot shows
where each thread waits. Unset, nothing changes: no thread is started.

The code is in runtime/shims/startup_trace.h; docs/boot.md and runtime/README.md name the variable.

The commit is 4ld0-vito's (from their fork, where it was used for the boot work), carried over unchanged with its
author kept.

Tested

  • Built gta6rt, gta6rt_tls_host, unimplemented_policy_test and xapu_chad_test with MSVC 2022 (Windows 11,
    RelWithDebInfo, Ninja) on this branch: all link; the two shim tests that include startup_trace.h pass.
  • Not run against the retail executable here.

GPUs tested: n/a (no GPU code)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)
**@⁠CLHelper** on GitHub · 2026-11-24 08:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/576) `CLHelper:runtime/hang-dump` → `main` · +72 −0 · 3 files · 1 commits · closed without merging Claimed in the coordination issue as: RT-HANG-DUMP (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why A retail boot that faults leaves a last exception in the trace; one that hangs leaves nothing to look at. With `LEONIDA_HANG_DUMP=<seconds>`, `gta6rt` waits that long and then writes the stack of every thread in the process to the startup trace (each thread is suspended briefly while its context is read, then resumed), so a stalled boot shows where each thread waits. Unset, nothing changes: no thread is started. The code is in `runtime/shims/startup_trace.h`; `docs/boot.md` and `runtime/README.md` name the variable. The commit is 4ld0-vito's (from their fork, where it was used for the boot work), carried over unchanged with its author kept. ### Tested - Built `gta6rt`, `gta6rt_tls_host`, `unimplemented_policy_test` and `xapu_chad_test` with MSVC 2022 (Windows 11, RelWithDebInfo, Ninja) on this branch: all link; the two shim tests that include `startup_trace.h` pass. - Not run against the retail executable here. GPUs tested: n/a (no GPU code) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change)
A diagnostic for boots that hang: with --trace and LEONIDA_HANG_DUMP=<seconds>, that many seconds after the trace
opens every other thread is suspended just long enough to read its registers, and its stack is walked (with the
exception events' walker) after it resumes and written as a thread_stack event. The retail title's watchdog gives up
after 30 seconds when its main thread stops progressing; this shows where each thread waits. Documented in
docs/boot.md and runtime/README.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit cc424342d8725eb24d45aa4fa0b9ad5a6903010a)
(cherry picked from commit 7acba811a885dbc6fb7d481aa4399bf9fed94ed5)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:09:33 +00:00
github-bot closed this pull request 2026-11-26 09:09:33 +00:00
Author
Owner

@⁠PierreEbele on GitHub · 2026-11-25 01:05 UTC · original

Reviewed e6438eb9: startup_trace.h is Windows-only (0 references in the Linux CMake build graph; no MSVC/mingw in this sandbox), so runtime behavior could not be executed here. Verified on both trees: tests/test_docs_env_vars.py, test_docs_table_rows.py, test_docs_links.py (all OK; LEONIDA_HANG_DUMP scanned at startup_trace.h:292), PR Linux portable ctest (72/72 pass; 2 skipped), main root_signature/host_cpu (2/2 pass), and static doc consistency for startup_trace.h (thread_stack, tid/stack, 16-frame bound, docs/boot.md bullet and header example).

Static bug found: log_thread_stack_guarded does not set in_handler = true before __try (and reset it after __except). Because detail::handler is a first-chance VEH (AddVectoredExceptionHandler(1, ...)), any access violation inside IsBadReadPtr or RtlVirtualUnwind while walking a resumed thread's stack invokes detail::handler before __except and writes a spurious exception (0xc0000005) event from the dump thread.

Also note: while event() guards detail::file under detail::mutex(), close() does not stop the detached thread, so an in-flight walk during normal CRT teardown (--init/--dry-run/tests) can access non-immortal g_lookup_mutex/g_lookups in rt::shims::module_offset() (and a pending timer survives close() across reopen). Nits: invalid/non-positive LEONIDA_HANG_DUMP values are ignored silently, and static_cast<DWORD>(seconds) * 1000 wraps starting at 4294968 s (704 ms; 536870912 s -> 0 ms). Please guard in_handler (and try/catch allocation errors) in the dump path and confirm with one Windows run using --trace and a small LEONIDA_HANG_DUMP value. AI-assisted review.

Recommendation: Fix first

**@⁠PierreEbele** on GitHub · 2026-11-25 01:05 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/576#issuecomment-7149188152) Reviewed e6438eb9: `startup_trace.h` is Windows-only (0 references in the Linux CMake build graph; no MSVC/mingw in this sandbox), so runtime behavior could not be executed here. Verified on both trees: `tests/test_docs_env_vars.py`, `test_docs_table_rows.py`, `test_docs_links.py` (all OK; `LEONIDA_HANG_DUMP` scanned at `startup_trace.h:292`), PR Linux portable `ctest` (72/72 pass; 2 skipped), main `root_signature`/`host_cpu` (2/2 pass), and static doc consistency for `startup_trace.h` (`thread_stack`, `tid`/`stack`, 16-frame bound, `docs/boot.md` bullet and header example). Static bug found: `log_thread_stack_guarded` does not set `in_handler = true` before `__try` (and reset it after `__except`). Because `detail::handler` is a first-chance VEH (`AddVectoredExceptionHandler(1, ...)`), any access violation inside `IsBadReadPtr` or `RtlVirtualUnwind` while walking a resumed thread's stack invokes `detail::handler` before `__except` and writes a spurious `exception` (`0xc0000005`) event from the dump thread. Also note: while `event()` guards `detail::file` under `detail::mutex()`, `close()` does not stop the detached thread, so an in-flight walk during normal CRT teardown (`--init`/`--dry-run`/tests) can access non-immortal `g_lookup_mutex`/`g_lookups` in `rt::shims::module_offset()` (and a pending timer survives `close()` across reopen). Nits: invalid/non-positive `LEONIDA_HANG_DUMP` values are ignored silently, and `static_cast<DWORD>(seconds) * 1000` wraps starting at `4294968` s (704 ms; `536870912` s -> 0 ms). Please guard `in_handler` (and `try/catch` allocation errors) in the dump path and confirm with one Windows run using `--trace` and a small `LEONIDA_HANG_DUMP` value. AI-assisted review. **Recommendation:** Fix first
Author
Owner

@⁠CLHelper on GitHub · 2026-11-25 08:16 UTC · original

Closing this: main now has LEONIDA_STACKS (runtime/src/thread_stacks.cpp, landed with #984), which does what this pull request did and more: several snapshot times, the stack copied while the thread is suspended and unwound from the copy, and its own test. A second dump in the startup trace would only duplicate it. Thanks @⁠PierreEbele for the review: the missing in_handler guard on the dump thread and the timer that outlives close() were both real, and they go away with this. AI-assisted.

**@⁠CLHelper** on GitHub · 2026-11-25 08:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/576#issuecomment-7156832081) Closing this: `main` now has `LEONIDA_STACKS` (`runtime/src/thread_stacks.cpp`, landed with #984), which does what this pull request did and more: several snapshot times, the stack copied while the thread is suspended and unwound from the copy, and its own test. A second dump in the startup trace would only duplicate it. Thanks @⁠PierreEbele for the review: the missing `in_handler` guard on the dump thread and the timer that outlives `close()` were both real, and they go away with this. AI-assisted.

Pull request closed

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