Question

I have a program that performs very fast-paced calls to a Lua script using lua_pcall. It seems if the program calls the lua script too fast, things will foul up and cause access violations in the most random of places.

I've tried mutexes and even enabled SEH exceptions with try/catch to no avail. Error functions are in place and I'm checking all of the approprate return codes; the problem is an actual access violation deep within the pcall, not a safely handled Lua error.

A lot of the time the break occurs in luaV_execute, but sometimes it's in other random places. I've checked to make sure all parameters pushed to the stack are valid.

Is there a way to force Lua to complete a call before returning, or some way to ensure the call stack doesn't get corrupted?

Was it helpful?

Solution

Although the Lua system as a whole is fully re-entrant, individual lua_State instances are not in themselves thread safe.

If you're accessing a lua_State from multiple threads, you should use a mutex or other locking mechanism to ensure that only one thread at a time can manipulate that state. Simultaneous accesses could easily result in the sort of corruption you're seeing.

If you're working with multiple lua_State instances, each state can have its own access lock; you don't need a single global lock for the whole Lua runtime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top