Index: camel/providers/imap/ChangeLog
===================================================================
--- camel/providers/imap/ChangeLog	(revision 8552)
+++ camel/providers/imap/ChangeLog	(working copy)
@@ -1,3 +1,10 @@
+2008-03-05  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #520526
+
+	* camel-imap-folder.c: (imap_rescan), (imap_update_summary):
+	Stop work as soon as we know application is going to exit.
+
 2008-02-18  Chenthill Palanisamy  <pchenthill@novell.com>
 
 	* camel-imap-store.c (connect_to_server): Fix for some
Index: camel/providers/imap/camel-imap-folder.c
===================================================================
--- camel/providers/imap/camel-imap-folder.c	(revision 8552)
+++ camel/providers/imap/camel-imap-folder.c	(working copy)
@@ -697,6 +697,9 @@ imap_rescan (CamelFolder *folder, int ex
 		char *uid;
 		guint32 flags;
 
+		if (camel_application_is_exiting)
+			break;
+
 		data = parse_fetch_response (imap_folder, resp);
 		g_free (resp);
 		if (!data)
@@ -718,7 +721,7 @@ imap_rescan (CamelFolder *folder, int ex
 	}
 
 	camel_operation_end (NULL);
-	if (type == CAMEL_IMAP_RESPONSE_ERROR) {
+	if (type == CAMEL_IMAP_RESPONSE_ERROR || camel_application_is_exiting) {
 		for (i = 0; i < summary_len && new[i].uid; i++)
 			g_free (new[i].uid);
 		g_free (new);
@@ -2489,6 +2492,7 @@ imap_update_summary (CamelFolder *folder
 	CamelStream *stream;
 	char *uid, *resp;
 	GData *data;
+	extern int camel_application_is_exiting;
 
 	if (store->server_level >= IMAP_LEVEL_IMAP4REV1) {
 		if (store->headers == IMAP_FETCH_ALL_HEADERS)
@@ -2543,8 +2547,10 @@ imap_update_summary (CamelFolder *folder
 	 */
 	fetch_data = g_ptr_array_new ();
 	messages = g_ptr_array_new ();
-	while ((type = camel_imap_command_response (store, &resp, ex)) ==
-	       CAMEL_IMAP_RESPONSE_UNTAGGED) {
+	while ((type = camel_imap_command_response (store, &resp, ex)) == CAMEL_IMAP_RESPONSE_UNTAGGED) {
+		if (camel_application_is_exiting)
+			break;
+
 		data = parse_fetch_response (imap_folder, resp);
 		g_free (resp);
 		if (!data)
@@ -2576,7 +2582,7 @@ imap_update_summary (CamelFolder *folder
 	}
 	camel_operation_end (NULL);
 
-	if (type == CAMEL_IMAP_RESPONSE_ERROR)
+	if (type == CAMEL_IMAP_RESPONSE_ERROR || camel_application_is_exiting)
 		goto lose;
 
 	/* Free the final tagged response */
@@ -2607,7 +2613,7 @@ imap_update_summary (CamelFolder *folder
 
 		camel_operation_start (NULL, _("Fetching summary information for new messages in %s"), folder->name);
 
-		while (uid < needheaders->len) {
+		while (uid < needheaders->len && !camel_application_is_exiting) {
 			uidset = imap_uid_array_to_set (folder->summary, needheaders, uid, UID_SET_LIMIT, &uid);
 			if (!camel_imap_command_start (store, folder, ex,
 						       "UID FETCH %s BODY.PEEK[%s]",
@@ -2622,6 +2628,9 @@ imap_update_summary (CamelFolder *folder
 
 			while ((type = camel_imap_command_response (store, &resp, ex))
 			       == CAMEL_IMAP_RESPONSE_UNTAGGED) {
+				if (camel_application_is_exiting)
+					break;
+
 				data = parse_fetch_response (imap_folder, resp);
 				g_free (resp);
 				if (!data)
@@ -2636,7 +2645,7 @@ imap_update_summary (CamelFolder *folder
 				g_datalist_clear (&data);
 			}
 
-			if (type == CAMEL_IMAP_RESPONSE_ERROR) {
+			if (type == CAMEL_IMAP_RESPONSE_ERROR || camel_application_is_exiting) {
 				g_ptr_array_free (needheaders, TRUE);
 				camel_operation_end (NULL);
 				goto lose;
@@ -2645,6 +2654,9 @@ imap_update_summary (CamelFolder *folder
 		g_string_free (header_spec, TRUE);
 		g_ptr_array_free (needheaders, TRUE);
 		camel_operation_end (NULL);
+
+		if (camel_application_is_exiting)
+			goto lose;
 	}
 
 	/* Now finish up summary entries (fix UIDs, set flags and size) */
