Skip to content

Fix PLAYTEST: editor crashes when play mode exits (Transform::MainThreadCleanup) - #877

Closed
Myarcer wants to merge 1 commit into
bdunderscore:mainfrom
Myarcer:fix/preview-scene-play-mode-exit-crash
Closed

Myarcer wants to merge 1 commit into
bdunderscore:mainfrom
Myarcer:fix/preview-scene-play-mode-exit-crash

Conversation

@Myarcer

@Myarcer Myarcer commented Nov 25, 2026

Copy link
Copy Markdown

Source

Playtest: Unity 2022.3.22f1, VRChat avatar project, NDMF 1.15.0-beta.5, four scenes loaded additively, Enter Play Mode Options on. The handler is unchanged on main.

Symptom

  • The Unity editor sometimes crashes with a segfault when you exit play mode.
  • The crash is intermittent. The same native stack occurred four times in one month.

Root cause

  • When play mode exits, NDMFPreviewSceneManager closes the preview scene at PlayModeStateChange.ExitingPlayMode (Editor/PreviewSystem/Rendering/NDMFPreviewSceneManager.cs:86-87).
  • Unity then restores the scene backups in EditorSceneManager::RestoreSceneBackups. When a backup finishes integration, Unity sets the active scene, and this fires EditorSceneManager.activeSceneChangedInEditMode.
  • The handler at line 99 calls GetPreviewScene(). The preview scene is no longer valid, so it calls EditorSceneManager.OpenScene(assetPath, OpenSceneMode.Additive) at line 144, in the middle of the restore.
  • That nested load waits for all pending async operations, which include an UnloadUnusedAssetsOperation. Its GarbageCollectSharedAssets step deletes objects while a restored scene is only half loaded, and the editor crashes in Transform::MainThreadCleanup.

Fix

  • The activeSceneChangedInEditMode handler now defers GetPreviewScene() with EditorApplication.delayCall. The preview scene opens on the next editor update, after Unity has finished the restore.
  • GetPreviewScene() returns early when the preview scene is already valid, so several deferred calls from one restore open the scene only once.
  • No public API changes.

For existing users, the preview scene now opens one editor update after any active scene change, including normal scene switches in edit mode, instead of inside the event.

Evidence

Editor log of the crash, on a later play mode exit in the same session:
Opening scene 'Packages/nadena.dev.ndmf/Runtime/___NDMF Preview___.unity additively'
Received signal SIGSEGV

Native stack of the crash (addresses and Mono runtime frames removed):
Transform::MainThreadCleanup
delete_object_internal_step1
BatchDeleteObjectInternal
CleanupUnusedObjects
GarbageCollectSharedAssets
UnloadUnusedAssetsOperation::IntegrateMainThread
PreloadManager::UpdatePreloadingSingleStep
PreloadManager::WaitForAllAsyncOperationsToComplete
RuntimeSceneManager::LoadScene
EditorSceneManager::OpenSceneLoaded
EditorSceneManager::OpenScene
UnityEditor.SceneManagement.EditorSceneManager:OpenScene (string,UnityEditor.SceneManagement.OpenSceneMode)
nadena.dev.ndmf.preview.NDMFPreviewSceneManager:GetPreviewScene () (at ./Packages/nadena.dev.ndmf/Editor/PreviewSystem/Rendering/NDMFPreviewSceneManager.cs:144)
nadena.dev.ndmf.preview.NDMFPreviewSceneManager/<>c:<Init>b__8_3 (UnityEngine.SceneManagement.Scene,UnityEngine.SceneManagement.Scene) (at ./Packages/nadena.dev.ndmf/Editor/PreviewSystem/Rendering/NDMFPreviewSceneManager.cs:99)
UnityEditor.SceneManagement.EditorSceneManager:Internal_ActiveSceneChangedInEditor (UnityEngine.SceneManagement.Scene,UnityEngine.SceneManagement.Scene)
ActiveSceneChanged
RuntimeSceneManager::SetActiveScene
EditorSceneManager::SetActiveScene
RuntimeSceneManager::EndIntegrateMainThread
LoadSceneOperation::IntegrateMainThread
PreloadManager::UpdatePreloadingSingleStep
PreloadManager::WaitForAllAsyncOperationsToComplete
EditorSceneManager::RestoreSceneBackups
PlayerLoopController::ExitPlayMode

Editor log, one play mode exit before the fix (no crash on this exit, timing lines removed).
The preview scene opens before the backups and loads between them:
Opening scene 'Packages/nadena.dev.ndmf/Runtime/___NDMF Preview___.unity additively'
Unloading 19764 unused Assets / (108.0 MB). Loaded Objects now: 54277.
Loaded scene 'Temp/__Backupscenes/1.backup'
Loaded scene 'Temp/__Backupscenes/2.backup'
Loaded scene 'Temp/__Backupscenes/3.backup'
Loaded scene 'Packages/nadena.dev.ndmf/Runtime/___NDMF Preview___.unity'
Loaded scene 'Temp/__Backupscenes/0.backup'

Editor log, one play mode exit after the fix (timing and unrelated lines removed).
All backups load first, then the preview scene opens:
Loaded scene 'Temp/__Backupscenes/0.backup'
Unloading 26883 unused Assets / (52.3 MB). Loaded Objects now: 29235.
Loaded scene 'Temp/__Backupscenes/1.backup'
Loaded scene 'Temp/__Backupscenes/2.backup'
Loaded scene 'Temp/__Backupscenes/3.backup'
Opening scene 'Packages/nadena.dev.ndmf/Runtime/___NDMF Preview___.unity additively'
Loaded scene 'Packages/nadena.dev.ndmf/Runtime/___NDMF Preview___.unity'

The unload after 0.backup in the second log is part of Unity's own restore. NDMF does not call UnloadUnusedAssets, and the preview scene has not opened yet at that point.

Confidence

Medium. The stack shows the crash inside the nested OpenScene call from this handler, and the fix moves that call out of the restore. The limit: I tested the fix on one play mode exit only. The crash is intermittent, so this shows the new load order, not a measured crash rate. I did not add a unit test, because the crash needs a real play mode exit with scene backups.

When play mode exits, NDMF closes the preview scene at ExitingPlayMode. Unity then restores the scene backups in EditorSceneManager::RestoreSceneBackups and sets the active scene while some backups still load. This fires activeSceneChangedInEditMode, and the handler calls GetPreviewScene, which opens the preview scene additively with EditorSceneManager.OpenScene in the middle of the restore. That nested load runs UnloadUnusedAssetsOperation while a restored scene is only half loaded, and the editor can segfault in Transform::MainThreadCleanup. The handler now defers GetPreviewScene with EditorApplication.delayCall, so the preview scene opens after the restore is complete. Seen four times in one month on Unity 2022.3.22f1 with NDMF 1.15.0-beta.5.
@Myarcer

Myarcer commented Nov 25, 2026

Copy link
Copy Markdown
Author

Closing this. A later play mode exit with this change crashed in the same asset cleanup inside RestoreSceneBackups, with no NDMF frame on the stack, so this change does not fix the crash. Sorry for the noise.

@Myarcer Myarcer closed this Nov 25, 2026
@Myarcer
Myarcer deleted the fix/preview-scene-play-mode-exit-crash branch November 25, 2026 10:22
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