lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

I'm using the environment index for my libevent binding for Lua and
have found it appears to be behaving rather odd...

<code inside luaevent_callback>
	lua_rawgeti(L, LUA_ENVIRONINDEX, arg->callbackRef);
	lua_pushinteger(L, event);
	ret = lua_resume(L, 1);
	if(ret == LUA_YIELD) {
	} else { /* Done... */
		luaL_unref(L, LUA_ENVIRONINDEX, arg->callbackRef);

The crash occurs at the luaL_unref location consistently.  If I
connect/disconnect w/ a single client, it always occurs.  If I connect
multiple times and then begin disconnecting, some of the
disconnections don't cause a segfault, but when count goes to zero, a
seg always occurs....

I've inserted some code to make sure that the memory location holding
callbackRef is still valid and it stays the same...

If I don't use the environment index, then no segfault happens...

I setup the ENV index at the beginning of the registration of the
library and the function that is responsible for the callback is one
of the functions registered.  Could it be that the ENV table is tied
to the current coroutine?  Even in that case it doesn't explain why
lua_rawgeti works and luaL_unref doesnt.

Note: I manually insert a single value at the index 1 in the ENV...
looking at luaL_ref and luaL_unref, this doesnt seem to be an issue
since they use the length if no free values, otherwise from a free
Any ideas?

*** More detailed description ***
Basically this binding is being developed to be a drop-in replacement
for copas and interop w/ luasocket.

Add listening server
  Create an event object at the beginning on the server socket
  Yield the socket and EV_READ event
  On event, accept a socket
    configure client to be non-blocking
    create coroutine for client
      if client needs to yield a waiting event, configure an event
object for it and store it in a weak table associated w/ the socket
Hopefully this will help.  Note that I anticipate making the project
Thomas Harning Jr.