Search code examples
c#lualuainterface

LuaInterface random exceptions


I'm using LuaInterface for scripting in a game I'm making.

The scripting works quite nice, but somehow I'm getting a lot of random exceptions.

I've narrowed the problem down to where I call lua functions from C# code, which are invoked from another thread, handling networking with the gameserver.

I think what is happening, is that the lua code is being run, and lua functions are called from C# from another thread, which causes the random errors.

How I'm calling the functions from C#:

function.Call(message);

function is of type LuaFunction and message is a custom object.

Errors I'm getting:

  • A first chance exception of type 'LuaInterface.LuaScriptException' occurred in LuaInterface.dll
  • Error running lua: function
  • A first chance exception of type 'LuaInterface.LuaScriptException' occurred in LuaInterface.dll
  • Unable to open script: mainmenu/console

Could it have to do with running lua functions from threads other than the thread the LuaVM was created on?


Solution

  • Lua is not thread safe. You either need to have a Lua state per thread and call the thread specific state, or place locks around any Lua calls.