Index: mail/mail-stub-exchange.c
===================================================================
--- mail/mail-stub-exchange.c	(revision 1843)
+++ mail/mail-stub-exchange.c	(working copy)
@@ -402,13 +402,17 @@ message_removed (MailStub *stub, MailStu
 	MailStubExchangeMessage *mmsg;
 	guint index;
 
+	g_static_rec_mutex_lock (&g_changed_msgs_mutex);
 	mmsg = g_hash_table_lookup (mfld->messages_by_href, href);
-	if (!mmsg)
+	if (!mmsg) {
+		g_static_rec_mutex_unlock (&g_changed_msgs_mutex);
 		return;
+	}
 	index = find_message_index (mfld, mmsg->seq);
 	g_return_if_fail (index != -1);
 
 	message_remove_at_index (stub, mfld, index);
+	g_static_rec_mutex_unlock (&g_changed_msgs_mutex);
 }
 
 static void
@@ -1134,12 +1138,16 @@ sync_deletions (MailStubExchange *mse, M
 
 	e2k_results_free (results, nresults);
 
+	g_static_rec_mutex_lock (&g_changed_msgs_mutex);
 	if (visible_count >= mfld->messages->len) {
-		if (mfld->deleted_count == deleted_count)
+		if (mfld->deleted_count == deleted_count) {
+			g_static_rec_mutex_unlock (&g_changed_msgs_mutex);
 			return;
+		}
 
 		if (mfld->deleted_count == 0) {
 			mfld->deleted_count = deleted_count;
+			g_static_rec_mutex_unlock (&g_changed_msgs_mutex);
 			return;
 		}
 	}
@@ -1165,7 +1173,7 @@ sync_deletions (MailStubExchange *mse, M
 	my_i = mfld->messages->len - 1;
 	while ((result = e2k_result_iter_next (iter))) {
 		mmsg = find_message_by_href (mfld, result->href);
-		if (!mmsg || mmsg->seq >= highest_verified_seq) {
+		if (!mmsg || (mmsg->seq >= highest_verified_seq && highest_verified_seq != -1)) {
 			/* This is a new message or a message we already
 			 * verified. Skip it.
 			 */
@@ -1193,6 +1201,7 @@ sync_deletions (MailStubExchange *mse, M
 			message_removed (stub, mfld, my_mmsg->href);
 			changes = TRUE;
 			my_i--;
+			highest_unverified_index--;
 			my_mmsg = mfld->messages->pdata[my_i];
 		}
 		highest_verified_seq = mmsg->seq;
@@ -1206,6 +1215,7 @@ sync_deletions (MailStubExchange *mse, M
 		}
 
 		my_i--;
+		highest_unverified_index--;
 	}
 	status = e2k_result_iter_free (iter);
 
@@ -1220,6 +1230,8 @@ sync_deletions (MailStubExchange *mse, M
 		changes = TRUE;
 	}
 
+	g_static_rec_mutex_unlock (&g_changed_msgs_mutex);
+
 	if (changes)
 		mail_stub_push_changes (stub);
 }
@@ -1446,6 +1458,7 @@ refresh_folder_internal (MailStub *stub,
 			       CAMEL_STUB_ARG_FOLDER, mfld->name,
 			       CAMEL_STUB_ARG_END);
 
+	g_static_rec_mutex_lock (&g_changed_msgs_mutex);
 	qsort (messages->data, messages->len,
 	       sizeof (rm), refresh_message_compar);
 	for (i = 0; i < messages->len; i++) {
@@ -1500,6 +1513,7 @@ refresh_folder_internal (MailStub *stub,
 			       CAMEL_STUB_ARG_END);
 
 	mfld->scanned = TRUE;
+	g_static_rec_mutex_unlock (&g_changed_msgs_mutex);
 	folder_changed (mfld);
 
 	if (background)
@@ -1591,6 +1605,7 @@ expunge_uids (MailStub *stub, const char
 	if (!mfld)
 		return;
 
+	g_static_rec_mutex_lock (&g_changed_msgs_mutex);
 	hrefs = g_ptr_array_new ();
 	for (i = 0; i < uids->len; i++) {
 		mmsg = find_message (mfld, uids->pdata[i]);
@@ -1604,6 +1619,7 @@ expunge_uids (MailStub *stub, const char
 		 */
 		g_ptr_array_free (hrefs, TRUE);
 		mail_stub_return_ok (stub);
+		g_static_rec_mutex_unlock (&g_changed_msgs_mutex);
 		return;
 	}
 
@@ -1627,6 +1643,7 @@ expunge_uids (MailStub *stub, const char
 		mail_stub_return_progress (stub, ndeleted * 100 / hrefs->len);
 	}
 	status = e2k_result_iter_free (iter);
+	g_static_rec_mutex_unlock (&g_changed_msgs_mutex);
 
 	mail_stub_return_data (stub, CAMEL_STUB_RETVAL_THAW_FOLDER,
 			       CAMEL_STUB_ARG_FOLDER, mfld->name,
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 1843)
+++ ChangeLog	(working copy)
@@ -1,3 +1,13 @@
+2008-12-08  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #522277
+
+	* mail/mail-stub-exchange.c: (message_removed), (sync_deletions),
+	(refresh_folder_internal), (expunge_uids):
+	Use global lock to prevent race conditions.
+	* mail/mail-stub-exchange.c: (sync_deletions): Use properly
+	highest_verified_seq and highest_unverified_index variables.
+
 2008-11-20  Milan Crha  <mcrha@redhat.com>
 
 	** Part of fix for bug #558883
