Search Unity

Question How to properly stop game server

Discussion in 'Game Server Hosting' started by Lemior, Aug 12, 2023.

  1. Lemior

    Lemior

    Joined:
    Nov 12, 2020
    Posts:
    15
    Hi,

    im currently running my netcode server build with unity game server hosting.
    My problem is that the server takes very long to stop after the game has finished.
    After calling these two functions
    Code (CSharp):
    1. NetworkManager.Singleton.Shutdown();
    2. Environment.Exit(0);
    The server is in the state allocated for about 1 min and after that in the state online for also about 1 min. This is way to long. When i manually stop it it takes less than a second to go to online and about 5s to go to available.
    At the moment i always have to manually stop the server so that i dont have to wait for 2 min to test my game again.

    In the server lifecycle documentation (https://docs.unity.com/ugs/en-us/manual/game-server-hosting/manual/concepts/server-lifecycle) it states the following

    1. ...
    2. Exit build executable: The build executable automatically exits with a code 0 when it detects that the game session has been completed and all players have disconnected.
    3. Deallocate server: Game Server Hosting automatically deallocates the game server when it detects that the build executables exits with code 0.
    4. Server becomes idle: The game server becomes idle because it’s not allocated.
    5. ...

    Am i doing something wrong?

    Thank you for your help.
     
  2. INTERBRAIN

    INTERBRAIN

    Joined:
    Aug 14, 2020
    Posts:
    2
    Did you try Application.Quit() on server side?
     
  3. Lemior

    Lemior

    Joined:
    Nov 12, 2020
    Posts:
    15
    I tried it:

    Code (CSharp):
    1. NetworkManager.Singleton.Shutdown();
    2. Application.Quit();
    3. Environment.Exit(0);
    but now it takes at least the same time or maybe also longer than before.
    This does not change anything too.

    Code (CSharp):
    1. NetworkManager.Singleton.Shutdown();
    2. Environment.Exit(0);
    3. Application.Quit();
     
  4. jackward84

    jackward84

    Joined:
    Jan 26, 2017
    Posts:
    87
    Are you sure you don't have something blocking shutdown? Application.quitting event handlers or wantsToQuit come to mind. I think hanging async processes can block shutdown too but I can't remember.

    It could very well be a bug but I find when I call Application.Quit() the server page updates basically instantly.
     
  5. Lemior

    Lemior

    Joined:
    Nov 12, 2020
    Posts:
    15
    Hi,
    thanks for your reply.

    I'll look if i find anything that blocks the shutdown.
    Which versions do u use? Im using the following:

    Unity 2022.3.7
    Multiplay 1.0.5
    Multiplayer Tools 1.0.0
    Netcode for Gameobjects 1.5.2
    Matchmaker 1.0.0
     
  6. Lemior

    Lemior

    Joined:
    Nov 12, 2020
    Posts:
    15
    I found the cause. I have am async task that updates the server query handler every 100ms. For some reason this does not get canceled.
     
    CAwesome likes this.
  7. CAwesome

    CAwesome

    Joined:
    Nov 28, 2014
    Posts:
    10
    Hey,
    I am in a similar situation. This is the code I have now..

    Code (CSharp):
    1. public async void ShutDownServer() {
    2.             Log.Info("start to call shutdown server");
    3.             await FindObjectOfType<PP_BackFillTicketManager>().ServerIsShuttingDown();
    4.             await FindObjectOfType<PP_BasicSpawner>()._runner.Shutdown();
    5.             await MultiplayService.Instance.UnreadyServerAsync();
    6.             Log.Info("finished all shutting down, backfill ticket deletions, unready server, etc.");
    7.             ExitApp();
    8.         }
    9.  
    10.         private void ExitApp() {
    11.             Log.Info("will call exit app");
    12.             Application.Quit(0);
    13.             Environment.Exit(0);
    14.             Log.Info("did call exit app");
    15.         }
    The code get's called, I can see that in the log.
    upload_2024-1-26_16-27-43.png
    And it shuts down but does not deallocate properly. After a while it complains SQP isn't running and it gets shut down

    upload_2024-1-26_16-27-14.png
    It works but not correctly. Anyone got ideas as to what I could try to get it working properly?

    Cheers