Kaynağa Gözat

fix: 在air640w上的socket使用,如果lua端提前close,而后台线程之后才关闭, 有概率报错

Wendal Chen 5 yıl önce
ebeveyn
işleme
c81743c968

+ 2 - 1
luat/include/netclient.h

@@ -64,10 +64,11 @@ typedef struct netclient
 
     // Lua callback function
     int cb_recv;
-    int cb_close;
+    //int cb_close;
     int cb_connect;
     int cb_any;
     int cb_error;
+    int self_ref;
 }netclient_t;
 
 uint32_t netc_next_no(void);

+ 18 - 11
luat/modules/luat_lib_socket.c

@@ -89,6 +89,9 @@ static int luat_lib_netc_msg_handler(lua_State* L, void* ptr) {
             lua_pushstring(L, buff);
             lua_call(L, 1, 0);
         }
+        if (ent->lua_ref) {
+            luaL_unref(L, ent->lua_ref, LUA_REGISTRYINDEX);
+        }
         goto exit;
     }
     LLOGD("netc[%ld] event=%ld lua_ref=%ld", ent->netc_id, ent->event, ent->lua_ref);
@@ -267,6 +270,10 @@ static int netc_connect(lua_State *L) {
     thiz->rx = luat_lib_socket_ent_handler;
     //LLOGI("netc[%ld] host=%s port=%d type=%s", thiz->id, thiz->hostname, thiz->port, thiz->type == NETC_TYPE_TCP ? "TCP" : "UDP");
     re = netclient_start(thiz);
+    if (re == 0) {
+        lua_settop(L, 1);
+        thiz->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);
+    }
     lua_pushinteger(L, re);
     return 1;
 }
@@ -326,9 +333,9 @@ static int netc_gc(lua_State *L) {
     if (netc->cb_recv) {
         luaL_unref(L, LUA_REGISTRYINDEX, netc->cb_recv);
     }
-    if (netc->cb_close) {
-        luaL_unref(L, LUA_REGISTRYINDEX, netc->cb_close);
-    }
+    // if (netc->cb_close) {
+    //     luaL_unref(L, LUA_REGISTRYINDEX, netc->cb_close);
+    // }
     if (netc->cb_connect) {
         luaL_unref(L, LUA_REGISTRYINDEX, netc->cb_connect);
     }
@@ -427,11 +434,11 @@ static int netc_clean(lua_State *L) {
         luaL_unref(L, LUA_REGISTRYINDEX, netc->cb_recv);
         netc->cb_recv = 0;
     }
-    if (netc->cb_close) {
-        //LOG_D("netc[%ld] unref 0x%08X", netc->id, netc->cb_close);
-        luaL_unref(L, LUA_REGISTRYINDEX, netc->cb_close);
-        netc->cb_close = 0;
-    }
+    // if (netc->cb_close) {
+    //     //LOG_D("netc[%ld] unref 0x%08X", netc->id, netc->cb_close);
+    //     luaL_unref(L, LUA_REGISTRYINDEX, netc->cb_close);
+    //     netc->cb_close = 0;
+    // }
     if (netc->cb_connect) {
         //LOG_D("netc[%ld] unref 0x%08X", netc->id, netc->cb_connect);
         luaL_unref(L, LUA_REGISTRYINDEX, netc->cb_connect);
@@ -461,9 +468,9 @@ static int netc_on(lua_State *L) {
             if (strcmp("recv", ent) == 0) {
                 netc->cb_recv = luaL_ref(L, LUA_REGISTRYINDEX);
             }
-            else if (strcmp("close", ent) == 0) {
-                netc->cb_close = luaL_ref(L, LUA_REGISTRYINDEX);
-            }
+            // else if (strcmp("close", ent) == 0) {
+            //     netc->cb_close = luaL_ref(L, LUA_REGISTRYINDEX);
+            // }
             else if (strcmp("connect", ent) == 0) {
                 netc->cb_connect = luaL_ref(L, LUA_REGISTRYINDEX);
             }

+ 1 - 1
luat/rtt/luat_netclient_rtt.c

@@ -407,7 +407,7 @@ static void netclient_thread_entry(void *param)
     //}
 netc_exit:
     thiz->closed = 1;
-    EVENT(thiz->id, thiz->rx, 0, NETC_EVENT_END, 0, RT_NULL);
+    EVENT(thiz->id, thiz->rx, thiz->self_ref, NETC_EVENT_END, 0, RT_NULL);
     LLOGW("netc[%ld] thread end", thiz->id);
 }