[OpenWrt-Devel] libubox-lua: uloop.process gc bug

陈斌 ewolfok at 126.com
Thu Jul 16 05:32:10 EDT 2015


HI, all

this is a patch  to fix libubox-lua's  uloop.process gc bug:    before free lua userdata  proc, we may need  to call uloop_process_delete first.


Best regards

Signed-off-by:Chen Bin <ewolfok at 126.com>
---
--- a/lua/uloop.c
+++ b/lua/uloop.c
@@ -77,9 +77,9 @@ static int ul_timer_set(lua_State *L)
 static int ul_timer_free(lua_State *L)
 {
 struct lua_uloop_timeout *tout = lua_touserdata(L, 1);
-
+
 uloop_timeout_cancel(&tout->t);
-
+
 /* obj.__index.__gc = nil , make sure executing only once*/
 lua_getfield(L, -1, "__index");
 lua_pushstring(L, "__gc");
@@ -181,7 +181,7 @@ static int get_sock_fd(lua_State* L, int
 static int ul_ufd_delete(lua_State *L)
 {
 struct lua_uloop_fd *ufd = lua_touserdata(L, 1);
-
+
 uloop_fd_delete(&ufd->fd);


 /* obj.__index.__gc = nil , make sure executing only once*/
@@ -263,6 +263,33 @@ static int ul_ufd_add(lua_State *L)
 return 1;
 }


+static int ul_process_delete(lua_State *L)
+{
+struct lua_uloop_process *proc = lua_touserdata(L, 1);
+
+/* obj.__index.__gc = nil , make sure executing only once*/
+lua_getfield(L, -1, "__index");
+lua_pushstring(L, "__gc");
+lua_pushnil(L);
+lua_settable(L, -3);
+
+if (proc->r != LUA_NOREF) {
+uloop_process_delete(&proc->p);
+
+lua_getglobal(state, "__uloop_cb");
+luaL_unref(state, -1, proc->r);
+lua_remove(state, -1);
+}
+
+return 1;
+}
+
+static const luaL_Reg uproc_m[] = {
+{ "delete", ul_process_delete },
+{ NULL, NULL }
+};
+
+
 static void ul_process_cb(struct uloop_process *p, int ret)
 {
 struct lua_uloop_process *proc = container_of(p, struct lua_uloop_process, p);
@@ -271,6 +298,7 @@ static void ul_process_cb(struct uloop_p
 lua_rawgeti(state, -1, proc->r);


 luaL_unref(state, -2, proc->r);
+proc->r = LUA_NOREF;
 lua_remove(state, -2);
 lua_pushinteger(state, ret >> 8);
 lua_call(state, 1, 0);
@@ -331,6 +359,18 @@ static int ul_process(lua_State *L)
 ref = luaL_ref(L, -2);


 proc = lua_newuserdata(L, sizeof(*proc));
+lua_createtable(L, 0, 2);
+lua_pushvalue(L, -1);
+lua_setfield(L, -2, "__index");
+lua_pushcfunction(L, ul_process_delete);
+lua_setfield(L, -2, "__gc");
+lua_pushvalue(L, -1);
+lua_setmetatable(L, -3);
+lua_pushvalue(L, -2);
+luaI_openlib(L, NULL, uproc_m, 1);
+lua_pushvalue(L, -2);
+
+
 memset(proc, 0, sizeof(*proc));


 proc->r = ref;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.infradead.org/pipermail/openwrt-devel/attachments/20150716/e6670f23/attachment.htm>
-------------- next part --------------
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list