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 — Wrap ECONNREFUSED in PortUnreachableException for UDP (#16131) · netty/netty@1f03ac7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • testsuite/src/main/java/io/netty/testsuite/transport/socket
    • DatagramConnectedWriteExceptionTest.java
  • transport-classes-epoll/src/main/java/io/netty/channel/epoll
    • EpollDatagramChannel.java
  • transport-classes-io_uring/src/main/java/io/netty/channel/uring
    • IoUringDatagramChannel.java
  • transport-classes-kqueue/src/main/java/io/netty/channel/kqueue
    • KQueueDatagramChannel.java
  • transport-native-io_uring/src/test/java/io/netty/channel/uring
    • IoUringDatagramConnectedWriteExceptionTest.java
    • IoUringSocketTestPermutation.java
  • transport-native-kqueue/src/test/java/io/netty/channel/kqueue
    • KQueueDatagramConnectedWriteExceptionTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramConnectedWriteExceptionTest.java‎

Copy file name to clipboard
+128Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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.testsuite.transport.socket;
17+
18+
import io.netty.bootstrap.Bootstrap;
19+
import io.netty.buffer.ByteBuf;
20+
import io.netty.buffer.Unpooled;
21+
import io.netty.channel.Channel;
22+
import io.netty.channel.ChannelHandlerContext;
23+
import io.netty.channel.ChannelOption;
24+
import io.netty.channel.SimpleChannelInboundHandler;
25+
import io.netty.channel.socket.DatagramPacket;
26+
import io.netty.testsuite.transport.TestsuitePermutation;
27+
import io.netty.util.CharsetUtil;
28+
import io.netty.util.NetUtil;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.TestInfo;
31+
import org.junit.jupiter.api.Timeout;
32+
import org.junit.jupiter.api.condition.DisabledOnOs;
33+
import org.junit.jupiter.api.condition.OS;
34+
35+
import java.net.InetSocketAddress;
36+
import java.net.PortUnreachableException;
37+
import java.util.List;
38+
import java.util.concurrent.CountDownLatch;
39+
import java.util.concurrent.TimeUnit;
40+
import java.util.concurrent.atomic.AtomicReference;
41+
42+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
43+
import static org.junit.jupiter.api.Assertions.assertNotNull;
44+
import static org.junit.jupiter.api.Assertions.assertTrue;
45+
46+
public class DatagramConnectedWriteExceptionTest extends AbstractClientSocketTest {
47+
48+
@Override
49+
protected List<TestsuitePermutation.BootstrapFactory<Bootstrap>> newFactories() {
50+
return SocketTestPermutation.INSTANCE.datagramSocket();
51+
}
52+
53+
@Test
54+
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
55+
@DisabledOnOs(OS.WINDOWS)
56+
public void testWriteThrowsPortUnreachableException(TestInfo testInfo) throws Throwable {
57+
run(testInfo, (Runner<Bootstrap>) this::testWriteExceptionAfterServerStop);
58+
}
59+
60+
protected void testWriteExceptionAfterServerStop(Bootstrap clientBootstrap) throws Throwable {
61+
CountDownLatch serverReceivedLatch = new CountDownLatch(1);
62+
Bootstrap serverBootstrap = clientBootstrap.clone()
63+
.option(ChannelOption.SO_BROADCAST, false)
64+
.handler(new SimpleChannelInboundHandler<DatagramPacket>() {
65+
66+
@Override
67+
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) {
68+
serverReceivedLatch.countDown();
69+
}
70+
});
71+
72+
Channel serverChannel = serverBootstrap.bind(new InetSocketAddress(NetUtil.LOCALHOST, 0)).sync().channel();
73+
InetSocketAddress serverAddress = (InetSocketAddress) serverChannel.localAddress();
74+
75+
clientBootstrap.option(ChannelOption.AUTO_READ, false)
76+
.handler(new SimpleChannelInboundHandler<DatagramPacket>() {
77+
78+
@Override
79+
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) {
80+
// no-op
81+
}
82+
});
83+
84+
Channel clientChannel = clientBootstrap.connect(serverAddress).sync().channel();
85+
86+
CountDownLatch clientFirstSendLatch = new CountDownLatch(1);
87+
try {
88+
ByteBuf firstMessage = Unpooled.wrappedBuffer("First message".getBytes(CharsetUtil.UTF_8));
89+
clientChannel.writeAndFlush(firstMessage)
90+
.addListener(future -> {
91+
if (future.isSuccess()) {
92+
clientFirstSendLatch.countDown();
93+
}
94+
});
95+
96+
assertTrue(serverReceivedLatch.await(5, TimeUnit.SECONDS), "Server should receive first message");
97+
assertTrue(clientFirstSendLatch.await(5, TimeUnit.SECONDS), "Client should send first message");
98+
99+
serverChannel.close().sync();
100+
101+
AtomicReference<Throwable> writeException = new AtomicReference<>();
102+
CountDownLatch writesCompleteLatch = new CountDownLatch(10);
103+
104+
for (int i = 0; i < 10; i++) {
105+
ByteBuf message = Unpooled.wrappedBuffer(("Message " + i).getBytes(CharsetUtil.UTF_8));
106+
clientChannel.writeAndFlush(message)
107+
.addListener(future -> {
108+
if (!future.isSuccess()) {
109+
writeException.compareAndSet(null, future.cause());
110+
}
111+
writesCompleteLatch.countDown();
112+
});
113+
Thread.sleep(50);
114+
}
115+
116+
assertTrue(writesCompleteLatch.await(5, TimeUnit.SECONDS), "All writes should complete");
117+
118+
assertNotNull(writeException.get(), "Should have captured a write exception");
119+
120+
assertInstanceOf(PortUnreachableException.class, writeException.get(), "Expected " +
121+
"PortUnreachableException but got: " + writeException.get().getClass().getName());
122+
} finally {
123+
if (clientChannel != null) {
124+
clientChannel.close().sync();
125+
}
126+
}
127+
}
128+
}
Collapse file

