Skip to content

FIX: [IN-158913] Fixed action state monitors left behind when disposed under another InputManager - #2511

Draft
josepmariapujol-unity wants to merge 1 commit into
developfrom
fix/in-158913-stale-action-state-monitors
Draft

josepmariapujol-unity wants to merge 1 commit into
developfrom
fix/in-158913-stale-action-state-monitors

Conversation

@josepmariapujol-unity

Copy link
Copy Markdown
Collaborator

Description

With domain reload disabled, stopping Play Mode while an InputTestFixture play mode test is running can leave the editor throwing a NullReferenceException from InputActionState+BindingState.get_actionIndex on every mouse move in the next Play Mode session. Three asserts come first each time: Map index out of range, Control index out of range and Binding index out of range.

The cause is that InputActionState adds its state change monitors to InputSystem.manager and later removes them from InputSystem.manager. These are not always the same manager. When the project has a UI Toolkit panel in Play Mode:

  1. The InputForUI provider (InputSystemProvider) enables the UI map of its own DefaultInputActions on the real InputManager.
  2. The test's InputTestFixture.Setup installs a temporary InputManager.
  3. When Play Mode is stopped, the ExitingPlayMode handlers run in subscription order. UI Toolkit's handler was registered when the editor loaded, so it runs first and shuts down the provider. The provider disables its UI map and disposes DefaultInputActions while the fixture's manager is still installed. The monitors are removed from the fixture's manager, which doesn't have them, and the action state's memory is freed.
  4. The fixture's handler then restores the real manager, which still holds the monitors (36 in the reported case).
  5. With domain reload disabled, the real manager survives into the next Play Mode session, and the first mouse event fires the monitors into the freed state.

InputActionState now records the InputManager it installs its monitors in, and removes monitors and timeouts from that manager. If none of the state's controls are enabled, enabling picks the current manager as before. If some are already enabled, new monitors join the existing ones in the same manager, so all of them can be removed together later.

The initial-state-check onBeforeUpdate hook still uses InputSystem.manager. It cannot cause these errors, so it is not part of this change.

Testing status & QA

  • Added CoreTests.Actions_DisposingActionWhileAnotherInputManagerIsInstalled_RemovesItsStateMonitors. It enables an action, installs a second InputManager, disposes the action, switches back, checks that no monitor of the disposed state is left, and presses the bound button. Without the fix it fails because the disposed state is still monitoring the original manager.
  • Ran the full non-performance PlayMode suite locally on macOS with 6000.0.69f1: 1,845 passed, 0 failed, 75 skipped.
  • Ran the EditMode suite: 2 failures in InputActionsEditorTests (editor window focus and rename timing). The same class also has failures on develop without this change, so they appear unrelated.
  • Not yet verified against the original repro project (IN-158913). QA: enable Enter Play Mode Options with domain reload disabled, put a UI Toolkit UIDocument in the scene, start an InputTestFixture [UnityTest] that yields for a while, stop Play Mode during the test, then enter Play Mode again and move the mouse over the Game view. No errors should be logged.

Overall Product Risks

  • Complexity: Low. The change only affects which manager an action state's monitors and timeouts are removed from, plus the manager new monitors go to while some are already installed.
  • Halo Effect: Medium. Every action enable and disable goes through this code. The only behaviour change is when managers are swapped (InputTestFixture, remoting tests): new monitors for a state that already has monitors in another manager now join them instead of going to the current manager.

Comments to reviewers

  • HasEnabledControls() scans the enabled-controls bitfield once per EnableControls call. That is one 32-bit word per 32 controls, and it runs only when actions are enabled.
  • m_MonitorManager is not cleared when every control is disabled, so StopTimeout calls made after DisableControls still go to the manager that has the timeouts.
  • Related, but not changed here: InputSystemProvider.Shutdown unsubscribes from InputSystem.onActionsChange on whichever manager is current, so the real manager can keep a callback into a provider that has been shut down.

This description, the fix and the test were written by an AI agent (Claude Code, Claude Opus 5.5) working for the PR author.

Checklist

Before review:

  • Changelog entry added.
    • Explains the change in Changed, Fixed, Added sections.
    • For API change contains an example snippet and/or migration example.
    • JIRA ticket linked, example (case %%). If it is a private issue, just add the case ID without a link.
    • Jira port for the next release set as "Resolved".
  • Tests added/changed, if applicable.
    • Functional tests Area_CanDoX, Area_CanDoX_EvenIfYIsTheCase, Area_WhenIDoX_AndYHappens_ThisIsTheResult.
    • Performance tests.
    • Integration tests.
  • Docs for new/changed API's.
    • Xmldoc cross references are set correctly.
    • Added explanation how the API works.
    • Usage code examples added.
    • The manual is updated, if needed.

During merge:

  • Commit message for squash-merge is prefixed with one of the list:
    • NEW: ___.
    • FIX: ___.
    • DOCS: ___.
    • CHANGE: ___.
    • RELEASE: 1.1.0-preview.3.

🤖 Generated with Claude Code

…d under another InputManager

InputActionState removed its state change monitors and timeouts from
InputSystem.manager, which is not always the manager they were added to.
When Play Mode stopped during an InputTestFixture test, UI Toolkit shut
down the InputForUI provider before the fixture restored the real
InputManager. The provider's actions were disposed and their memory
freed, but their monitors stayed in the real manager. With domain reload
disabled, every mouse move in the next Play Mode session then threw a
NullReferenceException from the freed InputActionState.

The state now records the manager it installs monitors in and removes
monitors and timeouts from that manager.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant