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 — Port 4.1: Fix flaky DatagramUnicastIPv6MappedTest on macOS (#17981) (… · netty/netty@bc83ebd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • testsuite/src/main/java/io/netty/testsuite/transport/socket
    • DatagramUnicastIPv6MappedTest.java
    • DatagramUnicastTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastIPv6MappedTest.java
+82-5Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,99 @@
1919
import io.netty.channel.socket.nio.NioDatagramChannel;
2020
import io.netty.util.NetUtil;
2121
import io.netty.util.internal.PlatformDependent;
22+
import io.netty.util.internal.SuppressJava6Requirement;
2223

23-
import java.net.Inet4Address;
24+
import java.io.IOException;
25+
import java.net.BindException;
2426
import java.net.InetAddress;
2527
import java.net.InetSocketAddress;
2628
import java.net.SocketAddress;
29+
import java.net.StandardProtocolFamily;
2730
import java.net.UnknownHostException;
2831

32+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
33+
2934
public class DatagramUnicastIPv6MappedTest extends DatagramUnicastIPv6Test {
3035

36+
private static final InetAddress IPV4_ANY;
37+
private static final int MAX_PORT_SELECTION_ATTEMPTS = 10;
38+
39+
static {
40+
try {
41+
IPV4_ANY = InetAddress.getByAddress(new byte[] {0, 0, 0, 0});
42+
} catch (UnknownHostException e) {
43+
throw new ExceptionInInitializerError(e);
44+
}
45+
}
46+
3147
@Override
3248
protected SocketAddress newSocketAddress() {
3349
// Explicit use IPv4 wildcard for bind while the channel is using SocketProtocolFamily.INET6.
34-
try {
35-
return new InetSocketAddress(InetAddress.getByAddress(new byte[] {0, 0 , 0, 0}), 0);
36-
} catch (UnknownHostException e) {
37-
throw new IllegalStateException(e);
50+
//
51+
// Don't use port 0: when an IPv6 socket binds to the wildcard address, macOS only checks the ports of other
52+
// IPv6 sockets when it selects the ephemeral port, so it might select a port that an IPv4 socket is
53+
// bound to. Datagrams that are sent to the IPv4 loopback address are then delivered to the IPv4 socket and
54+
// never reach the channel, no matter how often they are resent:
55+
//
56+
// - in6_pcbsetport() checks if the port is used with in6_pcblookup_local()...
57+
// https://github.com/apple-oss-distributions/xnu/blob/xnu-12377.121.6/bsd/netinet6/in6_src.c#L1779-L1780
58+
// - ...which skips all sockets that are not IPv6 sockets.
59+
// https://github.com/apple-oss-distributions/xnu/blob/xnu-12377.121.6/bsd/netinet6/in6_pcb.c#L1297-L1301
60+
// - The netns_reserve_in() check that follows only fails if Skywalk reserved the port, not BSD sockets.
61+
// https://github.com/apple-oss-distributions/xnu/blob/xnu-12377.121.6/bsd/skywalk/namespace/netns.c#L774-L783
62+
// - When an IPv4 datagram is received, in_pcblookup_hash_locked() prefers IPv4 sockets bound to the
63+
// destination or the wildcard address over IPv6 sockets bound to the wildcard address.
64+
// https://github.com/apple-oss-distributions/xnu/blob/xnu-12377.121.6/bsd/netinet/in_pcb.c#L2634-L2666
65+
return new InetSocketAddress(IPV4_ANY, unusedPort());
66+
}
67+
68+
/**
69+
* Returns a port that is neither used by an IPv4 nor by an IPv6 socket.
70+
*/
71+
@SuppressJava6Requirement(reason = "Guarded by java version check")
72+
private static int unusedPort() {
73+
assumeTrue(PlatformDependent.javaVersion() >= 7);
74+
BindException cause = null;
75+
for (int i = 0; i < MAX_PORT_SELECTION_ATTEMPTS; i++) {
76+
int port;
77+
// The IPv4 stack selects a port that is unused by IPv4 sockets and by IPv6 sockets that accept IPv4
78+
// datagrams.
79+
java.nio.channels.DatagramChannel ipv4 = null;
80+
try {
81+
ipv4 = java.nio.channels.DatagramChannel.open(StandardProtocolFamily.INET);
82+
ipv4.bind(new InetSocketAddress(IPV4_ANY, 0));
83+
port = ((InetSocketAddress) ipv4.getLocalAddress()).getPort();
84+
} catch (IOException e) {
85+
throw new IllegalStateException(e);
86+
} finally {
87+
closeQuietly(ipv4);
88+
}
89+
// Bind to the port like the channel will do, which fails if an IPv6 socket that only accepts IPv6
90+
// datagrams uses it. The IPv4 channel must be closed by now, as this binds to the IPv4 wildcard address
91+
// as well on some platforms (like Linux).
92+
java.nio.channels.DatagramChannel ipv6 = null;
93+
try {
94+
ipv6 = java.nio.channels.DatagramChannel.open(StandardProtocolFamily.INET6);
95+
ipv6.bind(new InetSocketAddress(IPV4_ANY, port));
96+
return port;
97+
} catch (BindException e) {
98+
cause = e;
99+
} catch (IOException e) {
100+
throw new IllegalStateException(e);
101+
} finally {
102+
closeQuietly(ipv6);
103+
}
104+
}
105+
throw new IllegalStateException("Failed to find a port that is unused by IPv4 and IPv6 sockets", cause);
106+
}
107+
108+
private static void closeQuietly(java.nio.channels.DatagramChannel channel) {
109+
if (channel != null) {
110+
try {
111+
channel.close();
112+
} catch (IOException ignore) {
113+
// ignore
114+
}
38115
}
39116
}
40117

Collapse file

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

Copy file name to clipboardExpand all lines: testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java
+22-14Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -227,23 +227,31 @@ protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throw
227227
}
228228
}).bind(newSocketAddress()).sync().channel();
229229

