102df2
diff --git a/bin/dig/dighost.c b/bin/dig/dighost.c
102df2
index f657c30..ff9a2d2 100644
102df2
--- a/bin/dig/dighost.c
102df2
+++ b/bin/dig/dighost.c
102df2
@@ -1694,6 +1694,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");
Adam Tkac f8a48c
+		query->pending_free = ISC_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)
102df2
@@ -1719,10 +1726,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)
Adam Tkac f8a48c
-		query->pending_free = ISC_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
 /*%
102df2
@@ -2811,9 +2815,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