{"record":{"id":"b07d11bae474c43e","repo":"stride3d/stride","slug":"directinput-gamecontroller-already-opened-deviceinstance","errorCode":null,"errorMessage":"DirectInput GameController already opened {deviceInstance.InstanceGuid}/{deviceInstance.InstanceName}","messagePattern":"DirectInput GameController already opened (.+?)/(.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Input/Windows/InputSourceWindowsDirectInput.cs","lineNumber":163,"sourceCode":"                if (!Devices.ContainsKey(device.InstanceGuid))\n                {\n                    OpenDevice(device);\n                }\n            }\n        }\n\n        /// <summary>\n        /// Opens a new game controller or gamepad\n        /// </summary>\n        /// <param name=\"deviceInstance\">The device instance</param>\n        public void OpenDevice(DeviceInstance deviceInstance)\n        {\n            // Ignore XInput devices since they are handled by XInput\n            if (IsXInputDevice(deviceInstance.ProductGuid))\n                return;\n\n            if (Devices.ContainsKey(deviceInstance.InstanceGuid))\n                throw new InvalidOperationException($\"DirectInput GameController already opened {deviceInstance.InstanceGuid}/{deviceInstance.InstanceName}\");\n\n            GameControllerDirectInput controller;\n            try\n            {\n                controller = new GameControllerDirectInput(this, directInput, deviceInstance);\n            }\n            catch (SharpDXException)\n            {\n                // Some failure occured during device creation\n                return;\n            }\n\n            // Find gamepad layout\n            var layout = GamePadLayouts.FindLayout(this, controller);\n            if (layout != null)\n            {\n                // Create a gamepad wrapping around the controller\n                var gamePad = new GamePadDirectInput(this, inputManager, controller, layout);","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Input/Windows/InputSourceWindowsDirectInput.cs#L145-L181","documentation":"InputSourceWindowsDirectInput.OpenDevice throws InvalidOperationException when a DirectInput device with the same InstanceGuid is already present in the Devices dictionary. Scan enumerates devices repeatedly, and opening the same device twice would create duplicate controllers with conflicting state.","triggerScenarios":"Scan() runs while Devices already contains an entry for deviceInstance.InstanceGuid — e.g. two Scan passes without removal, or a device re-enumerated after an unplug that was not deregistered.","commonSituations":"Plugging a controller in and out quickly so the removal queue lags behind rescan; window focus changes triggering rescan while the device is still registered; custom code calling Scan concurrently.","solutions":["Check Devices.ContainsKey(deviceInstance.InstanceGuid) before calling OpenDevice.","Ensure disconnected devices are removed from Devices before rescanning.","Guard against concurrent/redundant Scan calls in custom input code."],"exampleFix":"// before\ninputSource.OpenDevice(deviceInstance);\n// after\nif (!inputSource.Devices.ContainsKey(deviceInstance.InstanceGuid))\n    inputSource.OpenDevice(deviceInstance);","handlingStrategy":"validation","validationCode":"if (!inputSource.Devices.ContainsKey(deviceInstance.InstanceGuid))\n    inputSource.OpenDevice(deviceInstance);","typeGuard":"bool IsOpen(GameControllerDirectInput c, Guid instanceGuid) => c != null && inputSource.Devices.ContainsKey(instanceGuid);","tryCatchPattern":"try\n{\n    inputSource.OpenDevice(deviceInstance);\n}\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"DirectInput GameController already opened\"))\n{\n    // Device already tracked; ignore or refresh its state.\n}","preventionTips":["Check the Devices dictionary before opening a device instance.","Remove unplugged devices from Devices before rescanning.","Serialize Scan calls; avoid overlapping scans on focus change and device arrival."],"tags":["directinput","gamepad","duplicate-device","windows"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}