Skip to content

ggml : fix race-condition in ggml-rpc #13600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ggml/src/ggml-rpc/ggml-rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct socket_t {
close(this->fd);
#endif
}
std::mutex send_rpc_cmd_mutex;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes it sense to use this mutex for both sending and receiving data, so better rename it to sock_mutex

Copy link
Author

@gkpln3 gkpln3 May 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense for multiple threads to read from the same socket? Sounds like it would introduce new issues

Copy link
Collaborator

@rgerganov rgerganov May 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My assumption was that if 3rd party clients need multi-threaded send, they would also need multi-threaded receive. If this is not the case, you can leave it as is, just rename it to send_mutex

};

// all RPC structures must be packed
Expand Down Expand Up @@ -386,6 +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<socket_t> & sock, enum rpc_cmd cmd, const void * input, size_t input_size) {
std::lock_guard<std::mutex> lock(sock->send_rpc_cmd_mutex);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should also acquire the lock in the overloaded send_rpc_cmd function below to prevent multiple threads reading from the socket at the same time;

you may also need to use std::recursive_mutex for this to work

uint8_t cmd_byte = cmd;
if (!send_data(sock->fd, &cmd_byte, sizeof(cmd_byte))) {
return false;
Expand Down