GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Return early in DnsQueryContext.writeQuery when the query ID space is… · netty/netty@ec35cbe

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • resolver-dns/src
    • main/java/io/netty/resolver/dns
      • DnsQueryContext.java
    • test/java/io/netty/resolver/dns
      • DnsQueryContextTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎resolver-dns/src/main/java/io/netty/resolver/dns/DnsQueryContext.java‎

Copy file name to clipboardExpand all lines: resolver-dns/src/main/java/io/netty/resolver/dns/DnsQueryContext.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ final void writeQuery(boolean flush) {
178178
IllegalStateException e = new IllegalStateException("query ID space exhausted: " + question());
179179
finishFailure("failed to send a query via " + protocol(), e, false);
180180
queryLifecycleObserver.queryWritten(nameServerAddr, channel.newFailedFuture(e));
181+
return;
181182
}
182183

183184
// Ensure we remove the id from the QueryContextManager once the query completes.
Collapse file

‎resolver-dns/src/test/java/io/netty/resolver/dns/DnsQueryContextTest.java‎

Copy file name to clipboard
+163Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.resolver.dns;
17+
18+
import io.netty.channel.AddressedEnvelope;
19+
import io.netty.channel.embedded.EmbeddedChannel;
20+
import io.netty.handler.codec.dns.DefaultDnsQuestion;
21+
import io.netty.handler.codec.dns.DnsRecord;
22+
import io.netty.handler.codec.dns.DnsRecordType;
23+
import io.netty.handler.codec.dns.DnsResponse;
24+
import io.netty.util.ReferenceCountUtil;
25+
import io.netty.util.concurrent.Promise;
26+
import org.junit.jupiter.api.Test;
27+
28+
import java.net.InetAddress;
29+
import java.net.InetSocketAddress;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
34+
import static org.junit.jupiter.api.Assertions.assertNull;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
36+
37+
/**
38+
* Regression test for the missing {@code return;} in
39+
* {@link DnsQueryContext#writeQuery(boolean)} after
40+
* {@code finishFailure()} is called when the query-ID space is exhausted.
41+
*
42+
* <p>When {@code queryContextManager.add(...)} returns {@code -1} the method
43+
* must fail the promise and return immediately. Without the fix, execution
44+
* falls through: a second {@code queryLifecycleObserver.queryWritten()} call
45+
* is made, {@code newQuery(-1, ...)} builds a bogus datagram with id 65535,
46+
* and {@code sendQuery()} actually writes it to the channel.
47+
*/
48+
public class DnsQueryContextTest {
49+
50+
/**
51+
* Regression test: when the DNS transaction-ID space is exhausted,
52+
* {@code writeQuery} must NOT write any message to the channel.
53+
*
54+
* <p>The bug: a missing {@code return;} after {@code finishFailure()} caused
55+
* fall-through into {@code sendQuery()}, writing a query with id -1 (encoded
56+
* as 65535) to the channel even though the promise was already failed.
57+
*
58+
* <p>This test is intentionally RED against the unfixed code: with the bug
59+
* present, {@code channel.readOutbound()} returns a non-null
60+
* {@link io.netty.handler.codec.dns.DatagramDnsQuery}, causing the assertion
61+
* {@code assertNull(outbound)} to fail.
62+
*/
63+
@Test
64+
public void writeQueryMustNotSendWhenIdSpaceExhausted() throws Exception {
65+
EmbeddedChannel channel = new EmbeddedChannel();
66+
try {
67+
InetSocketAddress nameServerAddr =
68+
new InetSocketAddress(InetAddress.getLoopbackAddress(), 53);
69+
70+
DnsQueryContextManager manager = new DnsQueryContextManager();
71+
72+
// Exhaust the entire ID space for nameServerAddr.
73+
// DnsQueryIdSpace.maxUsableIds() == 65536 (IDs 0..65535).
74+
// We need one dummy context object to fill the manager; reusing the
75+
// same instance is safe because DnsQueryContextMap stores contexts
76+
// by ID (which is unique per add()), not by identity.
77+
//
78+
// We create the dummy with a separate promise so that it is never
79+
// written and does not interfere with the context-under-test.
80+
Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>> dummyPromise =
81+
channel.eventLoop().newPromise();
82+
DatagramDnsQueryContext dummyCtx = new DatagramDnsQueryContext(
83+
channel,
84+
nameServerAddr,
85+
manager,
86+
NoopDnsQueryLifecycleObserver.INSTANCE,
87+
/* maxPayLoadSize */ 4096,
88+
/* recursionDesired */ true,
89+
/* queryTimeoutMillis */ 5000,
90+
new DefaultDnsQuestion("dummy.netty.io.", DnsRecordType.A),
91+
new DnsRecord[0],
92+
dummyPromise,
93+
/* socketBootstrap */ null,
94+
/* retryWithTcpOnTimeout */ false);
95+
96+
// Drain all 65536 IDs from the id space for nameServerAddr.
97+
DnsQueryIdSpace idSpace = new DnsQueryIdSpace();
98+
int maxIds = idSpace.maxUsableIds(); // 65536
99+
100+
for (int i = 0; i < maxIds; i++) {
101+
int assignedId = manager.add(nameServerAddr, dummyCtx);
102+
assertThat(assignedId)
103+
.as("Expected a valid id during exhaustion, got -1 at iteration %d", i)
104+
.isGreaterThanOrEqualTo(0);
105+
}
106+
107+
// The next add must return -1 — the space is now full.
108+
int overflowId = manager.add(nameServerAddr, dummyCtx);
109+
assertEquals(-1, overflowId,
110+
"Expected -1 when ID space is exhausted");
111+
112+
// Build the context-under-test that will be subjected to writeQuery.
113+
Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>> testPromise =
114+
channel.eventLoop().newPromise();
115+
DatagramDnsQueryContext ctx = new DatagramDnsQueryContext(
116+
channel,
117+
nameServerAddr,
118+
manager,
119+
NoopDnsQueryLifecycleObserver.INSTANCE,
120+
/* maxPayLoadSize */ 4096,
121+
/* recursionDesired */ true,
122+
/* queryTimeoutMillis */ 5000,
123+
new DefaultDnsQuestion("test.netty.io.", DnsRecordType.A),
124+
new DnsRecord[0],
125+
testPromise,
126+
/* socketBootstrap */ null,
127+
/* retryWithTcpOnTimeout */ false);
128+
129+
// Invoke writeQuery — this should detect id == -1, call finishFailure(),
130+
// and return WITHOUT writing anything to the channel.
131+
ctx.writeQuery(true);
132+
133+
// ---- Assertions ----
134+
135+
// 1. The promise must be completed and failed (correct behaviour both
136+
// before and after the fix).
137+
assertTrue(testPromise.isDone(),
138+
"Promise should be done after writeQuery with exhausted ID space");
139+
Throwable cause = testPromise.cause();
140+
assertInstanceOf(DnsNameResolverException.class, cause,
141+
"Promise should be failed with a DnsNameResolverException when ID space is exhausted");
142+
assertInstanceOf(IllegalStateException.class, cause.getCause(),
143+
"The root cause should be the IllegalStateException reporting the exhausted ID space");
144+
145+
// 2. No outbound message should have been written to the channel.
146+
// With the bug present the fall-through reaches sendQuery(), which
147+
// calls channel.writeAndFlush(query) with a bogus id (-1/65535),
148+
// so readOutbound() returns a non-null DatagramDnsQuery — the test
149+
// fails here on unfixed code, confirming the RED state.
150+
Object outbound = channel.readOutbound();
151+
// Release the outbound message regardless of outcome so that the
152+
// reference-counted DatagramDnsQuery (written by the buggy fall-through)
153+
// does not trigger a secondary memory-leak error that would obscure the
154+
// primary assertion failure.
155+
ReferenceCountUtil.release(outbound);
156+
assertNull(outbound,
157+
"No DNS query must be written to the channel when ID space is exhausted, " +
158+
"but found: " + outbound);
159+
} finally {
160+
channel.finishAndReleaseAll();
161+
}
162+
}
163+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.