Sign In Register

How can we help you today?

Start a new topic
Answered

error: timeout when executing cloud code script

Hello.

I'm implementing my turn-based game server with Cloud Code scripts. Each challenge is played by 2 players. And there's only 1 event: TakeTurn. It receives all moves made by player during his turn. 


When 1st player sends TakeTurn event it receives all the moves that player has made during his turn and just saves them to challenge scriptData. When 2nd player sends TakeTurn event results for player1 and player2 moves are calculated and updated game state is stored in challenge scriptData.


The problem is that when 2nd player sends TakeTurn it returns "error": "timeout".

It's waiting for response for about 5 seconds and then returns the error.

I've already optimized move results calculation as much as I could: used associative arrays instead of searches in array, don't use methods like Array.prototype.filter(), try to minimize number of cycles. What else can I do to resolve the problem? I cannot compute player1 moves after player1 turn since player2 moves can prevent player1 ones if successful. Also deciding whether the moves are successful is random and random value generation should be done on backend since we shouldn't trust clients.


It's a turn-based game, so it may be fine for player to wait more than 5 seconds. Is it possible to extend the timeout to longer time period? Or could you suggest any other solutions?

I'm using Unity 5.3.4f1 and latest GameSparks SDK: 5.3.5.171. But I had same problem with older GameSparks SDK versions too.


Thanks.


Best Answer

Hi Anatoli, this is typically due to an overly intensive instruction count in the events cloud code/combined with the use of modules. Other common instances of this occurring is the sending of large amounts of data over the wire to the client. I'll investigate your game and see if I can solve the problem. What is the name of the event, and is it ok to create a test user so as to debug the event in the test harness? 

Best Regards, Patrick.  


Hi Anatolii


The unexpected error is that the event has hit the time limit.

Debugging time depends on the calculations being made. 

Have you tried splitting the event in two as you said before?

We do not extend the time limit above 30 seconds.


Regards

Katie

The error occurs in much less than 30 seconds after the request is sent. Error response is sent in about 3 - 8 seconds. Are you sure that timeout is 30 seconds and not 3?

Hi Anatolii


The timeout is 30 seconds.

Have you tried to break down the process into separate events yet?


Regards

Katie

Answer

Hi Anatoli, this is typically due to an overly intensive instruction count in the events cloud code/combined with the use of modules. Other common instances of this occurring is the sending of large amounts of data over the wire to the client. I'll investigate your game and see if I can solve the problem. What is the name of the event, and is it ok to create a test user so as to debug the event in the test harness? 

Best Regards, Patrick.  

Hello Patrick. Thanks for your response. The event name is TakeTurn. It's OK to create test user. I'm looking forward to your advice.

Patrick, you've written that this problem happens when there are a lot of instructions or a lot of modules in the script.
I tried to merge all modules with classes (models) that I had to single module called Models. However it didn't resolve the problem.


There are pretty many instructions in my script, but the game logic is quite complex and many instructions are needed to check whether client input is correct and to update game state.


One more thing that adds a lot of instructions is that I use model classes with serialize/deserialize methods. Serialization/deserialization adds instructions count, of course, but it allows to bind entities by reference instead of linking by id and performing a loop every time I need to find an object by id. Maybe there's a better way to handle this.


And last thing is that I tested the scripts both in Test Harness and client and I never got "error: timeout" in Test Harness. I got it only when sending events from my game client.

Thanks for your help. I'm looking forward to your response.

Regarding TakeTurn event, I think I can split it into 2:
1. SubmitMoves that will receive moves from client, check if they are valid and save them to challenge scriptData.
2. TakeTurn that will compute move results and switch current player.
Maybe it will resolve the problem. However in total 2 events will last much longer since serialization/deserialization will be made 2 times, once for each event. So for player experience it's better to make timeout on GameSparks longer (if possible) than break one event to multiple.


Also when I tried to publish my current cloud code and try it on Live configuration I got timeout error in ChallengeCreatedResponse as well, even though it has much less code. So maybe even splitting my event to 2 won't help.



Have you checked my scripts? Do you have any ideas?
I'm looking forward to your response.

Thanks.

What's the exact limit on number of instructions allowed in Cloud Code?
Where is it documented? I found only limit for storage in the docs.
Is it really a limit or a bug in GameSparks?
Can anyone answer me at least something? I've already wasted 2 days of development time due to this error, which is costly.


Thanks.

Hi Anatolii


I am looking at your scripts.

Our timeout on scripts is 30 seconds. 

Is there any way to break up the logic done in Take turn into different events?

Can the serialize/deserialize methods have they own event?

Putting everything into one event/call isn't recommend. 



Regards

Katie



Hi Katie.

I tested it in Test Harness. And I have an error in both client and Test Harness. When I run it in Test Harness without debug the error is:

{
 "@class": ".GameSparksErrorResponse",
 "error": {
  "message": "Long running script, terminating after 30000 millis for TakeTurn"
 },
 "message": "Long running script, terminating after 30000 millis for TakeTurn"
}


However when I try to debug it I cannot reach the end of script. Debug fails on the line 186 of TakeTurn module. The error is:

{
 "@class": ".GameSparksErrorResponse",
 "error": {
  "errorInstanceId": "a2794f75-8ca8-4910-9e2e-1f940666d23b",
  "message": "An unexpected error has occurred"
 },
 "message": "An unexpected error has occurred"
}


Thanks.


Apart of having a debugger crash in my script I have another problem. It's that debugging process is really slow. I need to wait for about 30 seconds to get to the next line of code, even if operation is simple like value assignment.

What does it depend on? Debugging process is much faster in simpler scripts. But is there a way to make it faster in my script?
By the way I've noticed that it has become slower after I started using Object.defineProperties method to define non-writable properties. Could that be the reason?

Thanks.

Regarding separate events for serialize/deserialize - that's impossible.

In each event I need to deserialize data first. That means I need to read it from challenge.scriptData and create appropriate objects that will be used in my script logic. After script logic is finished I need to save my updated objects back to challenge.scriptData. So I need to serialize them: save them with the data structure I'm using in scriptData.

Login to post a comment