question

Brent Batas (Lisk) avatar image
Brent Batas (Lisk) asked

StartGame race condition?

I'm experiencing an odd error where players fail to connect to my game server if it is the first game on that server instance. For subsequent games, it works as expected (players can connect).

I looked at archived games, and noticed that the first game for a given IP would start up successfully, but it would contain no players (and players reported that the Lobby said "game started!" but remained stuck in the lobby rather than the game starting up).

I suspect some kind of race condition where I am calling StartGame() then immediately sending a GameServerReady message (which contains the IP & port of the game server to connect to) and the clients are trying to connect before that server is ready.

Does that sound like it is the case?

If so, how can I wait for the server started by StartGame() to be "ready"?

Or should it be the client's responsibility to retry every few seconds?

apisMatchmaking
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
brendan avatar image
brendan answered

When you call Matchmake or StartGame, any server instance started (assuming your min available instances and max instances per server host are set appropriately, so that you're not waiting on a server host to spin up) is kicked off immediately. So by the time the client gets the response and can make a connection to the server, the executable has already been started. What you'll want to check is, what is the startup time for your exe? That can vary considerably, depending on your code. One best practice, of course, is to start your communication logic on a separate thread from your main game logic, so that it can manage user connections without delays due to resource loads. But libraries that are being loaded for the first time may cause delays, depending on your game engine. I'd do some profiling on this locally, to get a feel for how long that is, and if there's anything you can do to improve that time. If you've exhausted your options but still have a lag before the server is ready, you may want to add a pause on the client before it attempts to connect, or a retry.

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Brent Batas (Lisk) avatar image Brent Batas (Lisk) commented ·

Thanks for the reply. That makes sense.

The startup time for my exe is about 1 second (or maybe a little less). My client already attempts to retry - I locally tested this by running my client, waiting 5 seconds, then running my server. The client successfully connects in this situation.

I will keep investigating my code to see if I can gather more insights.

0 Likes 0 ·
brendan avatar image brendan Brent Batas (Lisk) commented ·

I'd recommend taking the server build to a machine you haven't been using for testing, installing it there, and running it so that you can test its first time launch on a "clean" system (so that there's nothing potentially cached).

1 Like 1 ·
Brent Batas (Lisk) avatar image Brent Batas (Lisk) brendan commented ·

I went ahead and timed it on a clean system as well as timed it on the PlayFab system and the highest I got was 1.139 seconds.

Pretty weird how it's still failing to connect, given that I tested my client with 5 seconds of delay and it worked fine.

It may have something to do with my code. I'll keep on debugging!

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.