From 70f5e98110b2c63755f283712eebbd075787081b Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Tue, 5 Nov 2024 03:15:04 +1400
Subject: [PATCH 146/156] bugfix for sem_wait call when errno is EINTR

Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
 src/cmd/isula/base/start.c                               | 4 +++-
 src/cmd/isula/client_console.c                           | 4 +++-
 src/cmd/isula/stream/attach.c                            | 8 ++++++--
 src/cmd/isula/stream/exec.c                              | 4 +++-
 src/cmd/isulad-shim/process.c                            | 4 +++-
 src/cmd/isulad/main.c                                    | 8 ++++++--
 src/daemon/entry/connect/grpc/grpc_containers_service.cc | 8 ++++++--
 src/daemon/entry/cri/streams/attach_serve.cc             | 4 +++-
 src/daemon/entry/cri/streams/websocket/ws_server.cc      | 5 ++++-
 src/daemon/modules/events/collector.c                    | 8 ++++++--
 src/daemon/modules/service/io_handler.c                  | 4 +++-
 11 files changed, 46 insertions(+), 15 deletions(-)

diff --git a/src/cmd/isula/base/start.c b/src/cmd/isula/base/start.c
index 6a066638..3f8c13ee 100644
--- a/src/cmd/isula/base/start.c
+++ b/src/cmd/isula/base/start.c
@@ -193,7 +193,9 @@ out:
 void client_wait_fifo_exit(const struct client_arguments *args)
 {
     if (args->custom_conf.attach_stdin || args->custom_conf.attach_stdout || args->custom_conf.attach_stderr) {
-        sem_wait(&g_console_waitexit_sem);
+        while(sem_wait(&g_console_waitexit_sem) == -1 && errno == EINTR) {
+            continue;
+        }
     }
 }
 
diff --git a/src/cmd/isula/client_console.c b/src/cmd/isula/client_console.c
index 555f59b6..fb6f6c05 100644
--- a/src/cmd/isula/client_console.c
+++ b/src/cmd/isula/client_console.c
@@ -259,7 +259,9 @@ int start_client_console_thread(struct command_fifo_config *console_fifos, bool
         return -1;
     }
 
-    sem_wait(console_fifos->wait_open);
+    while(sem_wait(console_fifos->wait_open) == -1 && errno == EINTR) {
+        continue;
+    }
 
     return 0;
 }
diff --git a/src/cmd/isula/stream/attach.c b/src/cmd/isula/stream/attach.c
index b61c9350..bc3eb141 100644
--- a/src/cmd/isula/stream/attach.c
+++ b/src/cmd/isula/stream/attach.c
@@ -285,7 +285,9 @@ static int container_wait_thread(struct client_arguments *args, uint32_t *exit_c
         (void)sem_destroy(&sem_started);
         return -1;
     }
-    (void)sem_wait(&sem_started);
+    while(sem_wait(&sem_started) == -1 && errno == EINTR) {
+        continue;
+    }
     (void)sem_destroy(&sem_started);
     return 0;
 }
@@ -366,7 +368,9 @@ static int client_attach(struct client_arguments *args, uint32_t *exit_code)
     }
 
 #ifndef GRPC_CONNECTOR
-    sem_wait(&g_attach_waitexit_sem);
+    while(sem_wait(&g_attach_waitexit_sem) == -1 && errno == EINTR) {
+        continue;
+    }
 #endif
 
     if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
diff --git a/src/cmd/isula/stream/exec.c b/src/cmd/isula/stream/exec.c
index cacb0278..6eab4d4f 100644
--- a/src/cmd/isula/stream/exec.c
+++ b/src/cmd/isula/stream/exec.c
@@ -380,7 +380,9 @@ static int local_cmd_exec(struct client_arguments *args, uint32_t *exit_code)
     ret = client_exec(args, command_fifos, exit_code);
     if (ret == 0 &&
         (args->custom_conf.attach_stdin || args->custom_conf.attach_stdout || args->custom_conf.attach_stderr)) {
-        sem_wait(&g_command_waitexit_sem);
+        while(sem_wait(&g_command_waitexit_sem) == -1 && errno == EINTR) {
+            continue;
+        }
     }
 out:
     delete_command_fifo(command_fifos);
diff --git a/src/cmd/isulad-shim/process.c b/src/cmd/isulad-shim/process.c
index 18fae03f..11903a5c 100644
--- a/src/cmd/isulad-shim/process.c
+++ b/src/cmd/isulad-shim/process.c
@@ -1237,7 +1237,9 @@ int process_io_start(process_t *p, pthread_t *tid_epoll)
     if (ret != SHIM_OK) {
         return SHIM_SYS_ERR(errno);
     }
-    (void)sem_wait(&p->sem_mainloop);
+    while(sem_wait(&p->sem_mainloop) == -1 && errno == EINTR) {
+        continue;
+    }
     (void)sem_destroy(&p->sem_mainloop);
 
     return SHIM_OK;
diff --git a/src/cmd/isulad/main.c b/src/cmd/isulad/main.c
index 0228caa8..7c6148fd 100644
--- a/src/cmd/isulad/main.c
+++ b/src/cmd/isulad/main.c
@@ -1648,7 +1648,9 @@ static void *do_shutdown_handler(void *arg)
 
     prctl(PR_SET_NAME, "Shutdown");
 
