--- contrib/bind9/bin/dig/dighost.c.orig	2009-09-13 14:24:13.000000000 +0000
+++ contrib/bind9/bin/dig/dighost.c	2009-09-13 14:31:52.000000000 +0000
@@ -2604,11 +2604,13 @@
 
 	if (sevent->result == ISC_R_CANCELED) {
 		debug("in cancel handler");
-		isc_socket_detach(&query->sock);
-		sockcount--;
-		INSIST(sockcount >= 0);
-		debug("sockcount=%d", sockcount);
-		query->waiting_connect = ISC_FALSE;
+		if (query->sock != NULL) {
+			isc_socket_detach(&query->sock);
+			sockcount--;
+			INSIST(sockcount >= 0);
+			debug("sockcount=%d", sockcount);
+			query->waiting_connect = ISC_FALSE;
+		}
 		isc_event_free(&event);
 		l = query->lookup;
 		clear_query(query);
