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

Closed
CLHelper wants to merge 1 commit from upstream-pr/576 into main
CLHelper commented 2026-11-24 08:49:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by CLHelper on 2026-11-24T08:49:57Z.


Claimed in the coordination issue as: RT-HANG-DUMP (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7126610928)

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)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5468972877 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/576). Originally posted by [CLHelper](https://github.com/CLHelper) on 2026-11-24T08:49:57Z. --- Claimed in the coordination issue as: RT-HANG-DUMP (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7126610928) ### 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)
PierreEbele commented 2026-11-25 01:05:02 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by PierreEbele on 2026-11-25T01:05:02Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7149188152 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/576#issuecomment-7149188152). Originally posted by [PierreEbele](https://github.com/PierreEbele) on 2026-11-25T01:05:02Z. --- 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
CLHelper commented 2026-11-25 08:16:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by CLHelper on 2026-11-25T08:16:52Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7156832081 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/576#issuecomment-7156832081). Originally posted by [CLHelper](https://github.com/CLHelper) on 2026-11-25T08:16:52Z. --- 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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!576
No description provided.