diff --git a/camel/camel-net-utils.c b/camel/camel-net-utils.c
index d447e41..7ea5bbf 100644
--- a/camel/camel-net-utils.c
+++ b/camel/camel-net-utils.c
@@ -512,7 +512,15 @@ cs_waitinfo(gpointer (worker)(gpointer), struct _addrinfo_msg *msg, const gchar
 		} else {
 			struct _addrinfo_msg *reply = (struct _addrinfo_msg *)camel_msgport_try_pop(reply_port);
 
-			g_assert(reply == msg);
+			if (!msg->cancelled)
+				g_assert(reply == msg);
+			else {
+				static gboolean first = TRUE;
+				if (first) {
+					printf ("%s: thread was cancelled, reply:%p msg:%p\n", __FUNCTION__, reply, msg);
+					first = FALSE;
+				}
+			}
 			d(printf("waiting for child to exit\n"));
 			pthread_join(id, NULL);
 			d(printf("child done\n"));
@@ -525,6 +533,11 @@ cs_waitinfo(gpointer (worker)(gpointer), struct _addrinfo_msg *msg, const gchar
 	return cancel;
 }
 
+#define test_cancelled(_msg)	G_STMT_START {				\
+	gint old_cancelled = (_msg)->cancelled; (_msg)->cancelled = 1;	\
+	pthread_testcancel ();						\
+	(_msg)->cancelled = old_cancelled; } G_STMT_END
+
 #ifdef NEED_ADDRINFO
 static gpointer
 cs_getaddrinfo(gpointer data)
@@ -535,17 +548,17 @@ cs_getaddrinfo(gpointer data)
 	struct addrinfo *res, *last = NULL;
 	struct sockaddr_in *sin;
 	in_port_t port = 0;
-	gint i;
+	gint i, old_cancelstate = PTHREAD_CANCEL_ENABLE;
 
 	/* This is a pretty simplistic emulation of getaddrinfo */
 
 	while ((msg->result = camel_gethostbyname_r(msg->name, &h, msg->hostbufmem, msg->hostbuflen, &herr)) == ERANGE) {
-		pthread_testcancel();
+		test_cancelled (msg);
                 msg->hostbuflen *= 2;
                 msg->hostbufmem = g_realloc(msg->hostbufmem, msg->hostbuflen);
 	}
 
-	pthread_testcancel ();
+	test_cancelled (msg);
 
 	/* If we got cancelled, dont reply, just free it */
 	if (msg->cancelled)
@@ -599,7 +612,7 @@ cs_getaddrinfo(gpointer data)
 		}
 	}
 
-	pthread_testcancel ();
+	test_cancelled (msg);
 
 	for (i=0;h.h_addr_list[i];i++) {
 		res = g_malloc0(sizeof(*res));
@@ -630,7 +643,9 @@ cs_getaddrinfo(gpointer data)
 		}
 	}
 reply:
+	pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &old_cancelstate);
 	camel_msgport_reply((CamelMsg *)msg);
+	pthread_setcancelstate (old_cancelstate, &old_cancelstate);
 cancel:
 	return NULL;
 }
@@ -651,10 +666,15 @@ cs_getaddrinfo(gpointer data)
 			info->result = getaddrinfo(info->name, "443", info->hints, info->res);
 	}
 
-	pthread_testcancel ();
+	test_cancelled (info);
 
-	if (!info->cancelled)
+	if (!info->cancelled) {
+		gint old_cancelstate = PTHREAD_CANCEL_ENABLE;
+
+		pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &old_cancelstate);
 		camel_msgport_reply((CamelMsg *)info);
+		pthread_setcancelstate (old_cancelstate, &old_cancelstate);
+	}
 
 	return NULL;
 }
@@ -747,7 +767,7 @@ cs_getnameinfo(gpointer data)
 
 	while ((msg->result = camel_gethostbyaddr_r((const gchar *)&sin->sin_addr, sizeof(sin->sin_addr), AF_INET, &h,
 						    msg->hostbufmem, msg->hostbuflen, &herr)) == ERANGE) {
-		pthread_testcancel ();
+		test_cancelled (msg);
                 msg->hostbuflen *= 2;
                 msg->hostbufmem = g_realloc(msg->hostbufmem, msg->hostbuflen);
 	}
@@ -755,7 +775,7 @@ cs_getnameinfo(gpointer data)
 	if (msg->cancelled)
 		goto cancel;
 
-	pthread_testcancel ();
+	test_cancelled (msg);
 
 	if (msg->host) {
 		g_free(msg->host);
@@ -786,7 +806,7 @@ cs_getnameinfo(gpointer data)
 	/* there doens't appear to be a return code which says host or serv buffers are too short, lengthen them */
 	msg->result = getnameinfo(msg->addr, msg->addrlen, msg->host, msg->hostlen, msg->serv, msg->servlen, msg->flags);
 
-	pthread_testcancel ();
+	test_cancelled (msg);
 
 	if (!msg->cancelled)
 		camel_msgport_reply((CamelMsg *)msg);
