Code

frontend: Be more robust in case of failed connections.
authorSebastian Harl <sh@tokkee.org>
Thu, 10 Apr 2014 20:15:03 +0000 (22:15 +0200)
committerSebastian Harl <sh@tokkee.org>
Thu, 10 Apr 2014 20:15:03 +0000 (22:15 +0200)
Make sure not to write to a failed connection, most notably, don't try to log
to a failed connection.

src/frontend/connection.c
src/frontend/sock.c

index 12812c7132b20e35da6954757ddbcb5f6f2e7a7d..69c71edbd3e6f7ac9e5876841d9237f738267ab5 100644 (file)
@@ -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)));
index 70f22d705b694a4e24b6ff0639cd4cd33e0f4932..7cfdb7cac4b84d21c2f476e38b08855ea80c970b 100644 (file)
@@ -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);