Search Unity

POSSIBLE BUG - Calling UnPairDevices after device disconnect cause indexOutOfRange exc.

Discussion in 'Input System' started by WilliamBenichou, Mar 9, 2020.

  1. WilliamBenichou

    WilliamBenichou

    Joined:
    Apr 11, 2016
    Posts:
    9
    Hi, while working with the new Input System (1.0.0 preview 6, reproduced in 5), I noticed that disconnecting a controller binded to an InputUser with a controlScheme (only optional devices inside) causes an indexOutOfRange exception when calling
    InputUser.PerformPairingWithDevice(device, playerInput.user);


    The stacktrace:
    Code (csharp):
    1. IndexOutOfRangeException: Index was outside the bounds of the array.
    2. (wrapper stelemref) System.Object.virt_stelemref_class_small_idepth(intptr,object)
    3. UnityEngine.InputSystem.Utilities.ArrayHelpers.EraseSliceWithCapacity[TValue] (TValue[]& array, System.Int32& length, System.Int32 index, System.Int32 count) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.6/InputSystem/Utilities/ArrayHelpers.cs:740)
    4. UnityEngine.InputSystem.Users.InputUser.UpdateControlSchemeMatch (System.Int32 userIndex, System.Boolean autoPairMissing) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.6/InputSystem/Plugins/Users/InputUser.cs:1364)
    5. UnityEngine.InputSystem.Users.InputUser.AddDeviceToUser (System.Int32 userIndex, UnityEngine.InputSystem.InputDevice device, System.Boolean asLostDevice, System.Boolean dontUpdateControlScheme) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.6/InputSystem/Plugins/Users/InputUser.cs:1202)
    6. UnityEngine.InputSystem.Users.InputUser.PerformPairingWithDevice (UnityEngine.InputSystem.InputDevice device, UnityEngine.InputSystem.Users.InputUser user, UnityEngine.InputSystem.Users.InputUserPairingOptions options) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.6/InputSystem/Plugins/Users/InputUser.cs:923)
    7. BKPlayerInputMetadata.OnDeviceChange (System.Int32 player, UnityEngine.InputSystem.InputDevice device) (at Assets/Scripts/BK2/Input/BKPlayerInputMetadata.cs:27)
    8. BK2InputManager.Bind (System.Int32 i, UnityEngine.InputSystem.InputDevice device) (at Assets/Scripts/BK2/Input/BK2InputManager.cs:167)
    9. TestInputState.ManageState () (at Assets/Scripts/Sample/TestInputState.cs:40)
    10. BK2GameMode.Update () (at Assets/Scripts/BK2/GameModes/BK2GameMode.cs:72)
    After investigating, I found that
    UpdateControlSchemeMatch()
    in InputUser l.1280 doesn't reset deviceLost count after l.1329.
    So
    s_AllUserData[userIndex].lostDeviceCount
    is equal to 1, and
    s_AllLostDeviceCount
    is equal to 0, leading to the indexOutOfRange after the next action on InputUser.

    Not sure it is a bug, maybe just a misinterpretation of the code resulting of on error on my side.

    Also, thanks for your work guys, it's amazing!
     
  2. WilliamBenichou

    WilliamBenichou

    Joined:
    Apr 11, 2016
    Posts:
    9
    Fix confirmed after replacing:
    Code (CSharp):
    1.  if (s_AllUserData[userIndex].lostDeviceCount > 0)
    2.                                 ArrayHelpers.EraseSliceWithCapacity(ref s_AllLostDevices, ref s_AllLostDeviceCount,
    3.                                     s_AllUserData[userIndex].lostDeviceStartIndex,
    4.                                     s_AllUserData[userIndex].lostDeviceCount);
    by
    Code (CSharp):
    1. if (s_AllUserData[userIndex].lostDeviceCount > 0)
    2.                             {
    3.                                 ArrayHelpers.EraseSliceWithCapacity(ref s_AllLostDevices, ref s_AllLostDeviceCount,
    4.                                     s_AllUserData[userIndex].lostDeviceStartIndex,
    5.                                     s_AllUserData[userIndex].lostDeviceCount);
    6.  
    7.                                 s_AllUserData[userIndex].lostDeviceCount = 0;
    8.                             }
    at line 1328 of InputUser.cs
     
    jdh5259 likes this.
  3. WilliamBenichou

    WilliamBenichou

    Joined:
    Apr 11, 2016
    Posts:
    9
    Still not fixed on InputSystem:1.0.0 (now on line 1322)
     
    Last edited: Jun 23, 2020
  4. jdh5259

    jdh5259

    Joined:
    Sep 14, 2017
    Posts:
    20
    Currently running into a similar issue relating to this as well. Calling RegisterLayoutMatcher causes devices to be recreated and readded to the user (which is fine) but it is resulting in the same error as above.

    I can confirm this is an issue in 1.0.0 and it also appears to still be an issue in 1.1.0-preview.1 as well.

    EDIT: Forgot to mention that the proposed solution above appears to fix this issue.
     
  5. dutchiedave

    dutchiedave

    Joined:
    Feb 11, 2016
    Posts:
    9
    I confirm I have the exact same issue when disconnecting and then reconnecting a device and calling SwitchCurrentControlScheme.

    I also confirm that the fix above seems to fix the problem.

    Has anyone submitted this as a bug on the Unity Issue Tracker?
     
  6. Sparot

    Sparot

    Joined:
    Aug 23, 2020
    Posts:
    9
    Kind of wish I had found this sooner. I'm dealing with a similar issue in the same vein. It appears InputUser is having issues with its lostDevices array. I ended up writing a barebones PlayerInput-style class just to test this issue, and ended up with similar results.

    Interestingly, in my later tests, I found the issue only happens with controllers. Unplugging my keyboard and reconnecting restored keyboard input properly.

    I posted a similar bug here: https://forum.unity.com/threads/cal...ct-causes-an-indexoutofrangeexception.957057/

    I've also submitted this as a bug with the project attached. I linked this thread in the bug report, since it outlines the problem a bit better than I did.
     
    WilliamBenichou likes this.
  7. WilliamBenichou

    WilliamBenichou

    Joined:
    Apr 11, 2016
    Posts:
    9
    Any news on this? Has it been fix? @Sparot could you post the bug report link please?
    We are on a custom modified version of the input system package, this isn't a long term solution obviously.
     
  8. Sparot

    Sparot

    Joined:
    Aug 23, 2020
    Posts:
    9
    Oh, sorry, I havn't checked this in a while. There hasn't been any update on this issue. I'm beginning to wonder if I did not report it correctly.
     
    Last edited: Feb 24, 2021
  9. Sparot

    Sparot

    Joined:
    Aug 23, 2020
    Posts:
    9
    jdh5259 and WilliamBenichou like this.
  10. WilliamBenichou

    WilliamBenichou

    Joined:
    Apr 11, 2016
    Posts:
    9
    jdh5259 likes this.
  11. Sparot

    Sparot

    Joined:
    Aug 23, 2020
    Posts:
    9
    Just got an update on the bug tracker. The issue appears to have been fixed, and should be in the next release of the Input package.
     
    SGJPile and WilliamBenichou like this.