230-
SocketAddress address = server.localAddress();
231-
DatagramSocket client;
232230
try {
233-
client = new DatagramSocket(newSocketAddress());
234-
} catch (IllegalArgumentException e) {
235-
assumeThat(e.getMessage()).doesNotContainIgnoringCase("unsupported address type");
236-
throw e;
237-
}
238-
SocketAddress sendAddress = address instanceof InetSocketAddress ?
239-
sendToAddress((InetSocketAddress) address) : address;
240-
for (int i = 0; i < 100; i++) {
231+
SocketAddress address = server.localAddress();
232+
DatagramSocket client;
233+
try {
234+
client = new DatagramSocket(newSocketAddress());
235+
} catch (IllegalArgumentException e) {
236+
assumeThat(e.getMessage()).doesNotContainIgnoringCase("unsupported address type");
237+
throw e;
238+
}
241239
try {
242-
client.send(new java.net.DatagramPacket(EmptyArrays.EMPTY_BYTES, 0, sendAddress));
243-
} catch (BindException e) {
244-
throw new TestAbortedException("JDK sockets do not support binding to these addresses.", e);
240+
SocketAddress sendAddress = address instanceof InetSocketAddress ?
241+
sendToAddress((InetSocketAddress) address) : address;
242+
for (int i = 0; i < 100; i++) {
243+
try {
244+
client.send(new java.net.DatagramPacket(EmptyArrays.EMPTY_BYTES, 0, sendAddress));
245+
} catch (BindException e) {
246+
throw new TestAbortedException("JDK sockets do not support binding to these addresses.", e);
247+
}
248+
semaphore.acquire();
249+
}
250+
} finally {
251+
client.close();
245252
}
246-
semaphore.acquire();
253+
} finally {
254+
closeChannel(server);
247255
}
248256
}
249257

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.