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

  1. Modify the source to Remove individual devices instead of Clear/Reset.
  2. Reimplement the custom IInputSource so Devices only raises Add and Remove.
  3. 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

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


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)