Skip to content

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

Closed
CLHelper wants to merge 1 commit into
ProjectLeonida:mainfrom
CLHelper:runtime/hang-dump
Closed

CLHelper wants to merge 1 commit into
ProjectLeonida:mainfrom
CLHelper:runtime/hang-dump

Conversation

@CLHelper

Copy link
Copy Markdown
Contributor

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

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)

…seconds

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 <noreply@anthropic.com>
(cherry picked from commit cc424342d8725eb24d45aa4fa0b9ad5a6903010a)
(cherry picked from commit 7acba811a885dbc6fb7d481aa4399bf9fed94ed5)
@PierreEbele

Copy link
Copy Markdown
Contributor

Reviewed e6438eb: 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

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@CLHelper

Copy link
Copy Markdown
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants