OnApplicationShutdown behaviour
Describe the bug When unity receives a shutdown request, it forwards it to MonoBehaviour.OnApplicationShutdown and Application.wantsToQuit callback
This "request" can be cancelled by returning false in the Application.wantsToQuit callback.
Depending on the semi-random (?) ordering mirror may shut down the Transport/NetworkManager even if the request gets cancelled later on.
You may want to cancel the (immediate) shutdown to do a graceful shutdown. Especially for dedicated servers it might be a bit better to tell users the server is shutting down in x seconds, have them disconnect after finishing up and then flush any save state before finally quitting unity for real
[IMPORTANT] How can we reproduce the issue, step by step:
- Add a script:
using UnityEngine;
class ShutdownInterrupter : MonoBehaviour {
void Awake() {
Application.wantsToQuit += () => false;
}
}
- Run a build
- Attempt to close the window or send a SIGINT (on linux)
- Mirror will still shutdown even though the shutdown was cancelled
Expected behavior Mirror should not shut down until the application really quits (via handling OnDestroy).
Additional context See also #923
overriding the NetworkManager.OnApplicationQuit is easy enough, but since Transport.OnApplicationQuit is now added a user will have to subclass a Transport too to change the default behaviour.
The other question is, why not just use OnDestroy to handle cleanup? Is that too late in some cases?
@imerr so if we use OnDestroy in NM and Transport, this is solved?
so if OnAppQuit cancels the quit, ondestory is never called?
@imerr so if we use OnDestroy in NM and Transport, this is solved?
Yes, thats what I'm doing for my things
so if OnAppQuit cancels the quit, ondestory is never called?
Yes, and OnDestroy is also called when the application shuts down, not quite sure why OnApplicationQuit was used though unless it's ordering related (OnApplicationQuit -> OnDisable -> OnDestroy - see https://docs.unity3d.com/Manual/ExecutionOrder.html)