Skip to content

Fixing a crash when exiting or re-entering play mode#6296

Open
Codengineer wants to merge 2 commits into
developfrom
bobd/fix_crash_exit_playmode
Open

Fixing a crash when exiting or re-entering play mode#6296
Codengineer wants to merge 2 commits into
developfrom
bobd/fix_crash_exit_playmode

Conversation

@Codengineer
Copy link
Copy Markdown
Contributor

@Codengineer Codengineer commented May 14, 2026

Fixes UUM-135678. The crash occurs because Academy.HandleOnPlayModeChanged only handles ExitingEditMode, not ExitingPlayMode. This means when you exit play mode:

  1. Academy.Dispose() is never called
  2. The Worker (inference engine) with its native memory (CPUTensorData containing JobHandle fences) is never properly disposed

Proposed change(s)

Added ExitingPlayMode handling so the Academy properly disposes all ModelRunner instances (and their Worker objects with native memory) before domain reload occurs. This ensures Academy.Dispose() → ModelRunner.Dispose() → Worker.Dispose() → CPUTensorData.Dispose() all run on the main thread during the ExitingPlayMode phase, before domain reload triggers GC finalization on background threads.

Retested intensively enter-exit play mode cycle and could not get it to crash again while it was crashing almost all the times before.

Useful links (Github issues, JIRA tickets, ML-Agents forum threads etc.)

https://jira.unity3d.com/browse/UUM-135678?filter=-1

Types of change(s)

  • Bug fix
  • New feature
  • Code refactor
  • Breaking change
  • Documentation update
  • Other (please describe)

Checklist

  • Added tests that prove my fix is effective or that my feature works
  • Updated the changelog (if applicable)
  • Updated the documentation (if applicable)
  • Updated the migration guide (if applicable)

Other comments

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