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) (#16150) · netty/netty@496f55c

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

418-
return doWriteOrSendBytes(data, remoteAddress, false) > 0;
418+
try {
419+
return doWriteOrSendBytes(data, remoteAddress, false) > 0;
420+
} catch (NativeIoException e) {
421+
if (remoteAddress == null) {
422+
throw translateForConnected(e);
423+
}
424+
throw e;
425+
}
419426
}
420427

421428
private static void checkUnresolved(AddressedEnvelope<?, ?> envelope) {
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
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import io.netty.util.internal.ObjectUtil;
3535
import io.netty.util.internal.StringUtil;
3636

37+
import java.io.IOException;
3738
import java.net.InetAddress;
3839
import java.net.InetSocketAddress;
3940
import java.net.NetworkInterface;
@@ -265,7 +266,11 @@ protected boolean doWriteMessage(Object msg) throws Exception {
265266
if (data.hasMemoryAddress()) {
266267
long memoryAddress = data.memoryAddress();
267268
if (remoteAddress == null) {
268-
writtenBytes = socket.writeAddress(memoryAddress, data.readerIndex(), data.writerIndex());
269+
try {
270+
writtenBytes = socket.writeAddress(memoryAddress, data.readerIndex(), data.writerIndex());
271+
} catch (Errors.NativeIoException e) {
272+
throw translateForConnected(e);
273+
}
269274
} else {
270275
writtenBytes = socket.sendToAddress(memoryAddress, data.readerIndex(), data.writerIndex(),
271276
remoteAddress.getAddress(), remoteAddress.getPort());
@@ -295,6 +300,16 @@ protected boolean doWriteMessage(Object msg) throws Exception {
295300
return writtenBytes > 0;
296301
}
297302

303+
private static IOException translateForConnected(Errors.NativeIoException e) {
304+
// We need to correctly translate connect errors to match NIO behaviour.
305+
if (e.expectedErr() == Errors.ERROR_ECONNREFUSED_NEGATIVE) {
306+
PortUnreachableException error = new PortUnreachableException(e.getMessage());
307+
error.initCause(e);
308+
return error;
309+
}
310+
return e;
311+
}
312+
298313
private static void checkUnresolved(AddressedEnvelope<?, ?> envelope) {
299314
if (envelope.recipient() instanceof InetSocketAddress
300315
&& (((InetSocketAddress) envelope.recipient()).isUnresolved())) {
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.