From a8eee6f0a3f7bb09bff8a58005e801bde6cfd13c Mon Sep 17 00:00:00 2001 From: ckolivas Date: Thu, 26 Jun 2014 09:21:07 +0300 Subject: [PATCH 1/7] We cannot intercept signal 9 --- src/ckpool.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index a9f234c1..7f45025b 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -700,17 +700,12 @@ static void sighandler(int sig) pthread_cancel(ckp->pth_watchdog); join_pthread(ckp->pth_watchdog); - if (sig != 9) { - __shutdown_children(ckp, SIGTERM); - /* Wait a second, then send SIGKILL */ - sleep(1); - __shutdown_children(ckp, SIGKILL); - pthread_cancel(ckp->pth_listener); - exit(0); - } else { - __shutdown_children(ckp, SIGKILL); - exit(1); - } + __shutdown_children(ckp, SIGTERM); + /* Wait a second, then send SIGKILL */ + sleep(1); + __shutdown_children(ckp, SIGKILL); + pthread_cancel(ckp->pth_listener); + exit(0); } static void json_get_string(char **store, json_t *val, const char *res) From cdd8208269b9ac21bf59be238321473a257257cc Mon Sep 17 00:00:00 2001 From: ckolivas Date: Thu, 26 Jun 2014 09:50:37 +0300 Subject: [PATCH 2/7] accept() calls are automatically restarted with EINTR when there's no timeout --- src/stratifier.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index e3135926..9351ff6b 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -949,11 +949,9 @@ retry: sockd = accept(us->sockd, NULL, NULL); if (sockd < 0) { - if (interrupted()) - goto retry; - LOGERR("Failed to accept on stratifier socket, retrying in 5s"); - sleep(5); - goto retry; + LOGERR("Failed to accept on stratifier socket, exiting"); + ret = 1; + goto out; } dealloc(buf); From 2edb2a074d2ab0bf2388b466a65ee22c33c7ac05 Mon Sep 17 00:00:00 2001 From: ckolivas Date: Thu, 26 Jun 2014 09:51:52 +0300 Subject: [PATCH 3/7] Don't check for EINTR in generator as well --- src/generator.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/generator.c b/src/generator.c index e6853ae0..20a6cab1 100644 --- a/src/generator.c +++ b/src/generator.c @@ -220,9 +220,7 @@ retry: sockd = accept(us->sockd, NULL, NULL); if (sockd < 0) { - if (interrupted()) - goto retry; - LOGERR("Failed to accept on generator socket"); + LOGEMERG("Failed to accept on generator socket"); ret = 1; goto out; } @@ -1227,9 +1225,7 @@ reconnect: retry: sockd = accept(us->sockd, NULL, NULL); if (sockd < 0) { - if (interrupted()) - goto retry; - LOGERR("Failed to accept on proxy socket"); + LOGEMERG("Failed to accept on proxy socket"); ret = 1; goto out; } From b6e9d18bfcf602487968355f8d72e678e3d4d95b Mon Sep 17 00:00:00 2001 From: ckolivas Date: Thu, 26 Jun 2014 15:02:22 +0300 Subject: [PATCH 4/7] Handle signal interruptions on all poll and select calls --- src/ckpool.c | 7 +------ src/connector.c | 11 ++++------- src/libckpool.c | 28 ++++++++++------------------ 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index 7f45025b..95e4a865 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -227,7 +227,6 @@ void empty_buffer(connsock_t *cs) * of the buffer for use on the next receive. */ int read_socket_line(connsock_t *cs, int timeout) { - tv_t tv_timeout = {timeout, 0}; char *eom = NULL; size_t buflen; int ret = -1; @@ -250,11 +249,7 @@ int read_socket_line(connsock_t *cs, int timeout) while (42) { char readbuf[PAGESIZE] = {}; - FD_ZERO(&rd); - FD_SET(cs->fd, &rd); - if (eom) - tv_timeout.tv_sec = tv_timeout.tv_usec = 0; - ret = select(cs->fd + 1, &rd, NULL, NULL, &tv_timeout); + wait_read_select(cs->fd, eom ? 0 : timeout); if (eom && !ret) break; if (ret < 1) { diff --git a/src/connector.c b/src/connector.c index ae0893ce..78123f1c 100644 --- a/src/connector.c +++ b/src/connector.c @@ -260,7 +260,9 @@ retry: cksleep_ms(100); goto retry; } - ret = poll(fds, nfds, 1000); + do { + ret = poll(fds, nfds, 1000); + } while (unlikely(ret < 0 && interrupted())); if (ret < 0) { LOGERR("Failed to poll in receiver"); goto out; @@ -308,7 +310,6 @@ void *sender(void *arg) while (42) { sender_send_t *sender_send; client_instance_t *client; - tv_t timeout_tv = {0, 0}; bool only_send = false; int ret, fd, ofs = 0; @@ -346,11 +347,7 @@ void *sender(void *arg) * ready to receive data from us, put the send back on the * list. */ if (!only_send) { - fd_set writefds; - - FD_ZERO(&writefds); - FD_SET(fd, &writefds); - ret = select(fd + 1, NULL, &writefds, NULL, &timeout_tv); + ret = wait_write_select(fd, 0); if (ret < 1) { LOGDEBUG("Client %d not ready for writes", client->id); diff --git a/src/libckpool.c b/src/libckpool.c index 74c1cf73..01dca0ac 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -411,19 +411,15 @@ int connect_socket(char *url, char *port) * we can connect to quickly. */ noblock_socket(sockd); if (connect(sockd, p->ai_addr, p->ai_addrlen) == -1) { - struct timeval tv_timeout = {5, 0}; int selret; - fd_set rw; if (!sock_connecting()) { close(sockd); LOGDEBUG("Failed sock connect"); continue; } - FD_ZERO(&rw); - FD_SET(sockd, &rw); - selret = select(sockd + 1, NULL, &rw, NULL, &tv_timeout); - if (selret > 0 && FD_ISSET(sockd, &rw)) { + selret = wait_write_select(sockd, 5); + if (selret > 0) { socklen_t len; int err, n; @@ -457,13 +453,9 @@ out: int write_socket(int fd, const void *buf, size_t nbyte) { - tv_t tv_timeout = {1, 0}; - fd_set writefds; int ret; - FD_ZERO(&writefds); - FD_SET(fd, &writefds); - ret = select(fd + 1, NULL, &writefds, NULL, &tv_timeout); + ret = wait_write_select(fd, 5); if (ret < 1) { if (!ret) LOGNOTICE("Select timed out in write_socket"); @@ -487,12 +479,8 @@ void empty_socket(int fd) do { char buf[PAGESIZE]; - tv_t timeout = {0, 0}; - fd_set rd; - FD_ZERO(&rd); - FD_SET(fd, &rd); - ret = select(fd + 1, &rd, NULL, NULL, &timeout); + ret = wait_read_select(fd, 0); if (ret > 0) { ret = recv(fd, buf, PAGESIZE - 1, 0); buf[ret] = 0; @@ -625,7 +613,9 @@ int wait_read_select(int sockd, int timeout) FD_ZERO(&readfs); FD_SET(sockd, &readfs); - ret = select(sockd + 1, &readfs, NULL, NULL, &tv_timeout); + do { + ret = select(sockd + 1, &readfs, NULL, NULL, &tv_timeout); + } while (unlikely(ret < 0 && interrupted())); return ret; } @@ -700,7 +690,9 @@ int wait_write_select(int sockd, int timeout) FD_ZERO(&writefds); FD_SET(sockd, &writefds); - ret = select(sockd + 1, NULL, &writefds, NULL, &tv_timeout); + do { + ret = select(sockd + 1, NULL, &writefds, NULL, &tv_timeout); + } while (unlikely(ret < 0 && interrupted())); return ret; } From e789416b1737788a042df114d1a908a4a00847b8 Mon Sep 17 00:00:00 2001 From: ckolivas Date: Thu, 26 Jun 2014 15:15:36 +0300 Subject: [PATCH 5/7] Fix unused variable warnings --- src/ckpool.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ckpool.c b/src/ckpool.c index 95e4a865..11da8ea1 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -230,7 +230,6 @@ int read_socket_line(connsock_t *cs, int timeout) char *eom = NULL; size_t buflen; int ret = -1; - fd_set rd; if (unlikely(cs->fd < 0)) goto out; @@ -692,6 +691,8 @@ static void sighandler(int sig) { ckpool_t *ckp = global_ckp; + LOGWARNING("Parent process %s received signal %d, shutting down", + ckp->name, sig); pthread_cancel(ckp->pth_watchdog); join_pthread(ckp->pth_watchdog); From a092fd91a4d14ee6926c32b4b3de0105d94b5bac Mon Sep 17 00:00:00 2001 From: ckolivas Date: Thu, 26 Jun 2014 15:22:39 +0300 Subject: [PATCH 6/7] Forgot to set return value in read_socket_line --- src/ckpool.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ckpool.c b/src/ckpool.c index 11da8ea1..0e301b4c 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -248,7 +248,7 @@ int read_socket_line(connsock_t *cs, int timeout) while (42) { char readbuf[PAGESIZE] = {}; - wait_read_select(cs->fd, eom ? 0 : timeout); + ret = wait_read_select(cs->fd, eom ? 0 : timeout); if (eom && !ret) break; if (ret < 1) { From c9e7dd5780aacc08fc88d81564666dde211d63b8 Mon Sep 17 00:00:00 2001 From: ckolivas Date: Thu, 26 Jun 2014 15:31:15 +0300 Subject: [PATCH 7/7] We do not retry the accept call if it fails --- src/connector.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connector.c b/src/connector.c index 78123f1c..78c5c4bf 100644 --- a/src/connector.c +++ b/src/connector.c @@ -477,7 +477,7 @@ retry: close(sockd); sockd = accept(us->sockd, NULL, NULL); if (sockd < 0) { - LOGERR("Failed to accept on connector socket, retrying in 5s"); + LOGEMERG("Failed to accept on connector socket, exiting"); ret = 1; goto out; }