Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

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.