[integration] Runtime: the EXE's thread-local exit callback (#2315) and scripted input between reads (#2372) (INTEGRATION-ZRO-DRAFTS) #2395

Manually merged
github merged 7 commits from gh-pr/2395/head into gh-pr/2395/base 2026-11-26 18:35:56 +00:00
Owner

@⁠aslan2142 on GitHub · 2026-11-26 18:28 UTC · original
aslan2142:integration/tls-exit-and-scripted-input → main · +205 −39 · 9 files · 7 commits · merged 2026-11-26 18:30 UTC

This merges two of ZroIsHere's draft runtime PRs, each with a maintainer fix on top and a new test.

Claim: #1608 (item INTEGRATION-ZRO-DRAFTS)

Included

  • #2315 by ZroIsHere: the runtime now owns the executable's thread-local exit callback (_register_thread_local_exe_atexit_callback). The host UCRT accepts one callback per process and fast-fails on the second one. gta6rt's own startup registers the first, so on Windows the title stopped at boot. Under Wine, the title's callback replaced gta6rt's own and was never called. This removes the cause.
  • #2372 by ZroIsHere: scripted input in the build without SDL3 keeps every press in the reading history, even when the title does not read in between. Also adds LEONIDA_INPUT_TRACE (a reading trace), an A count in LEONIDA_INPUT_STATS, and -FirstRunUi in scripts/windows-boot/boot.ps1.

Maintainer changes

On #2315:

  • The callback is called with its real type, _tls_callback_type (void (__stdcall*)(void*, unsigned long, void*) in UCRT process.h), as (nullptr, DLL_PROCESS_DETACH, nullptr). The PR called it as void (*)(), so the reason argument was whatever rdx held.
  • exit and _cexit call it once, before the atexit handlers, as the UCRT does. The leaving thread's thread_local objects end before the static ones. The PR ran it after the static destructors, and not in _cexit.
  • The extra call on every thread detach is dropped. The loader already calls the same function from the executable's TLS directory on thread detach, and the UCRT never calls the registered copy there. The call after a returning entry point is dropped too.
  • Test: run_tiny_atexit gains thread_local_exit and thread_local_cexit.

On #2372:

  • The detached 8 ms clock thread is replaced. That thread never ended, took the input lock every 8 ms, and kept more or fewer edges depending on how the host scheduled it. update() now adds one reading for every start and end of a press since the last update, stamped with the script's time. The history is the same however often the title reads, and no thread runs. This removes the cause; the thread worked around it.
  • LEONIDA_INPUT_TRACE is documented in docs/gameinput.md and runtime/README.md.
  • Test: gameinput_no_sdl_script gains y@10, a press the title never reads while it is down.

Left out

Nothing.

Tests

On Linux, Mesa 26.3.1 RADV, Ryzen 9 7950X3D integrated GPU (gfx1036):

  • New tests fail on main bdbc7e923 and pass on this branch: run_tiny_atexit (on main, the thread_local destructor never runs under --run) and gameinput_no_sdl_script (4 history checks fail on main).
  • Windows suite under Wine: wine-staging 11.22, clang-cl + xwin build. 422 pass, 23 fail, 39 skip. The baseline on main bdbc7e923 is 421 pass, 24 fail. No test went from pass to fail. The one change is xmem_no_write_watch, from fail to pass, a known flake.
  • Linux runtime build and ctest: 112 of 112 pass, 2 skipped.
  • Python checks: tests/test_ci_registration.py, all tests/test_docs_*.py, tests/test_text_write_line_endings.py, scripts/check_no_game_data.py on the changed files and git diff --check all pass.

Not tested

  • Real Windows and MSVC.
  • The title through --run (a game run follows from this build).
  • boot.ps1 -FirstRunUi.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

