From e1e8e911e671e9b28f74ef44f321770b9cdcc689 Mon Sep 17 00:00:00 2001 From: Guy Kaplan Date: Sat, 17 May 2025 12:00:41 +0300 Subject: [PATCH 1/2] ggml : Fixed race-condition that leads to crashes when using ggml-rpc --- ggml/src/ggml-rpc/ggml-rpc.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ggml/src/ggml-rpc/ggml-rpc.cpp b/ggml/src/ggml-rpc/ggml-rpc.cpp index 4f0abb5a60f48..618cc597805d9 100644 --- a/ggml/src/ggml-rpc/ggml-rpc.cpp +++ b/ggml/src/ggml-rpc/ggml-rpc.cpp @@ -386,6 +386,8 @@ static bool parse_endpoint(const std::string & endpoint, std::string & host, int // RPC request : | rpc_cmd (1 byte) | request_size (8 bytes) | request_data (request_size bytes) | // No response static bool send_rpc_cmd(const std::shared_ptr & sock, enum rpc_cmd cmd, const void * input, size_t input_size) { + static std::mutex send_rpc_cmd_mutex; + std::lock_guard lock(send_rpc_cmd_mutex); uint8_t cmd_byte = cmd; if (!send_data(sock->fd, &cmd_byte, sizeof(cmd_byte))) { return false; From f33b76dffa638117fa44b16d3a8632ed67ca1553 Mon Sep 17 00:00:00 2001 From: Guy Kaplan Date: Sun, 25 May 2025 11:24:58 +0300 Subject: [PATCH 2/2] Refactor RPC command mutex handling to use socket-specific mutex --- ggml/src/ggml-rpc/ggml-rpc.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ggml/src/ggml-rpc/ggml-rpc.cpp b/ggml/src/ggml-rpc/ggml-rpc.cpp index 618cc597805d9..8581bdf6db887 100644 --- a/ggml/src/ggml-rpc/ggml-rpc.cpp +++ b/ggml/src/ggml-rpc/ggml-rpc.cpp @@ -51,6 +51,7 @@ struct socket_t { close(this->fd); #endif } + std::mutex send_rpc_cmd_mutex; }; // all RPC structures must be packed @@ -386,8 +387,7 @@ static bool parse_endpoint(const std::string & endpoint, std::string & host, int // RPC request : | rpc_cmd (1 byte) | request_size (8 bytes) | request_data (request_size bytes) | // No response static bool send_rpc_cmd(const std::shared_ptr & sock, enum rpc_cmd cmd, const void * input, size_t input_size) { - static std::mutex send_rpc_cmd_mutex; - std::lock_guard lock(send_rpc_cmd_mutex); + std::lock_guard lock(sock->send_rpc_cmd_mutex); uint8_t cmd_byte = cmd; if (!send_data(sock->fd, &cmd_byte, sizeof(cmd_byte))) { return false;