diff --git a/camel/providers/imapx/camel-imapx-server.c b/camel/providers/imapx/camel-imapx-server.c
index 8edb1ac..2fd8728 100644
--- a/camel/providers/imapx/camel-imapx-server.c
+++ b/camel/providers/imapx/camel-imapx-server.c
@@ -3584,17 +3584,23 @@ imapx_job_idle_start (CamelIMAPXJob *job,
 		   the IDLE command. */
 		if (camel_imapx_command_queue_is_empty (is->active)) {
 			imapx_command_start (is, ic);
+			QUEUE_UNLOCK (is);
 		} else {
 			c (is->tagprefix, "finally cancelling IDLE, other command was quicker\n");
+			QUEUE_UNLOCK (is);
+
 			imapx_unregister_job (is, job);
+
+			g_mutex_lock (&is->priv->idle_lock);
+			if (is->priv->idle_state == IMAPX_IDLE_PENDING)
+				is->priv->idle_state = IMAPX_IDLE_OFF;
+			g_mutex_unlock (&is->priv->idle_lock);
 		}
 	} else {
 		g_mutex_unlock (&is->priv->idle_lock);
 
-		QUEUE_LOCK (is);
 		imapx_unregister_job (is, job);
 	}
-	QUEUE_UNLOCK (is);
 
 	camel_imapx_command_unref (ic);
 