-    sem_wait(&g_daemon_shutdown_sem);
+    while(sem_wait(&g_daemon_shutdown_sem) == -1 && errno == EINTR) {
+        continue;
+    }
 
     daemon_shutdown();
 
@@ -1868,7 +1870,9 @@ int main(int argc, char **argv)
 
     server_common_start();
 
-    sem_wait(&g_daemon_wait_shutdown_sem);
+    while(sem_wait(&g_daemon_wait_shutdown_sem) == -1 && errno == EINTR) {
+        continue;
+    }
 
     DAEMON_CLEAR_ERRMSG();
     return 0;
diff --git a/src/daemon/entry/connect/grpc/grpc_containers_service.cc b/src/daemon/entry/connect/grpc/grpc_containers_service.cc
index c5e7c275..0a46b36c 100644
--- a/src/daemon/entry/connect/grpc/grpc_containers_service.cc
+++ b/src/daemon/entry/connect/grpc/grpc_containers_service.cc
@@ -306,7 +306,9 @@ Status ContainerServiceImpl::RemoteStart(ServerContext *context,
     // close pipe 1 first, make sure io copy thread exit
     close(read_pipe_fd[1]);
     if (container_req->attach_stderr && ret == 0) {
-        (void)sem_wait(&sem);
+        while(sem_wait(&sem) == -1 && errno == EINTR) {
+            continue;
+        }
     }
     (void)sem_destroy(&sem);
     close(read_pipe_fd[0]);
@@ -656,7 +658,9 @@ Status ContainerServiceImpl::Attach(ServerContext *context, ServerReaderWriter<A
     close(pipefd[1]);
     // Waiting sem, make sure the sem is posted always in attach callback.
     if (container_req->attach_stderr && ret == 0) {
-        (void)sem_wait(&sem_stderr);
+        while(sem_wait(&sem_stderr) == -1 && errno == EINTR) {
+            continue;
+        }
     }
     (void)sem_destroy(&sem_stderr);
     close(pipefd[0]);
diff --git a/src/daemon/entry/cri/streams/attach_serve.cc b/src/daemon/entry/cri/streams/attach_serve.cc
index 3d59e539..9c0e56c4 100644
--- a/src/daemon/entry/cri/streams/attach_serve.cc
+++ b/src/daemon/entry/cri/streams/attach_serve.cc
@@ -145,7 +145,9 @@ int AttachServe::ExecuteStreamCommand(SessionData *lwsCtx, void *request)
         WsWriteStdoutToClient(lwsCtx, message.c_str(), message.length());
     } else {
         // wait io copy thread complete
-        (void)sem_wait(&attachSem);
+        while(sem_wait(&attachSem) == -1 && errno == EINTR) {
+            continue;
+        }
     }
 
     (void)sem_destroy(&attachSem);
diff --git a/src/daemon/entry/cri/streams/websocket/ws_server.cc b/src/daemon/entry/cri/streams/websocket/ws_server.cc
index a8d89b36..7e3225b2 100644
--- a/src/daemon/entry/cri/streams/websocket/ws_server.cc
+++ b/src/daemon/entry/cri/streams/websocket/ws_server.cc
@@ -193,7 +193,10 @@ void WebsocketServer::CloseWsSession(int socketID)
             close(session->pipes.at(1));
             session->pipes.at(1) = -1;
         }
-        (void)sem_wait(session->syncCloseSem);
+
+        while(sem_wait(session->syncCloseSem) == -1 && errno == EINTR) {
+            continue;
+        }
         (void)sem_destroy(session->syncCloseSem);
         delete session->syncCloseSem;
         session->syncCloseSem = nullptr;
diff --git a/src/daemon/modules/events/collector.c b/src/daemon/modules/events/collector.c
index af688742..eb79bf81 100644
--- a/src/daemon/modules/events/collector.c
+++ b/src/daemon/modules/events/collector.c
@@ -932,7 +932,9 @@ int add_monitor_client(char *name, const types_timestamp_t *since, const types_t
         goto sem_free;
     }
 
-    sem_wait(&context_info->context_sem);
+    while(sem_wait(&context_info->context_sem) == -1 && errno == EINTR) {
+        continue;
+    }
 
 sem_free:
     sem_destroy(&context_info->context_sem);
@@ -1002,7 +1004,9 @@ static int start_monitored()
         goto out;
     }
 
-    sem_wait(msync.monitord_sem);
+    while(sem_wait(msync.monitord_sem) == -1 && errno == EINTR) {
+        continue;
+    }
     sem_destroy(msync.monitord_sem);
     if (monitored_exitcode) {
         isulad_set_error_message("Monitored start failed");
diff --git a/src/daemon/modules/service/io_handler.c b/src/daemon/modules/service/io_handler.c
index 474fa650..f3b47737 100644
--- a/src/daemon/modules/service/io_handler.c
+++ b/src/daemon/modules/service/io_handler.c
@@ -485,7 +485,9 @@ static int start_io_copy_thread(int sync_fd, bool detach, struct io_copy_arg *co
         return -1;
     }
 
-    sem_wait(&thread_arg.wait_sem);
+    while(sem_wait(&thread_arg.wait_sem) == -1 && errno == EINTR) {
+        continue;
+    }
     sem_destroy(&thread_arg.wait_sem);
     return 0;
 }
-- 
2.34.1

