handzlikchris / handzlikchris/FastScriptReload

Editor Crash when quitting while in Playmode

Aperta
#160 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
bug
Lingua principale
C#
Stelle
2.2k
Fork
167
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

So when closing the UnityEditor while in playmode, the Finalizer of FastScriptReloadManager causes Unity to Crash.

Here is the Stack Trace from the Unity Log:
```
=================================================================
Managed Stacktrace:
=================================================================
at <0xffffffff>
at UnityEngine.DebugLogHandler:Internal_Log <0x000f9>
at UnityEngine.DebugLogHandler:LogFormat <0x000da>
at UnityEngine.Logger:Log <0x001dd>
at ImmersiveVrToolsCommon.Runtime.Logging.LoggerScoped:LogInternal <0x000eb>
at ImmersiveVrToolsCommon.Runtime.Logging.LoggerScoped:LogError <0x00072>
at FastScriptReload.Editor.FastScriptReloadManager:Finalize <0x00172>
at System.Object:runtime_invoke_virtual_void__this__ <0x0018b>
=================================================================
```

The problem is that LoggerScoped uses `UnityEngine.Debug.unityLogger.Log` in the background, which is `null` as soon as Unity is quitting... An exception in that state causes Unity to hard-crash...
```csharp
private static void LogInternal(LogType logType, object message, Object context)
{
UnityEngine.Debug.unityLogger.Log(logType, string.Empty, (object) (LoggerScoped.LogPrefix + message), context);
}
```

To fix the issue I created a Flag that is set as reaction to the quitting Event of the Editor Application.
When the flag is set during deconstruction, it exits early and makes no calls to the LoggerScoped.
Cleanup of the FileWatchers should be savely ommittable in this instance as the process is shutting down anyway. The watchers being IDisposables should automatically dispose themselves.

```diff
@@ -239,6 +239,10 @@ static FastScriptReloadManager()
//do not add init code in here as with domain reload turned off it won't be properly set on play-mode enter, use Init method instead
EditorApplication.update += Instance.Update;
EditorApplication.playModeStateChanged += Instance.OnEditorApplicationOnplayModeStateChanged;
+ EditorApplication.quitting += () =>
+ {
+ _editorIsClosing = true;
+ };

///if is enabled, disable auto reload automatically when launching editor. Will be enabled automatically when adding file watcher manually
if ((bool)FastScriptReloadPreference.WatchOnlySpecified.GetEditorPersistedValueOrDefault() && SessionState.GetBool("NEED_EDITOR_SESSION_INIT", true))
@@ -248,8 +252,10 @@ static FastScriptReloadManager()
}
}

+ private static bool _editorIsClosing;
~FastScriptReloadManager()
{
+ if (_editorIsClosing) return;
LoggerScoped.LogDebug("Destroying FSR Manager ");
if (_instance != null)
{
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Start with FastScriptReloadManager's static initialization and finalizer, then inspect LoggerScoped.LogInternal and the EditorApplication.quitting event described in the issue. Reproduce by quitting the Unity Editor during Playmode; done means shutdown no longer crashes when the manager finalizes, while normal cleanup remains unaffected.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp, unity
Ambito
game-dev, tooling
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.