SDL: Fixed crash when the HIDAPI Xbox One rumble callbacks use the device context

From 79306fb73a5e86c7d9ef766edcbab209d0df9a4b Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 28 Oct 2026 15:00:44 -0700
Subject: [PATCH] Fixed crash when the HIDAPI Xbox One rumble callbacks use the
 device context

---
 src/joystick/hidapi/SDL_hidapijoystick.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c
index 5c2f331c13993..c2cfd184dbdb0 100644
--- a/src/joystick/hidapi/SDL_hidapijoystick.c
+++ b/src/joystick/hidapi/SDL_hidapijoystick.c
@@ -453,6 +453,11 @@ static void HIDAPI_CleanupDeviceDriver(SDL_HIDAPI_Device *device)
         HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
     }
 
+    // Make sure the rumble thread is done with this device
+    while (SDL_GetAtomicInt(&device->rumble_pending) > 0) {
+        SDL_Delay(10);
+    }
+
     if (device->driver->FreeDevice) {
         device->driver->FreeDevice(device);
     }
@@ -1027,11 +1032,6 @@ static void HIDAPI_DelDevice(SDL_HIDAPI_Device *device)
 
             HIDAPI_CleanupDeviceDriver(device);
 
-            // Make sure the rumble thread is done with this device
-            while (SDL_GetAtomicInt(&device->rumble_pending) > 0) {
-                SDL_Delay(10);
-            }
-
             for (i = 0; i < device->num_children; ++i) {
                 device->children[i]->parent = NULL;
             }