补充修复lwip 网络恢复死机bug 3007/1 3007/head 3019/target/1
author孙春阳 <snickersun@163.com>
Wed, 31 Dec 2025 04:51:36 +0000 (12:51 +0800)
committer孙春阳 <snickersun@163.com>
Wed, 31 Dec 2025 04:51:36 +0000 (12:51 +0800)
code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/api/api_msg.c

index 2a83cfa..8bef06c 100644 (file)
@@ -1945,20 +1945,26 @@ err_mem:
     /* everything was written: set back connection state
        and back to application task */
     /* Re-check current_msg validity before accessing it (err_tcp may have cleared it) */
-    if (conn->current_msg != NULL) {
-      /* Normal cleanup: current_msg is still valid, so we need to clean up */
-      sys_sem_t *op_completed_sem = LWIP_API_MSG_SEM(conn->current_msg);
-      conn->current_msg->err = err;
-      /* Reset state BEFORE clearing current_msg to avoid race condition:
-       * If poll_tcp/sent_tcp is called between these two assignments,
-       * we want state to be NETCONN_NONE (not NETCONN_WRITE) when current_msg is NULL */
-      conn->state = NETCONN_NONE;
-      conn->current_msg = NULL;
+    if (conn->current_msg != NULL && conn->state == NETCONN_WRITE) {
+      /* Normal cleanup: current_msg is still valid, so we need to clean up
+       * Double-check to ensure err_tcp hasn't cleared it between check and use */
+      struct api_msg *msg_to_cleanup = conn->current_msg;
+      if (conn->current_msg == msg_to_cleanup && conn->state == NETCONN_WRITE) {
+        sys_sem_t *op_completed_sem = LWIP_API_MSG_SEM(msg_to_cleanup);
+        msg_to_cleanup->err = err;
+        /* Reset state BEFORE clearing current_msg to avoid race condition:
+         * If poll_tcp/sent_tcp is called between these two assignments,
+         * we want state to be NETCONN_NONE (not NETCONN_WRITE) when current_msg is NULL */
+        conn->state = NETCONN_NONE;
+        conn->current_msg = NULL;
 #if LWIP_TCPIP_CORE_LOCKING
-      if (delayed)
+        if (delayed)
 #endif
-      {
-        sys_sem_signal(op_completed_sem);
+        {
+          sys_sem_signal(op_completed_sem);
+        }
+      } else {
+        /* current_msg was changed or state changed by err_tcp between checks, skip cleanup */
       }
     } else {
       /* current_msg was already cleared by err_tcp.