Skip to content

Commit fca76d1

Browse files
committed
8341637:java/net/Socket/UdpSocket.java fails with "java.net.BindException: Address already in use" (macos-aarch64)
1 parent 4ba170c commit fca76d1

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

test/jdk/java/net/Socket/UdpSocket.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -47,6 +47,8 @@
4747
@Test
4848
public class UdpSocket {
4949

50+
private static final int MAX_RETRIES = 3;
51+
5052
/**
5153
* Test using the Socket API to send/receive datagrams
5254
*/
@@ -133,16 +135,19 @@ public void testMaxSockets() throws Exception {
133135
}
134136

135137

136-
private Socket newUdpSocket() throws IOException {
137-
Socket s = null;
138-
139-
try {
140-
s = new Socket(InetAddress.getLoopbackAddress(), 8000, false);
141-
} catch (BindException unexpected) {
142-
System.out.println("BindException caught retry Socket creation");
143-
s = new Socket(InetAddress.getLoopbackAddress(), 8000, false);
138+
private Socket newUdpSocket() throws IOException, InterruptedException {
139+
BindException unexpected = null;
140+
for (int i=0; i < MAX_RETRIES; i++) {
141+
try {
142+
return new Socket(InetAddress.getLoopbackAddress(), 8000, false);
143+
} catch (BindException be) {
144+
unexpected = be;
145+
System.out.printf("BindException caught: retry Socket creation [%s/%s]%n",
146+
i+1, MAX_RETRIES);
147+
Thread.sleep(10 + 10 * i);
148+
}
144149
}
145-
return s;
150+
throw unexpected;
146151
}
147152

148153
private void closeAll(Deque<Socket> sockets) throws IOException {

0 commit comments

Comments
 (0)