|
I have recently embedded Lua in my MUD (Multi-User Dungeon) for use in writing new game commands, and scripts for NPCs. Everything has worked excellent until I moved from a single global state to coroutines using lua_newthread(), lua_resume(), and lua_status() on the C API, and implemented my own sleep function. The sleep function is implemented in Lua, and hence yields from Lua, but uses a Lua binding to the Linux gettimeofday() function to allow for sleep times in the milliseconds. I've made a very simple test script which I can execute as a command from within the MUD. The script appears to execute perfectly, the sleep function works as desired, and there are no apparent problems. I am able to execute the script a couple of times per second without incident. However, if I spam this command several times in a single second, some strange things happen. For one thing, the value returned by lua_status() after the script has executed, and is yielding in my sleep function, is sometimes very strange. It seems to return a number between 1 and 128 randomly. On scripts that do this, they seem to become invalid and will not resume any further, even though they haven't finished executing their chunks. And in the worst case, when I spam my test command, I get a crash as follows: Here is core file of the MUD in gdb: #0 0x000000001d2bf8b0 in ?? () The lua_updater class ensures that the status of the coroutine is LUA_YIELD before calling resume. Also, lua_updater::update() is called approximately every 250ms. So, it's possible for me to spawn several coroutines before the first call the the update function. Scripts are executed immediately upon spawn, and yielded coroutines are then updated every so often. This bug occurs for me on both 5.1.4 and 5.2.1, I am on a 64-bit system using the POSIX build option. Does anyone have any clue as to what is happening here? |