[Lua] Registry Overflow

Hello,

While adding computer opponents to my current project, I’ve bumped up against what I can only assume is a technical limitation within the Lua runtime module: Registry Overflow.

After doing some digging I did not have much luck finding this exact issue however I think I have a decent idea as to what’s happening.

Lua uses a register table for the creation of local variables, for which there is a hard-coded limit of 256 local variables per function (as the variables are indexed in a byte). When this limit is exceeded, an error occurs, though Nakama may be wrapping this Lua error as a Registry Overflow. The location of this error is within a recursive function that evaluates the possible moves for the computer and is likely because I am currently (wastefully) deep copying selective parts of the gamestate table for each iteration of move calculation. This made the evaluations much easier to leverage the rest of the match code as they all mostly interact with the state table, but this limit is being hit on turn 2.

I’m currently working on a change to this process that will, instead of copying these tables, set an evaluation flag that prevents game updates from being sent and store the current state represented as a string so that after each branch of evaluation I can simply roll back the gamestate. While I think this will make this work much longer, I fear this problem will resurface rather quickly as the number of possible moves will increase as the board gets more complex.

The root of my question is this:

  1. Is this Registry Overflow error what I suspect it to be?
  2. Is there any way to work around it, increase it, or otherwise avoid this issue?
  3. If there is not, would this more likely be solved by porting the match handling logic to another runtime module (i.e Go / JS)?

Below is some more technical information regarding the setup and the error in question. If more extensive logs are needed I can provide them.

Technical Info

Client: Godot 3.5
Server: Nakama 3.16.0 (Docker), Lua Runtime

Error Snippet

dev-nakama-1       | {"level":"warn","ts":"2023-11-07T05:35:53.312Z","caller":"server/match_handler.go:292","msg":"Stopping match after error from match_loop execution","mid":"a5871f40-beb4-4cad-a8d0-6d55831a61c6","tick":249,"error":"data/modules/match_handler.lua:203: registry overflow\nstack traceback:\n\tdata/modules/match_handler.lua:203: in function 'deepcopy'

Any insight is appreciated.

Hi, @khamarr3524.

Thank you for the detailed description of your issue.

  1. No, registry overflow means that the Lua VM used within Nakama has reached its max size either because of a big callstack (recursion) or code complexity.
  2. Yes, you may increase the size of the registry: Heroic Labs Documentation | Configuration
  3. If you reach a registry size that is not feasible to your project (depends on what hardware you are targeting), porting to Go might help, indeed. But I would recommend refactoring as much as possible before porting it.

Hope this helps. And glad to see you are back on the forum!

Regards,
Caetano

@khamarr3524 I once had similar issue with not the same topic, but let says calculating path finding and i did hit registry overflow, which in stock lua wasn’t hit but in gopher lua which nakama uses did hit.
I managed on some places to avoid these registry overflow by encapsulating other parts of the code within function in its separate
do
end
block :slight_smile:
Could might help you too, without increasing registry sizes and additional refactoring, but in the end ye :confused: you must design code to be as light as possible…