stride3d/stride · error · InvalidOperationException
Unsupported collection operation
Error message
Unsupported collection operation
What it means
InputDevicesOnCollectionChanged handles only Add and Remove actions on a device source's Devices collection; any other NotifyCollectionChangedAction reaches the default case and throws InvalidOperationException, since devices are produced/destroyed, not rearranged.
Solutions
- Modify the source to Remove individual devices instead of Clear/Reset.
- Reimplement the custom IInputSource so Devices only raises Add and Remove.
- Remove the offending source via InputManager.Sources to tear down its devices.
Example fix
// before Devices.Clear(); // triggers Reset -> throw // after foreach (var d in Devices.ToList()) Devices.Remove(d);
Defensive patterns
Strategy: try-catch
Try / catch
try { /* source device updates */ }
catch (InvalidOperationException ex) when (ex.Message == "Unsupported collection operation")
{ logger.Error(ex, "Source raised unsupported Devices change; replacing source"); } Prevention
- Custom sources must raise only Add/Remove on Devices.
- Avoid Clear/Reset on device collections; remove items individually.
- Test custom source hot-plug paths before shipping.
When it happens
Trigger: An IInputSource raising CollectionChanged with Replace, Reset or Move on its Devices collection — e.g. a custom source that Clears or reorders its device list.
Common situations: Custom input sources calling devices.Clear() to unload; data-bound device lists being replaced wholesale; buggy third-party source implementations.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- ActivatedGestures collection was modified but the action…
- Can not set more than one button at a time
- Cannot add null instance of VirtualButton
- Cannot set null instance of VirtualButton
- coreWindow
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/5420785ef23fcaaa.
Report an issue: GitHub.
Appendix: source
Thrown at sources/engine/Stride.Input/InputManager.cs:787
// Set mouse position for first mouse device
if (HasMouse)
{
Mouse.SetPosition(normalizedPosition);
}
}
private void InputDevicesOnCollectionChanged(IInputSource source, TrackingCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
OnInputDeviceAdded(source, (IInputDevice)e.Item);
break;
case NotifyCollectionChangedAction.Remove:
OnInputDeviceRemoved((IInputDevice)e.Item);
break;
default:
throw new InvalidOperationException("Unsupported collection operation");
}
}
private void OnInputDeviceAdded(IInputSource source, IInputDevice device)
{
devices.Add(device);
if (!devicesById.TryAdd(device.Id, device))
throw new InvalidOperationException($"Device with Id {device.Id}({device.Name}) already registered to {devicesById[device.Id].Name}");
if (device is IKeyboardDevice)
{
RegisterKeyboard((IKeyboardDevice)device);
keyboards.Sort((l, r) => -l.Priority.CompareTo(r.Priority));
}
else if (device is IPointerDevice)
{
RegisterPointer((IPointerDevice)device);
pointers.Sort((l, r) => -l.Priority.CompareTo(r.Priority));View on GitHub (pinned to 96fad776d2)