I came across what looks like a missing return in DnsQueryContext.writeQuery(boolean). In the branch that handles an exhausted query-ID space, the method fails the query but then keeps executing instead of returning:
|
if ((id = queryContextManager.add(nameServerAddr, this)) == -1) { |
|
// We did exhaust the id space, fail the query |
|
IllegalStateException e = new IllegalStateException("query ID space exhausted: " + question()); |
|
finishFailure("failed to send a query via " + protocol(), e, false); |
|
queryLifecycleObserver.queryWritten(nameServerAddr, channel.newFailedFuture(e)); |
|
} |
When queryContextManager.add(...) returns -1, the query is failed via finishFailure(...), but execution falls through: the completion listener is registered, newQuery(-1, ...) is called, and sendQuery(...) ends up writing an already-failed query to the wire with id -1 (the encoder truncates it to 65535). The listener then calls removeFromContextManager(-1) for an id that was never stored, which trips an assertion when assertions are enabled.
I reproduced it by exhausting the id space for a single nameserver (65536 in-flight queries) and calling writeQuery once more. A query is still written to the channel even though the promise is already failed:
expected: <null> but was: <DatagramDnsQuery(to: localhost/127.0.0.1:53, id: 65535, QUERY(0), RD)>
and the completion listener throws:
java.lang.AssertionError: DnsQueryContext not found, id: -1
at io.netty.resolver.dns.DnsQueryContextManager$DnsQueryContextMap.remove(DnsQueryContextManager.java:186)
at io.netty.resolver.dns.DnsQueryContextManager.remove(DnsQueryContextManager.java:85)
at io.netty.resolver.dns.DnsQueryContext.removeFromContextManager(DnsQueryContext.java:234)
at io.netty.resolver.dns.DnsQueryContext.lambda$writeQuery$0(DnsQueryContext.java:206)
at io.netty.resolver.dns.DnsQueryContext.writeQuery(DnsQueryContext.java:184)
This looks like it changed with #15812, where the branch previously did return channel.newFailedFuture(e);. Adding a return; after the queryWritten(...) call appears to restore the earlier behaviour. I'd be happy to open a PR.