How to properly stop game server

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

NetworkManager.Singleton.Shutdown();
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 (Game server lifecycle) it states the following

  • 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.
  • Deallocate server: Game Server Hosting automatically deallocates the game server when it detects that the build executables exits with code 0.
  • Server becomes idle: The game server becomes idle because it’s not allocated.

Am i doing something wrong?

Thank you for your help.

Did you try Application.Quit() on server side?

I tried it:

NetworkManager.Singleton.Shutdown();
Application.Quit();
Environment.Exit(0);

but now it takes at least the same time or maybe also longer than before.
This does not change anything too.

NetworkManager.Singleton.Shutdown();
Environment.Exit(0);
Application.Quit();

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.

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

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.

1 Like

Hey,
I am in a similar situation. This is the code I have now..

public async void ShutDownServer() {
            Log.Info("start to call shutdown server");
            await FindObjectOfType<PP_BackFillTicketManager>().ServerIsShuttingDown();
            await FindObjectOfType<PP_BasicSpawner>()._runner.Shutdown();
            await MultiplayService.Instance.UnreadyServerAsync();
            Log.Info("finished all shutting down, backfill ticket deletions, unready server, etc.");
            ExitApp();
        }

        private void ExitApp() {
            Log.Info("will call exit app");
            Application.Quit(0);
            Environment.Exit(0);
            Log.Info("did call exit app");
        }

The code get's called, I can see that in the log.
9604808--1361813--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

9604808--1361810--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