Repository navigation
gameinput: the poll thread no longer revives a force-feedback effect being released (RT-262) - #4102
etcetera-yt wants to merge 1 commit into
Conversation
…being released (RT-262) ForceFeedbackEffect::Release() drops the count to 0 and deletes the effect; its destructor then waits for g().mu to unlink it from the device. In that window apply_effects (holding g().mu) could AddRef the dying effect from 0 to 1, sync it after it was freed, and delete it a second time with its own Release (@AAyberkU's finding). apply_effects now takes a reference only on a live effect (add_ref_if_live: a compare-exchange that never moves a count up from 0), so an effect whose last Release ran is skipped. gameinput_test: add_ref_if_live itself, and 20,000 effects created and released while a second thread runs the poll step. With the old AddRef that run ends in STATUS_HEAP_CORRUPTION (0xC0000374) every time here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Kam471
left a comment
There was a problem hiding this comment.
Tested c5c9ff00 on Windows 11, built with -DLEONIDA_SDL3_FETCH=ON (pinned SDL3 3.4.20) so the shim and its test are compiled in.
gameinput_test.exe-> 305 checks, 0 failures, exit 0.ctest -R gameinput-> 1/1 passed, 1.65 s.- The new race case creates and releases 20,000 force-feedback effects from the title thread while a poll thread calls
rt::gameinput::poll()continuously, then joins and polls once more. test_add_ref_if_livepins both directions of the helper: a live count goes 1 -> 2, and a count of 0 stays 0 with afalsereturn.
The fix is the right shape: apply_effects now takes its reference under s.mu with a compare-exchange that refuses a count of 0, so an effect whose last Release already ran is not revived and cannot be used or freed twice by the poll thread's own Release. The plain AddRef path is unchanged for every effect that is still live. No blocking issue.
|
Thanks @Kam471 for building it with SDL3 and running it. AI-assisted (Claude Code). |
|
Tested c5c9ff0 on Windows 11 26200, MSVC 19.42 (VS 2022 Build Tools), Windows SDK 10.0.28000.0, built with
Reading the change: A real DualSense was connected during the run; it has no force-feedback device ( No blocker found. AI-assisted (Claude Code); no game files. |
|
Thanks @Maraco91 for the run, and for doing the negative control too. AI-assisted (Claude Code). |
|
Reviewed statically (no SDL3/MSVC build): There is no Python coverage for this path, so I could not build a negative control. AI-assisted review, no game files, no GPU. |
|
Thanks @tupacfan04 for the review. AI-assisted (Claude Code). |
|
APPROVE. The test unit-checks the CAS and adds a 20k create/release stress with a concurrent poll thread; it does not assert the skip directly, but the claimed negative control covers it. Merge clean. AI-assisted review, no game files, no GPU. |
|
Thanks @tupacfan04 for the detailed second pass. AI-assisted (Claude Code). |
|
Tested c5c9ff0 merged onto main 03b09a0 on an RTX 5090 (driver 616.92), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, RelWithDebInfo:
AI-assisted (Claude Code). |
|
Thanks @JRamos1993 for the full run on the RTX 5090. AI-assisted (Claude Code). |
Claimed in the coordination issue as: RT-262 (claim; @AAyberkU's finding)
What and why
runtime/shims/gameinput.cpphad a use-after-free and doubledeletewhen a title releases a force-feedback effect while the poll thread runs:ForceFeedbackEffect::Release()dropsrefs_to 0 and callsdelete this. The destructor then waits forg().muto unlink the effect fromdevice_->effects.apply_effectsholdsg().mu, finds the dying effect still in the list, andAddRefs it from 0 to 1.syncs the freed effect, and its ownRelease()deletes it a second time.The fix:
apply_effectstakes a reference only on a live effect, through a newrt::gameinput::add_ref_if_liveingameinput.h. It's a compare-exchange loop that never moves a count up from 0.Releasealready ran is skipped; its destructor unlinks it as before.ForceFeedbackEffectgets a one-lineadd_ref_if_live()that calls it.Nothing is left for this item.
Tested
NVIDIA GeForce RTX 5060 Ti, driver 621.14, Windows 10 19045. VS 2022 Build Tools (MSVC 19.44), Vulkan SDK 1.4.363.0, SDL3 3.4.20 from the pinned
-DLEONIDA_SDL3_FETCH=ON(without SDL3 the shim andgameinput_testaren't built).New checks in
runtime/test/gameinput_test.cpp:add_ref_if_live: a count of 1 goes to 2, and a count of 0 stays 0 and returns false.rt::gameinput::poll()(the test already setsLEONIDA_GAMEINPUT_THREAD=0, so this thread plays the poll thread), with the test's fake haptic backend.gameinput_test.exe305 checks, 0 failuresapply_effectsback to the plainAddRef(main's code), with the new test0xC0000374(STATUS_HEAP_CORRUPTION) during the 20,000-effect loopadd_ref_if_livemade to always add a referenceFAIL add_ref_if_live: a count of 0 stays 0, ...Full build and ctest, both configured with
-DLEONIDA_SDL3_FETCH=ON:aa2988ccc5c9ff00xgameruntime_test.cpp, #3576)d3d12_vulkan_device,d3d12_conservative_rasterfailgameinputThe two failures are the same on main on this Windows 10 host and are unrelated to this PR.
d3d12_vulkan_deviceis TEST-724.Not tested: a real force-feedback wheel (the test uses the fake haptic backend, as the existing force-feedback tests do); the production poll thread itself (the test runs
poll()on its own thread); other GPUs.Checklist
docs/design.md, with how it was confirmed. (n/a: no translator change)AI-assisted (Claude Code).
🤖 Generated with Claude Code