**@⁠aslan2142** on GitHub · 2026-11-26 18:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2395) `aslan2142:integration/tls-exit-and-scripted-input` → `main` · +205 −39 · 9 files · 7 commits · merged 2026-11-26 18:30 UTC This merges two of ZroIsHere's draft runtime PRs, each with a maintainer fix on top and a new test. Claim: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 (item `INTEGRATION-ZRO-DRAFTS`) ## Included - **#2315** by ZroIsHere: the runtime now owns the executable's thread-local exit callback (`_register_thread_local_exe_atexit_callback`). The host UCRT accepts one callback per process and fast-fails on the second one. gta6rt's own startup registers the first, so on Windows the title stopped at boot. Under Wine, the title's callback replaced gta6rt's own and was never called. This removes the cause. - **#2372** by ZroIsHere: scripted input in the build without SDL3 keeps every press in the reading history, even when the title does not read in between. Also adds `LEONIDA_INPUT_TRACE` (a reading trace), an `A` count in `LEONIDA_INPUT_STATS`, and `-FirstRunUi` in `scripts/windows-boot/boot.ps1`. ## Maintainer changes On #2315: - The callback is called with its real type, `_tls_callback_type` (`void (__stdcall*)(void*, unsigned long, void*)` in UCRT `process.h`), as `(nullptr, DLL_PROCESS_DETACH, nullptr)`. The PR called it as `void (*)()`, so the reason argument was whatever `rdx` held. - `exit` and `_cexit` call it once, before the atexit handlers, as the UCRT does. The leaving thread's `thread_local` objects end before the static ones. The PR ran it after the static destructors, and not in `_cexit`. - The extra call on every thread detach is dropped. The loader already calls the same function from the executable's TLS directory on thread detach, and the UCRT never calls the registered copy there. The call after a returning entry point is dropped too. - Test: `run_tiny_atexit` gains `thread_local_exit` and `thread_local_cexit`. On #2372: - The detached 8 ms clock thread is replaced. That thread never ended, took the input lock every 8 ms, and kept more or fewer edges depending on how the host scheduled it. `update()` now adds one reading for every start and end of a press since the last update, stamped with the script's time. The history is the same however often the title reads, and no thread runs. This removes the cause; the thread worked around it. - `LEONIDA_INPUT_TRACE` is documented in `docs/gameinput.md` and `runtime/README.md`. - Test: `gameinput_no_sdl_script` gains `y@10`, a press the title never reads while it is down. ## Left out Nothing. ## Tests On Linux, Mesa 26.3.1 RADV, Ryzen 9 7950X3D integrated GPU (gfx1036): - **New tests fail on `main` bdbc7e923 and pass on this branch:** `run_tiny_atexit` (on `main`, the `thread_local` destructor never runs under `--run`) and `gameinput_no_sdl_script` (4 history checks fail on `main`). - **Windows suite under Wine:** wine-staging 11.22, clang-cl + xwin build. 422 pass, 23 fail, 39 skip. The baseline on `main` bdbc7e923 is 421 pass, 24 fail. No test went from pass to fail. The one change is `xmem_no_write_watch`, from fail to pass, a known flake. - **Linux runtime build and ctest:** 112 of 112 pass, 2 skipped. - **Python checks:** `tests/test_ci_registration.py`, all `tests/test_docs_*.py`, `tests/test_text_write_line_endings.py`, `scripts/check_no_game_data.py` on the changed files and `git diff --check` all pass. ## Not tested - Real Windows and MSVC. - The title through `--run` (a game run follows from this build). - `boot.ps1 -FirstRunUi`. AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
#2315 kept `_register_thread_local_exe_atexit_callback` away from the host UCRT, which fast-fails a second
registration in the process (gta6rt's own startup makes the first). Review points fixed on top:

- The callback is a `_tls_callback_type` (UCRT process.h: `void (__stdcall*)(void*, unsigned long, void*)`), not
  `void (*)()`: vcruntime's TLS destructor callback reads its reason argument, which was left as whatever rdx held.
  It is now called as `(nullptr, DLL_PROCESS_DETACH, nullptr)`, as the UCRT does.
- `exit` and `_cexit` call it before the atexit table, as the UCRT does: the leaving thread's thread_local objects end
  before the static ones ([basic.start.term]). #2315 ran it after the static destructors, and not in `_cexit`.
- The extra call on every thread detach is dropped, with the per-thread flag and the loader's include of the process
  shim: the loader already calls the same function from the executable's TLS directory with DLL_THREAD_DETACH, and
  the UCRT never calls the registered one there. The call after a returning entry point is dropped too (MSVC's
  mainCRTStartup calls exit itself).
- run_tiny_atexit gains thread_local_exit and thread_local_cexit: a thread_local object of the main thread must end
  before the handlers and destructors. On main the --run path never ends it (Wine) or stops at the second
  registration (Windows).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
#2372 started a detached thread that took the input lock every 8 ms, so that a scripted press that begins and ends
while the title is not reading reaches the reading history. That works around the cause and adds its own: the
history then depends on how the host schedules a Sleep(8) thread (a slow or loaded machine keeps fewer edges, and a
Windows timer tick of 15.6 ms drops a short hold), the thread never ends, and it contends with the title's reads.

The cause is that update() looked only at the state at the moment of the read. It now first adds a reading for every
start and end of a press since the last update, stamped with the script's time, then the state at the read: the
history is the same however often the title reads, on any machine, and no thread runs. The press log lines carry the
script's time of the press.

Also: LEONIDA_INPUT_TRACE (added by #2372) is documented in docs/gameinput.md and runtime/README.md, as
tests/test_docs_env_vars.py requires. gameinput_no_sdl_script gains y@10, which the title never reads while it is down
(reads at 4.5 s and 60 s): the press and its release must be in the history with their own timestamps. On main the
history has no Y reading.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 18:35:54 +00:00
github manually merged commit 657808a7df into gh-pr/2395/base 2026-11-26 18:35:56 +00:00
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-26 18:30 UTC · original

Merging. This is a self-merge of an integration batch.

  • Included: #2315 and #2372 (@⁠ZroIsHere), each with a maintainer commit on top; the description says what changed and why.
    • #2315: the executable's thread-local exit callback is now called once, with its real signature, before the exit handlers in exit and _cexit. The extra call on every thread detach is gone, since the loader already makes that call from the TLS directory.
    • #2372: no clock thread. update() reports one reading per press start and end since the last read, stamped with the script's own time, so a replay does not depend on how the host schedules a thread. LEONIDA_INPUT_TRACE is documented.
  • Tested on main bdbc7e923 + this branch (657808a7d), integrated RDNA2 GPU (RADV, Mesa 26.3.1):
    • Windows suite under Wine (clang-cl + xwin): 422 pass and 23 fail, against 421 and 24 on main. No test went from pass to fail.
    • Linux runtime ctest: 112/112 pass.
    • Each PR has a new test that fails on main and passes here.
    • test_ci_registration.py, the docs tests, the line-ending test and check_no_game_data.py pass.
  • Not tested: real Windows with MSVC, boot.ps1 -FirstRunUi, and the title itself. A boot and exit check with the title from this main follows here; if it shows a problem in the exit path, a revert PR comes first.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

**@⁠aslan2142** on GitHub · 2026-11-26 18:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2395#issuecomment-7200533369) Merging. This is a self-merge of an integration batch. - **Included:** #2315 and #2372 (@⁠ZroIsHere), each with a maintainer commit on top; the description says what changed and why. - #2315: the executable's thread-local exit callback is now called once, with its real signature, before the exit handlers in `exit` and `_cexit`. The extra call on every thread detach is gone, since the loader already makes that call from the TLS directory. - #2372: no clock thread. `update()` reports one reading per press start and end since the last read, stamped with the script's own time, so a replay does not depend on how the host schedules a thread. `LEONIDA_INPUT_TRACE` is documented. - **Tested on `main` `bdbc7e923` + this branch** (`657808a7d`), integrated RDNA2 GPU (RADV, Mesa 26.3.1): - Windows suite under Wine (clang-cl + xwin): 422 pass and 23 fail, against 421 and 24 on `main`. No test went from pass to fail. - Linux runtime `ctest`: 112/112 pass. - Each PR has a new test that fails on `main` and passes here. - `test_ci_registration.py`, the docs tests, the line-ending test and `check_no_game_data.py` pass. - **Not tested:** real Windows with MSVC, `boot.ps1 -FirstRunUi`, and the title itself. A boot and exit check with the title from this `main` follows here; if it shows a problem in the exit path, a revert PR comes first. AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-26 18:49 UTC · original

Native Windows 11 x64, MSVC 19.44.35229, Release build. All three focused CTests passed on merged main e57bc63bc5: run_tiny_atexit, gameinput_no_sdl, and gameinput_no_sdl_script.

I also built the runtime from the integration's first parent, f3a50404c0, with the same three test files. The two TLS cases show why checking exit codes alone would miss the regression.

Synthetic fixture Native Windows EXE Parent loader Merged loader
thread_local_exit exit 13, one TLS destructor exit 13, zero TLS destructors exit 13, one TLS destructor
thread_local_cexit exit 14, one TLS destructor exit 14, zero TLS destructors exit 14, one TLS destructor

On the merged build, all ten termination paths matched the native output sequence, including the TLS destructor running before the exit handlers. The parent also failed four scripted input history assertions. The merged no-SDL build retained both edges of a Y press at 10.0–10.3 s across reads at 4.5 and 60 s, with the expected timestamps and forward/backward history. The unscripted input control passed on both builds.

These runs used the repository's synthetic fixtures. They cover the callback and input history on native Windows. Game boot and the first-run UI remain separate checks.

**@⁠ecox982745** on GitHub · 2026-11-26 18:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2395#issuecomment-7200890090) Native Windows 11 x64, MSVC 19.44.35229, Release build. All three focused CTests passed on merged main `e57bc63bc5`: `run_tiny_atexit`, `gameinput_no_sdl`, and `gameinput_no_sdl_script`. I also built the runtime from the integration's first parent, `f3a50404c0`, with the same three test files. The two TLS cases show why checking exit codes alone would miss the regression. | Synthetic fixture | Native Windows EXE | Parent loader | Merged loader | | --- | --- | --- | --- | | `thread_local_exit` | exit 13, one TLS destructor | exit 13, zero TLS destructors | exit 13, one TLS destructor | | `thread_local_cexit` | exit 14, one TLS destructor | exit 14, zero TLS destructors | exit 14, one TLS destructor | On the merged build, all ten termination paths matched the native output sequence, including the TLS destructor running before the exit handlers. The parent also failed four scripted input history assertions. The merged no-SDL build retained both edges of a Y press at 10.0–10.3 s across reads at 4.5 and 60 s, with the expected timestamps and forward/backward history. The unscripted input control passed on both builds. These runs used the repository's synthetic fixtures. They cover the callback and input history on native Windows. Game boot and the first-run UI remain separate checks.
This discussion has been locked. Commenting is limited to contributors.
No description provided.