‎transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java‎

Copy file name to clipboardExpand all lines: transport-classes-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,14 @@ private boolean doWriteMessage(Object msg) throws Exception {
412412
return true;
413413
}
414414

415-
return doWriteOrSendBytes(data, remoteAddress, false) > 0;
415+
try {
416+
return doWriteOrSendBytes(data, remoteAddress, false) > 0;
417+
} catch (NativeIoException e) {
418+
if (remoteAddress == null) {
419+
throw translateForConnected(e);
420+
}
421+
throw e;
422+
}
416423
}
417424

418425
private static void checkUnresolved(SocketAddress address) {
Collapse file

‎transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUringDatagramChannel.java‎

Copy file name to clipboardExpand all lines: transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUringDatagramChannel.java
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,9 @@ private boolean removeFromOutboundBuffer(ChannelOutboundBuffer outboundBuffer, i
498498
try {
499499
return ioResult(errormsg, res) != 0;
500500
} catch (Throwable cause) {
501-
return outboundBuffer.remove(cause);
501+
Throwable e = (connected && cause instanceof NativeIoException) ?
502+
translateForConnected((NativeIoException) cause) : cause;
503+
return outboundBuffer.remove(e);
502504
}
503505
}
504506

Collapse file

‎transport-classes-kqueue/src/main/java/io/netty/channel/kqueue/KQueueDatagramChannel.java‎

Copy file name to clipboardExpand all lines: transport-classes-kqueue/src/main/java/io/netty/channel/kqueue/KQueueDatagramChannel.java
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,11 @@ private boolean doWriteMessage(Object msg) throws Exception {
296296
if (data.hasMemoryAddress()) {
297297
long memoryAddress = data.memoryAddress();
298298
if (remoteAddress == null) {
299-
writtenBytes = socket.writeAddress(memoryAddress, data.readerIndex(), data.writerIndex());
299+
try {
300+
writtenBytes = socket.writeAddress(memoryAddress, data.readerIndex(), data.writerIndex());
301+
} catch (Errors.NativeIoException e) {
302+
throw translateForConnected(e);
303+
}
300304
} else {
301305
if (socket.protocolFamily() == SocketProtocolFamily.UNIX) {
302306
DomainSocketAddress address = (DomainSocketAddress) remoteAddress;
@@ -347,6 +351,16 @@ private boolean doWriteMessage(Object msg) throws Exception {
347351
return writtenBytes > 0;
348352
}
349353

354+
private static IOException translateForConnected(Errors.NativeIoException e) {
355+
// We need to correctly translate connect errors to match NIO behaviour.
356+
if (e.expectedErr() == Errors.ERROR_ECONNREFUSED_NEGATIVE) {
357+
PortUnreachableException error = new PortUnreachableException(e.getMessage());
358+
error.initCause(e);
359+
return error;
360+
}
361+
return e;
362+
}
363+
350364
private static void checkUnresolved(SocketAddress address) {
351365
if (address instanceof InetSocketAddress
352366
&& (((InetSocketAddress) address).isUnresolved())) {
Collapse file

‎transport-native-io_uring/src/test/java/io/netty/channel/uring/IoUringDatagramConnectedWriteExceptionTest.java‎

Copy file name to clipboard
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.channel.uring;
17+
18+
import io.netty.bootstrap.Bootstrap;
19+
import io.netty.testsuite.transport.TestsuitePermutation;
20+
import io.netty.testsuite.transport.socket.DatagramConnectedWriteExceptionTest;
21+
22+
import java.util.List;
23+
24+
public class IoUringDatagramConnectedWriteExceptionTest extends DatagramConnectedWriteExceptionTest {
25+
26+
@Override
27+
protected List<TestsuitePermutation.BootstrapFactory<Bootstrap>> newFactories() {
28+
return IoUringSocketTestPermutation.INSTANCE.datagramSocket();
29+
}
30+
}
Collapse file

‎transport-native-io_uring/src/test/java/io/netty/channel/uring/IoUringSocketTestPermutation.java‎

Copy file name to clipboardExpand all lines: transport-native-io_uring/src/test/java/io/netty/channel/uring/IoUringSocketTestPermutation.java
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,18 @@ public Bootstrap newInstance() {
275275
);
276276
}
277277

278+
@Override
279+
public List<BootstrapFactory<Bootstrap>> datagramSocket() {
280+
return Collections.<BootstrapFactory<Bootstrap>>singletonList(
281+
new BootstrapFactory<Bootstrap>() {
282+
@Override
283+
public Bootstrap newInstance() {
284+
return new Bootstrap().group(IO_URING_GROUP).channel(IoUringDatagramChannel.class);
285+
}
286+
}
287+
);
288+
}
289+
278290
public static DomainSocketAddress newDomainSocketAddress() {
279291
return UnixTestUtils.newDomainSocketAddress();
280292
}
Collapse file

‎transport-native-kqueue/src/test/java/io/netty/channel/kqueue/KQueueDatagramConnectedWriteExceptionTest.java‎

Copy file name to clipboard
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.channel.kqueue;
17+
18+
import io.netty.bootstrap.Bootstrap;
19+
import io.netty.testsuite.transport.TestsuitePermutation;
20+
import io.netty.testsuite.transport.socket.DatagramConnectedWriteExceptionTest;
21+
22+
import java.util.List;
23+
24+
public class KQueueDatagramConnectedWriteExceptionTest extends DatagramConnectedWriteExceptionTest {
25+
26+
@Override
27+
protected List<TestsuitePermutation.BootstrapFactory<Bootstrap>> newFactories() {
28+
return KQueueSocketTestPermutation.INSTANCE.datagramSocket();
29+
}
30+
}

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.