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 5.0: Fix flaky DatagramUnicastIPv6MappedTest on macOS (#17981) · netty/netty@681592a

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
+62-5Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,79 @@
2020
import io.netty.util.NetUtil;
2121
import io.netty.util.internal.PlatformDependent;
2222

23-
import java.net.Inet4Address;
23+
import java.io.IOException;
24+
import java.io.UncheckedIOException;
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

2932
public class DatagramUnicastIPv6MappedTest extends DatagramUnicastIPv6Test {
3033

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

4198
@Override
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
+45-51Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ public abstract class DatagramUnicastTest extends AbstractDatagramTest {
7070
private static final byte[] BAD_PREFIX = "BAD-PREFIX-".getBytes(StandardCharsets.UTF_8);
7171
private static final byte[] EXPECTED = "EXPECTED-direct-reader-index".getBytes(StandardCharsets.UTF_8);
7272
private static final byte[] BYTES = {0, 1, 2, 3};
73-
// UDP delivery is best-effort even on loopback, so allow a few resends before failing the test.
74-
private static final int SEND_ATTEMPTS = 3;
7573
protected enum WrapType {
7674
NONE, DUP, SLICE, READ_ONLY
7775
}
@@ -237,23 +235,31 @@ protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throw
237235
}
238236
}).bind(newSocketAddress()).get();
239237

240-
SocketAddress address = server.localAddress();
241-
DatagramSocket client;
242238
try {
243-
client = new DatagramSocket(newSocketAddress());
244-
} catch (IllegalArgumentException e) {
245-
assumeThat(e.getMessage()).doesNotContainIgnoringCase("unsupported address type");
246-
throw e;
247-
}
248-
SocketAddress sendAddress = address instanceof InetSocketAddress ?
249-
convertAnyAddress((InetSocketAddress) address) : address;
250-
for (int i = 0; i < 100; i++) {
239+
SocketAddress address = server.localAddress();
240+
DatagramSocket client;
241+
try {
242+
client = new DatagramSocket(newSocketAddress());
243+
} catch (IllegalArgumentException e) {
244+
assumeThat(e.getMessage()).doesNotContainIgnoringCase("unsupported address type");
245+
throw e;
246+
}
251247
try {
252-
client.send(new java.net.DatagramPacket(EmptyArrays.EMPTY_BYTES, 0, sendAddress));
253-
} catch (BindException e) {
254-
throw new TestAbortedException("JDK sockets do not support binding to these addresses.", e);
248+
SocketAddress sendAddress = address instanceof InetSocketAddress ?
249+
convertAnyAddress((InetSocketAddress) address) : address;
250+
for (int i = 0; i < 100; i++) {
251+
try {
252+
client.send(new java.net.DatagramPacket(EmptyArrays.EMPTY_BYTES, 0, sendAddress));
253+
} catch (BindException e) {
254+
throw new TestAbortedException("JDK sockets do not support binding to these addresses.", e);
255+
}
256+
semaphore.acquire();
257+
}
258+
} finally {
259+
client.close();
255260
}
256-
semaphore.acquire();
261+
} finally {
262+
closeChannel(server);
257263
}
258264
}
259265

@@ -344,22 +350,17 @@ public void channelRead0(ChannelHandlerContext ctx, Object msgs) {
344350
SocketAddress localAddr = sc.localAddress();
345351
SocketAddress addr = localAddr instanceof InetSocketAddress ?
346352
convertAnyAddress((InetSocketAddress) sc.localAddress()) : localAddr;
347-
boolean received = false;
348-
for (int attempt = 1; attempt <= SEND_ATTEMPTS && !received; attempt++) {
349-
List<Future<Void>> futures = new ArrayList<>(count);
350-
for (int i = 0; i < count; i++) {
351-
futures.add(write(cc, buf, addr, wrapType));
352-
}
353-
// release as we used buf.retain() before
354-
cc.flush();
353+
List<Future<Void>> futures = new ArrayList<>(count);
354+
for (int i = 0; i < count; i++) {
355+
futures.add(write(cc, buf, addr, wrapType));
356+
}
357+
// release as we used buf.retain() before
358+
cc.flush();
355359

356-
for (Future<Void> future: futures) {
357-
future.sync();
358-
}
359-
// UDP datagrams can be lost even on loopback, so retry a few times before giving up.
360-
received = latch.await(10, TimeUnit.SECONDS);
360+
for (Future<Void> future: futures) {
361+
future.sync();
361362
}
362-
if (!received) {
363+
if (!latch.await(10, TimeUnit.SECONDS)) {
363364
Throwable error = errorRef.get();
364365
if (error != null) {
365366
throw error;
@@ -404,30 +405,23 @@ private void testSimpleSendWithConnect0(Bootstrap sb, Bootstrap cb, ByteBuf buf,
404405
cc.connect(addr).syncUninterruptibly();
405406

406407
List<Future<Void>> futures = new ArrayList<>();
407-
boolean received = false;
408-
for (int attempt = 1; attempt <= SEND_ATTEMPTS && !received; attempt++) {
409-
futures.clear();
410-
for (int i = 0; i < count; i++) {
411-
futures.add(write(cc, buf, wrapType));
412-
}
413-
cc.flush();
414-
415-
for (Future<Void> future: futures) {
416-
future.sync();
417-
}
408+
for (int i = 0; i < count; i++) {
409+
futures.add(write(cc, buf, wrapType));
410+
}
411+
cc.flush();
418412

419-
// UDP datagrams (including the echo) can be lost even on loopback, so retry a few times
420-
// before giving up.
421-
received = latch.await(10, TimeUnit.SECONDS) && clientLatch.await(10, TimeUnit.SECONDS);
413+
for (Future<Void> future: futures) {
414+
future.sync();
422415
}
423-
if (!received) {
424-
if (latch.getCount() != 0) {
425-
Throwable cause = errorRef.get();
426-
if (cause != null) {
427-
throw cause;
428-
}
429-
fail();
416+
417+
if (!latch.await(10, TimeUnit.SECONDS)) {
418+
Throwable cause = errorRef.get();
419+
if (cause != null) {
420+
throw cause;
430421
}
422+
fail();
423+
}
424+
if (!clientLatch.await(10, TimeUnit.SECONDS)) {
431425
Throwable cause = clientErrorRef.get();
432426
if (cause != null) {
433427
throw cause;

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.