From 34853c598db82e4c43e9f45d63de829ac30fa370 Mon Sep 17 00:00:00 2001 From: ckolivas Date: Mon, 19 Jan 2015 15:58:30 +1100 Subject: [PATCH] Back off and retry on failed realloc in read_socket_line instead of failing. --- src/ckpool.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ckpool.c b/src/ckpool.c index c34b6055..5f775fc5 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -404,6 +404,8 @@ int read_socket_line(connsock_t *cs, int timeout) while (42) { char readbuf[PAGESIZE] = {}; + int backoff = 1; + char *newbuf; ret = wait_read_select(fd, eom ? 0 : timeout); if (eom && !ret) @@ -422,7 +424,16 @@ int read_socket_line(connsock_t *cs, int timeout) goto out; } buflen = cs->bufofs + ret + 1; - cs->buf = realloc(cs->buf, buflen); + while (42) { + newbuf = realloc(cs->buf, buflen); + if (likely(newbuf)) + break; + if (backoff == 1) + fprintf(stderr, "Failed to realloc %d in read_socket_line, retrying\n", (int)buflen); + cksleep_ms(backoff); + backoff <<= 1; + } + cs->buf = newbuf; if (unlikely(!cs->buf)) quit(1, "Failed to alloc buf of %d bytes in read_socket_line", (int)buflen); memcpy(cs->buf + cs->bufofs, readbuf, ret);