fix(host): resolve a reused ThreadType to its newest thread #2457
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2457
Loading…
Reference in a new issue
No description provided.
Delete branch "DuckNintendo/Longshot:pr/02-threadtype-reuse"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
A guest
ThreadTypeaddress can outlive theHosThreadrecord that first used it.hos_thread_ofreturnedthe first registry match, but
threads[]is not in creation order:hos_thread_destroyfills a hole with thelast entry. With two records for one address, the ordinary append order returns the older record, and a
destroy swap makes a last match return it. The new
ThreadTypethen gets the old thread's start state, coreor mask;
StartThreadon an already-started old record silently starts nothing.Two records coexist only when
CreateThreadruns on an address that is still registered; the normalcreate → destroy → free → reuse cycle removes the record first. The fix returns the matching record with the
highest creation id: the record created last on that address, whatever its position. The older record stays
tracked for the handle-based calls and the thread listings.
The bug is not visible by eye; read first
hos_thread_ofinhost/hos_thread.cand the new check. Againstmain,tests/test_thread_lookup.pyfails at the append-order case (creation order -> id 0x51 (want 0x52));on this branch it prints
ok.Testing
Tested: Linux, integrated Radeon GPU, RADV: Hyrule Field at 2560x1440, quality 6, threaded rendering off;
the fixed runtime played two 60 s runs and a 90 s probe (main played the other two 60 s runs).
python -I -B tests/test_thread_lookup.pyfails onmainat the append-order case and passes here; its first-match, last-match and oldest-id mutations each failon the case that catches them. The fixture now takes the runner's
--cc, honorsCCfor a direct run, andbuilds through
tests/compiler_command.py: direct with the default compiler, withCC=clang, with--cc 'cc -m64', and throughpython -I -B tests/run_all.py --jobs 1 test_thread_lookupall pass, andtests/test_run_all_declarations.pypasses.python -I -B tests/run_all.py: 848 passed, 22 failed, 110 skipped; the same22 checks fail on
mainin this environment (missing optional local toolchain pieces), and the new check isthe extra pass. The checks that read the changed files: 26 passed, 1 failed (the same
tests/test_dev_packs.pyfailure as
main), 2 skipped.Still to test:
Measurements
Verdict: no measurable frame-time change; this is a correctness fix, not a performance claim. The scan
visits every entry instead of breaking at the first match, but
thread_arganswers the calling thread's ownThreadTypebefore the scan: a 90 s probe counted 4,352 registry scans with 85 records.Setup: Linux, integrated Radeon GPU, RADV; Hyrule Field at 2560x1440, quality 6, threaded rendering off; 60 s
each, two interleaved runs, median.
8aa5ab876dbff7861eRisks
If the guest reuses an address before calling
DestroyThreadon the old object, that destroy remainsambiguous: the guest passed one address for an object it has already reissued. No ordering rule recovers the
intent; the lookup answers the address's newest record, not a liveness test. No switch; a revert is enough.
Nothing changes for players after updating: saves,
longshot_options.iniand the caches are untouched.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.Generated by opencode with deepseek-v4.1-flash-1017
fix(host): resolve a reused ThreadType to its newest threadto WIP: fix(host): resolve a reused ThreadType to its newest threadWIP: fix(host): resolve a reused ThreadType to its newest threadto fix(host): resolve a reused ThreadType to its newest thread@review
Recommendation: Approve. The lookup fix addresses the tested ordering cases, but its regression test skips or fails on supported setups that have Clang without GCC and ignores CC. Native Windows validation is also still outstanding. Nothing blocking is left: what remains is for the author before merging (below). Recommend approval; not approved on the forge.
The change selects the newest registered thread for a reused guest address, avoiding operations on an older record. It scans the full registry for cross-thread lookups; the reported Linux runs show no frame-time change outside their spread.
Should fix (1): not blocking, but worth doing before merging.
tests/test_thread_lookup.py:23: The test hard-codes GCC and ignores the runner's selected CC, so it does not run on every supported compiler setup. (more on the line) Do: Declare --cc with {cc}, honor CC for direct invocation, and parse compiler commands using the repository's compiler-command helper.Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
host/hos_thread.c:540: The changed host thread lookup has not yet been exercised in a native Windows game run. (more on the line) Do: Build and play on native Windows as listed in Still to test, checking thread behavior and for crashes.@fixcan do L1.Checks: Still running: 9 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes: the author reports the game-free test fails on the base at the append-order case and passes on this head. Two 60-second Linux runs per side show overlapping frame-time and 1% low results; they do not establish a performance gain.
Other systems: The lookup serves thread start, join, destroy, naming, priority and affinity calls on both Windows and Linux; it is independent of GPU vendor. The fixture covers registry ordering but substitutes locks and thread creation, and native Windows play remains untested. Saves, audio, rendering APIs and generated files are not directly changed.
Switches: None added, changed or removed.
Reviewed:
bff7861e19against8aa5ab876d: Read the changed test, diff, commits, repository rules, standards and pull request template; read the thread registry, creation, destruction and callers around the change, plus the test runner and source-cut helper. Skimmed unrelated portions of host/hos_thread.c. The requested docs/review-checklist.md and sibling Tooling checkout are absent; no generated files or recorded runs were changed.; nothing run (a code review).Checked and fine:
Testing: Read-only review; no code, tests or project scripts were run. The author reports 848 passed, 22 base-matching failures and 110 skipped; listed CI checks are still running.
Overlaps and merge order: Unknown; network access is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -537,2 +538,3 @@fills its hole with the last entry. The record created last on the address answers, whatever its position. */for (int i = 0; i < nthreads; i++)if (threads[i]->guest_tt == guest_tt) { r = threads[i]; break; }if (threads[i]->guest_tt == guest_tt && (!r || threads[i]->id > r->id)) r = threads[i];L2 (should fix): The changed host thread lookup has not yet been exercised in a native Windows game run.
When: Windows players use this same lookup and its native synchronization and thread lifecycle, while the reported game runs cover Linux only.
Do: Build and play on native Windows as listed in Still to test, checking thread behavior and for crashes.
@ -0,0 +20,4 @@sys.path.insert(0, str(Path(__file__).resolve().parent)) # (tests/srccut.py: python -I leaves this folder off the path)from srccut import between, block # noqa: E402RUN_ALL_ARGS = ["--cc", "gcc"]L1 (should fix): The test hard-codes GCC and ignores the runner's selected CC, so it does not run on every supported compiler setup.
When: On Windows with llvm-mingw Clang, or Linux with only Clang, run_all skips this regression test when gcc is absent, or fails it in strict mode; setting CC to Clang does not change that.
Do: Declare --cc with {cc}, honor CC for direct invocation, and parse compiler commands using the repository's compiler-command helper.
@fix
I didn't fix this pull request: it doesn't allow edits from maintainers (the author can turn on Allow edits from maintainers).
Automated fix by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
The fixture hard-coded gcc and ignored the runner's --cc, so a supported setup whose compiler is clang, or a CC configured with flags, could not run the check. Declare --cc as {cc}, honor CC for a direct run, and build through tests/compiler_command.py's parser in both cases. Assisted-by: opencode (deepseek-v4.1-flash)L1 is fixed in
ce7d5472(test(host): compile the thread lookup fixture with the selected compiler):RUN_ALL_ARGSnow declares--cc {cc}, so the runner passes the compiler it selected instead of the fixture always usinggcc.CC(falling back togcc, thenclang), and both the runner's value andCCare parsed withtests/compiler_command.py, so a compiler command with flags works too.Verified:
L2 (native Windows) is still in Still to test; no other review point is open. I also enabled "Allow maintainers to edit" on this PR.
Generated by opencode with deepseek-v4.1-flash-1017
@review
Recommendation: Approve. The code is ready: only testing is left. The lookup fix and its regression test address the reported reuse case, and the compiler selection issue is resolved. A native Windows game run and the remaining CI results are still needed before merge. Recommend approval; not approved on the forge.
The lookup chooses the newest thread record for a reused guest address regardless of registry order, preventing calls from using an older record. The scan now visits the whole registry; the reported Linux measurements show no clear frame-time change.
Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
host/hos_thread.c:540: The changed host thread lookup has not yet been exercised in a native Windows game run. When: Windows players use this same lookup and its native synchronization and thread lifecycle, while the reported game runs cover Linux only. Do: Build and play on native Windows as listed in Still to test, checking thread behavior and for crashes.Since the last review (on
bff7861e19)Resolved: L1 (The test hard-codes GCC and ignores the runner's selected CC, so it does not run on every…)
Still open: L2 (The changed host thread lookup has not yet been exercised in a native Windows game run.)
Checks: Still running: 9 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes: the reported game-free test fails on main and passes with the fix, including append and destroy-reordered cases. Two Linux runs on each side showed frame medians of 96.9/95.3 ms before and 93.5/99.2 ms after, within run-to-run variation.
Other systems: The lookup affects host thread operations on Linux and Windows, independently of GPU. Linux play and fixture runs are reported; native Windows play remains untested.
Switches: None.
Reviewed:
ce7d54727cagainst8aa5ab876d: Read the full pull request diff, changed test, compiler helper, repository rules and pull request template; inspected the host thread lookup and its callers. The large host source and runner were only partly inspected; recorded runs were read as reported evidence.; nothing run (a code review).Checked and fine:
Testing: The author reports Linux game runs and passing fixture and runner checks; native Windows testing is outstanding. CI is still running. No commands from the project were run during this review.
Overlaps and merge order: Not checked; network access is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.