102df2
diff --git a/bin/dig/dighost.c b/bin/dig/dighost.c
ad7b3b
index aa5315d..1fa711a 100644
102df2
--- a/bin/dig/dighost.c
102df2
+++ b/bin/dig/dighost.c
ad7b3b
@@ -1814,6 +1814,13 @@ clear_query(dig_query_t *query) {
Adam Tkac f8a48c
 
102df2
 	if (query->timer != NULL)
102df2
 		isc_timer_detach(&query->timer);
102df2
+
Adam Tkac f8a48c
+	if (query->waiting_senddone) {
Adam Tkac f8a48c
+		debug("send_done not yet called");
ad7b3b
+		query->pending_free = true;
Adam Tkac f8a48c
+		return;
Adam Tkac f8a48c
+	}
Adam Tkac f8a48c
+
Adam Tkac f8a48c
 	lookup = query->lookup;
Adam Tkac f8a48c
 
Adam Tkac f8a48c
 	if (lookup->current_query == query)
ad7b3b
@@ -1839,10 +1846,7 @@ clear_query(dig_query_t *query) {
Adam Tkac f8a48c
 	isc_mempool_put(commctx, query->recvspace);
Adam Tkac f8a48c
 	isc_buffer_invalidate(&query->recvbuf);
Adam Tkac f8a48c
 	isc_buffer_invalidate(&query->lengthbuf);
Adam Tkac f8a48c
-	if (query->waiting_senddone)
ad7b3b
-		query->pending_free = true;
Adam Tkac f8a48c
-	else
Adam Tkac f8a48c
-		isc_mem_free(mctx, query);
Adam Tkac f8a48c
+	isc_mem_free(mctx, query);
Adam Tkac f8a48c
 }
Adam Tkac f8a48c
 
Adam Tkac f8a48c
 /*%
ad7b3b
@@ -2892,9 +2896,9 @@ send_done(isc_task_t *_task, isc_event_t *event) {
Adam Tkac f8a48c
 	isc_event_free(&event);
Adam Tkac f8a48c
 
Adam Tkac f8a48c
 	if (query->pending_free)
Adam Tkac f8a48c
-		isc_mem_free(mctx, query);
Adam Tkac f8a48c
+		clear_query(query);
Adam Tkac f8a48c
 
Adam Tkac f8a48c
-	check_if_done();
Adam Tkac f8a48c
+	check_next_lookup(l);
Adam Tkac f8a48c
 	UNLOCK_LOOKUP;
Adam Tkac f8a48c
 }
Adam Tkac f8a48c