From dd1d464a861fde95087a72038512561e10dddd51 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 10 Apr 2014 22:15:03 +0200 Subject: [PATCH] frontend: Be more robust in case of failed connections. Make sure not to write to a failed connection, most notably, don't try to log to a failed connection. --- src/frontend/connection.c | 14 +++++++++++++- src/frontend/sock.c | 7 +++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/frontend/connection.c b/src/frontend/connection.c index 12812c7..69c71ed 100644 --- a/src/frontend/connection.c +++ b/src/frontend/connection.c @@ -142,7 +142,8 @@ connection_destroy(sdb_object_t *obj) sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection on fd=%i", conn->fd); - close(conn->fd); + if (conn->fd >= 0) + close(conn->fd); conn->fd = -1; sdb_strbuf_destroy(conn->buf); @@ -382,6 +383,9 @@ connection_read(sdb_conn_t *conn) { ssize_t n = 0; + if ((! conn) || (conn->fd < 0)) + return -1; + while (42) { ssize_t status; @@ -390,6 +394,9 @@ connection_read(sdb_conn_t *conn) if (status < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break; + + close(conn->fd); + conn->fd = -1; return (int)status; } else if (! status) /* EOF */ @@ -470,6 +477,11 @@ sdb_connection_send(sdb_conn_t *conn, uint32_t code, if (status < 0) { char errbuf[1024]; + /* tell other code that there was a problem and, more importantly, + * make sure we don't try to send further logs to the connection */ + close(conn->fd); + conn->fd = -1; + sdb_log(SDB_LOG_ERR, "frontend: Failed to send msg " "(code: %u, len: %u) to client: %s", code, msg_len, sdb_strerror(errno, errbuf, sizeof(errbuf))); diff --git a/src/frontend/sock.c b/src/frontend/sock.c index 70f22d7..7cfdb7c 100644 --- a/src/frontend/sock.c +++ b/src/frontend/sock.c @@ -534,6 +534,13 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop) while (sdb_llist_iter_has_next(iter)) { sdb_object_t *obj = sdb_llist_iter_get_next(iter); + + if (CONN(obj)->fd < 0) { + sdb_llist_iter_remove_current(iter); + sdb_object_deref(obj); + continue; + } + FD_SET(CONN(obj)->fd, &ready); FD_SET(CONN(obj)->fd, &exceptions); -- 2.30.2