From 18db58c919bb9767bbe2f11c7447cfed27312c63 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Thu, 9 Jun 2022 21:16:26 +0800
Subject: [PATCH 15/30] fix isulad-shim coredump when remove ioc fd node

Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
 src/cmd/isulad-shim/process.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/cmd/isulad-shim/process.c b/src/cmd/isulad-shim/process.c
index 21b2523a..6de470f7 100644
--- a/src/cmd/isulad-shim/process.c
+++ b/src/cmd/isulad-shim/process.c
@@ -195,10 +195,6 @@ static void remove_io_dispatch(io_thread_t *io_thd, int from, int to)
     }
     io_copy_t *ioc = io_thd->ioc;
 
-    if (pthread_mutex_lock(&(ioc->mutex))) {
-        return;
-    }
-
     fd_node_t *tmp = NULL;
     do {
         /* remove src fd */
@@ -233,7 +229,6 @@ static void remove_io_dispatch(io_thread_t *io_thd, int from, int to)
         free(tmp);
         tmp = NULL;
     }
-    pthread_mutex_unlock(&(ioc->mutex));
 }
 
 static int get_exec_winsize(const char *buf, struct winsize *wsize)
@@ -297,8 +292,14 @@ static void *do_io_copy(void *data)
             /* End of file. The remote has closed the connection */
             break;
         } else if (ioc->id != EXEC_RESIZE) {
+            if (pthread_mutex_lock(&(ioc->mutex)) != 0) {
+                continue;
+            }
+
             fd_node_t *fn = ioc->fd_to;
-            for (; fn != NULL; fn = fn->next) {
+            fd_node_t *next = fn;
+            for (; fn != NULL; fn = next) {
+                next = fn->next;
                 if (fn->is_log) {
                     shim_write_container_log_file(io_thd->terminal, ioc->id, buf, r_count);
                 } else {
@@ -309,7 +310,12 @@ static void *do_io_copy(void *data)
                     }
                 }
             }
+            pthread_mutex_unlock(&(ioc->mutex));
         } else {
+            if (pthread_mutex_lock(&(ioc->mutex)) != 0) {
+                continue;
+            }
+
             int resize_fd = ioc->fd_to->fd;
             struct winsize wsize = { 0x00 };
             if (get_exec_winsize(buf, &wsize) < 0) {
@@ -318,6 +324,7 @@ static void *do_io_copy(void *data)
             if (ioctl(resize_fd, TIOCSWINSZ, &wsize) < 0) {
                 break;
             }
+            pthread_mutex_unlock(&(ioc->mutex));
         }
 
         /*
-- 
2.32.1 (Apple Git-133)

