diff --git a/calendar/ChangeLog b/calendar/ChangeLog
index 2c772ac..db71604 100644
--- a/calendar/ChangeLog
+++ b/calendar/ChangeLog
@@ -1,3 +1,11 @@
+2007-12-31  Ondrej Jirman  <megous@megous.com>
+
+	** Fix for bug #506457
+
+	* libedata-cal/e-cal-backend.c: (e_cal_backend_remove_client):
+	New clients might have connected while last_client_gone signal was being
+	emitted. This should fix the race.
+
 2007-12-13  Milan Crha  <mcrha@redhat.com>
 
 	** Fix for bug #502866
diff --git a/calendar/libedata-cal/e-cal-backend.c b/calendar/libedata-cal/e-cal-backend.c
index 166858a..187fa3e 100644
--- a/calendar/libedata-cal/e-cal-backend.c
+++ b/calendar/libedata-cal/e-cal-backend.c
@@ -415,9 +415,6 @@ e_cal_backend_remove_client (ECalBackend *backend, EDataCal *cal)
 {
 	ECalBackendPrivate *priv;
 
-	/* XXX this needs a bit more thinking wrt the mutex - we
-	   should be holding it when we check to see if clients is
-	   NULL */
 	g_return_if_fail (backend != NULL);
 	g_return_if_fail (E_IS_CAL_BACKEND (backend));
 	g_return_if_fail (cal != NULL);
@@ -425,16 +422,24 @@ e_cal_backend_remove_client (ECalBackend *backend, EDataCal *cal)
 
 	priv = backend->priv;
 
+	/* up our backend's refcount here so that last_client_gone
+	   doesn't end up unreffing us (while we're holding the
+	   lock) */
+	g_object_ref (backend);
+
 	/* Disconnect */
 	g_mutex_lock (priv->clients_mutex);
 	priv->clients = g_list_remove (priv->clients, cal);
-	g_mutex_unlock (priv->clients_mutex);
 
 	/* When all clients go away, notify the parent factory about it so that
 	 * it may decide whether to kill the backend or not.
 	 */
 	if (!priv->clients)
 		last_client_gone (backend);
+
+	g_mutex_unlock (priv->clients_mutex);
+
+	g_object_unref (backend);
 }
 
 /**
