Skip to content

Move SDL joystick scanning off the main thread#7491

Merged
Goober5000 merged 1 commit into
scp-fs2open:masterfrom
Goober5000:fix/freezespace
Jun 2, 2026
Merged

Move SDL joystick scanning off the main thread#7491
Goober5000 merged 1 commit into
scp-fs2open:masterfrom
Goober5000:fix/freezespace

Conversation

@Goober5000

@Goober5000 Goober5000 commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Setting SDL_HINT_JOYSTICK_THREAD=1 before SDL_InitSubSystem(SDL_INIT_JOYSTICK) tells SDL2 to run joystick polling and HID device-detection on a dedicated background thread, rather than processing them inline inside SDL_PollEvent on the main thread.

Without this hint, every WM_DEVICECHANGE broadcast (which Windows sends when any HID device wakes, sleeps, or pulses the USB bus) causes SDL to walk every HID device class via SetupAPI / cfgmgr32 from inside SDL_PollEvent, stalling the frame loop for multiple seconds on machines with many HID peripherals - especially wireless Logitech mice and keyboards that periodically pulse the bus. Diagnosed via ProcMon stack capture on one affected user's machine, with the slow frame's call chain ending in SDL2.dll -> SETUPAPI.dll -> cfgmgr32.dll -> kernel registry queries against HKLM\System\CurrentControlSet\Enum\HID*.

With the hint enabled, that work happens on SDL's own thread and the main thread just dequeues already-collected events.

In draft pending confirmation via test.

Setting SDL_HINT_JOYSTICK_THREAD=1 before SDL_InitSubSystem(SDL_INIT_JOYSTICK)
tells SDL2 to run joystick polling and HID device-detection on a dedicated
background thread, rather than processing them inline inside SDL_PollEvent
on the main thread.

Without this hint, every WM_DEVICECHANGE broadcast (which Windows sends when
*any* HID device wakes, sleeps, or pulses the USB bus) causes SDL to walk
every HID device class via SetupAPI / cfgmgr32 from inside SDL_PollEvent,
stalling the frame loop for multiple seconds on machines with many HID
peripherals - especially wireless Logitech mice and keyboards that
periodically pulse the bus. Diagnosed via ProcMon stack capture on one
affected user's machine, with the slow frame's call chain ending in
SDL2.dll -> SETUPAPI.dll -> cfgmgr32.dll -> kernel registry queries against
HKLM\System\CurrentControlSet\Enum\HID\*.

With the hint enabled, that work happens on SDL's own thread and the main
thread just dequeues already-collected events.
@Goober5000 Goober5000 added this to the Release 26.0 milestone Jun 1, 2026
@Goober5000 Goober5000 added the fix A fix for bugs, not-a-bugs, and/or regressions. label Jun 1, 2026

@notimaginative notimaginative left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fine, assuming that it resolves the reported problem. I don't believe there is any real downside with this for us whether it solves the problem or not.

I should note as well that this is the default setting for SDL3.

@Goober5000

Copy link
Copy Markdown
Contributor Author

Well according to Joe it does not actually solve the problem. ¯\_(ツ)_/¯

It still looks like a good thing to do though.

@Goober5000 Goober5000 marked this pull request as ready for review June 1, 2026 06:58
@Goober5000 Goober5000 merged commit d52ba4e into scp-fs2open:master Jun 2, 2026
20 checks passed
@Goober5000 Goober5000 deleted the fix/freezespace branch June 2, 2026 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix A fix for bugs, not-a-bugs, and/or regressions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants