From cbe4ae63aecee40674509a2bf23bc5db067e7f3d Mon Sep 17 00:00:00 2001 From: Mark Nunberg Date: Mon, 5 Mar 2018 12:17:49 +0200 Subject: [PATCH] Handle connection errors better in blocking mode as well --- async.c | 2 +- net.c | 17 +++++++++++++---- net.h | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/async.c b/async.c index 7309344..0cecd30 100644 --- a/async.c +++ b/async.c @@ -508,7 +508,7 @@ void redisProcessCallbacks(redisAsyncContext *ac) { static int __redisAsyncHandleConnect(redisAsyncContext *ac) { int completed = 0; redisContext *c = &(ac->c); - if (redisFinishAsyncConnect(c, &completed) == REDIS_ERR) { + if (redisCheckConnectDone(c, &completed) == REDIS_ERR) { /* Error! */ redisCheckSocketError(c); if (ac->onConnect) ac->onConnect(ac, REDIS_ERR); diff --git a/net.c b/net.c index 7d5588e..a4b3abc 100644 --- a/net.c +++ b/net.c @@ -221,8 +221,10 @@ static int redisContextWaitReady(redisContext *c, long msec) { return REDIS_ERR; } - if (redisCheckSocketError(c) != REDIS_OK) + if (redisCheckConnectDone(c, &res) != REDIS_OK || res == 0) { + redisCheckSocketError(c); return REDIS_ERR; + } return REDIS_OK; } @@ -232,7 +234,7 @@ static int redisContextWaitReady(redisContext *c, long msec) { return REDIS_ERR; } -int redisFinishAsyncConnect(redisContext *c, int *completed) { +int redisCheckConnectDone(redisContext *c, int *completed) { int rc = connect(c->fd, (const struct sockaddr *)c->saddr, c->addrlen); if (rc == 0) { *completed = 1; @@ -410,8 +412,14 @@ addrretry: if (errno == EHOSTUNREACH) { redisContextCloseFd(c); continue; - } else if (errno == EINPROGRESS && !blocking) { - /* This is ok. */ + } else if (errno == EINPROGRESS) { + if (blocking) { + goto wait_for_ready; + } + /* This is ok. + * Note that even when it's in blocking mode, we unset blocking + * for `connect()` + */ } else if (errno == EADDRNOTAVAIL && reuseaddr) { if (++reuses >= REDIS_CONNECT_RETRIES) { goto error; @@ -420,6 +428,7 @@ addrretry: goto addrretry; } } else { + wait_for_ready: if (redisContextWaitReady(c,timeout_msec) != REDIS_OK) goto error; } diff --git a/net.h b/net.h index c35facc..a11594e 100644 --- a/net.h +++ b/net.h @@ -45,6 +45,6 @@ int redisContextConnectBindTcp(redisContext *c, const char *addr, int port, const char *source_addr); int redisContextConnectUnix(redisContext *c, const char *path, const struct timeval *timeout); int redisKeepAlive(redisContext *c, int interval); -int redisFinishAsyncConnect(redisContext *c, int *completed); +int redisCheckConnectDone(redisContext *c, int *completed); #endif