Skip to content

Fix SdlGamepad/SdlJoystick state going stale with no event subscribers - #2605

Merged
Perksey merged 2 commits into
dotnet:mainfrom
vchelaru:fix/sdlgamepad-stale-button-state
Jul 27, 2026
Merged

Fix SdlGamepad/SdlJoystick state going stale with no event subscribers#2605
Perksey merged 2 commits into
dotnet:mainfrom
vchelaru:fix/sdlgamepad-stale-button-state

Conversation

@vchelaru

@vchelaru vchelaru commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #2604

SdlGamepad and SdlJoystick's button/axis/trigger/hat handlers wrote the internal state array update as a side effect inside the argument list of the ?.Invoke(...) call for their change events (ButtonDown, ButtonUp, TriggerMoved on SdlGamepad; AxisMoved, HatMoved, ButtonDown, ButtonUp on SdlJoystick). C#'s null-conditional operator short-circuits the whole call, including evaluating the arguments, when the event has no subscribers. So if nobody's subscribed to those events, the array never actually gets updated, and polling the corresponding state (Buttons, Triggers, Axes, Hats) reads permanently stale data even with real input on the device.

ThumbstickMoved on SdlGamepad already did this correctly (array assignment as its own statement before the event fires), so this brings the rest in line with that same shape.

Full writeup, repro steps, and confirmation this affects the latest release/main (but not GlfwGamepad, which rewrites state unconditionally every frame) are in #2604.

…bers

The array assignment (_buttons[i] = new Button(...), _triggers[i] = new Trigger(...)) was written inside the argument list of the ButtonDown/ButtonUp/TriggerMoved ?.Invoke(...) call. C#'s null-conditional operator short-circuits the whole call, including evaluating its arguments, when the event has no subscribers. So with nobody subscribed, the internal arrays never actually got updated, and polling Buttons/Triggers read permanently stale state even with real button presses.

Moves the assignment to its own statement before the event fires, so it always runs regardless of subscribers. See issue dotnet#2604 for the full writeup and repro.
SdlJoystick.cs had the exact same bug for AxisMoved, HatMoved, ButtonDown, and ButtonUp: the array assignment was inside the ?.Invoke(...) argument list, so it never ran with zero subscribers. Same fix, moved to its own statement before the event fires.

Also dropped the local variables from the SdlGamepad fix in favor of just re-reading the array (matches how ThumbstickMoved, the one case that was already correct, does it).
@vchelaru vchelaru changed the title Fix SdlGamepad button/trigger state going stale with no event subscribers Fix SdlGamepad/SdlJoystick state going stale with no event subscribers Jul 27, 2026
@vchelaru

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree [company="{your company}"]

@dotnet-policy-service agree

@Perksey Perksey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good spot, thanks!

@github-project-automation github-project-automation Bot moved this from Todo to In Progress in Silk.NET Development Tracker Jul 27, 2026
@Perksey
Perksey enabled auto-merge (squash) July 27, 2026 04:18
@Perksey
Perksey merged commit d1e0877 into dotnet:main Jul 27, 2026
2 of 3 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Silk.NET Development Tracker Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

SdlGamepad should update internal button/trigger states even if no events are subscribed

2 participants