changeset 21325:915b968cae77

imapc: Fix infinite reconnect loop to remote server that is down This happened only in some situations. Usually there would have bene some command in the queue, which would cause the reconnect-check to fail.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 15 Dec 2016 17:31:18 +0200
parents ce210ada8bc2
children 2534aa23ae2d
files src/lib-imap-client/imapc-connection.c
diffstat 1 files changed, 12 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-imap-client/imapc-connection.c	Sat Dec 17 10:55:01 2016 +0200
+++ b/src/lib-imap-client/imapc-connection.c	Thu Dec 15 17:31:18 2016 +0200
@@ -126,6 +126,7 @@
 	struct timeout *to_throttle, *to_throttle_shrink;
 
 	unsigned int reconnecting:1;
+	unsigned int reconnect_ok:1;
 	unsigned int idling:1;
 	unsigned int idle_stopping:1;
 	unsigned int idle_plus_waiting:1;
@@ -150,6 +151,7 @@
 	conn->name = i_strdup_printf("%s:%u", client->set.host,
 				     client->set.port);
 	conn->literal.fd = -1;
+	conn->reconnect_ok = TRUE;
 	i_array_init(&conn->cmd_send_queue, 8);
 	i_array_init(&conn->cmd_wait_list, 32);
 	i_array_init(&conn->literal_files, 4);
@@ -434,12 +436,16 @@
 {
 	if (conn->selected_box != NULL)
 		return imapc_client_mailbox_can_reconnect(conn->selected_box);
-	else
-		return conn->reconnect_command_count == 0;
+	else {
+		return conn->reconnect_command_count == 0 &&
+			conn->reconnect_ok;
+	}
 }
 
 static void imapc_connection_reconnect(struct imapc_connection *conn)
 {
+	conn->reconnect_ok = FALSE;
+
 	if (conn->selected_box != NULL)
 		imapc_client_mailbox_reconnect(conn->selected_box);
 	else {
@@ -1372,6 +1378,10 @@
 				conn->selected_box->reconnect_ok = TRUE;
 		}
 	}
+	if (conn->reconnect_command_count == 0) {
+		/* we've successfully received replies to some commands. */
+		conn->reconnect_ok = TRUE;
+	}
 	imapc_connection_input_reset(conn);
 	imapc_command_reply_free(cmd, &reply);
 	imapc_command_send_more(conn);