Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnApplicationShutdown behaviour #2802

Closed
imerr opened this issue Jun 29, 2021 · 2 comments
Closed

OnApplicationShutdown behaviour #2802

imerr opened this issue Jun 29, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@imerr
Copy link
Contributor

imerr commented Jun 29, 2021

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?

@miwarnec miwarnec added the enhancement New feature or request label Apr 3, 2022
@miwarnec
Copy link
Collaborator

miwarnec commented Apr 3, 2022

@imerr so if we use OnDestroy in NM and Transport, this is solved?

so if OnAppQuit cancels the quit, ondestory is never called?

@imerr
Copy link
Contributor Author

imerr commented Apr 3, 2022

@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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants