From 7ff52af323d58b8eb09b8896dea8efa719bec82c Mon Sep 17 00:00:00 2001 From: Kevin Lin Date: Sun, 16 Jun 2024 10:41:51 -0700 Subject: [PATCH] Handle non-blocking I/O errors on Unix socket client connect --- shard_connection.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/shard_connection.cpp b/shard_connection.cpp index e873308..f8ddc54 100644 --- a/shard_connection.cpp +++ b/shard_connection.cpp @@ -288,16 +288,27 @@ int shard_connection::connect(struct connect_info* addr) { // call connect m_connection_state = conn_in_progress; - if (bufferevent_socket_connect(m_bev, - m_unix_sockaddr ? (struct sockaddr *) m_unix_sockaddr : addr->ci_addr, - m_unix_sockaddr ? sizeof(struct sockaddr_un) : addr->ci_addrlen) == -1) { + while (true) { + if (bufferevent_socket_connect(m_bev, + m_unix_sockaddr ? (struct sockaddr *) m_unix_sockaddr : addr->ci_addr, + m_unix_sockaddr ? sizeof(struct sockaddr_un) : addr->ci_addrlen) == 0) { + return 0; + } + + if (errno == EINPROGRESS) { + return 0; + } + + if (errno == EAGAIN || errno == EWOULDBLOCK) { + // resource temporarily unavailable; try again + continue; + } + disconnect(); benchmark_error_log("connect failed, error = %s\n", strerror(errno)); return -1; } - - return 0; } void shard_connection::disconnect() {