Skip to content

Commit 9a5b98e

Browse files
authored
Fix classloading error in host identifier (#9931)
* #9784 avoid NoClassDefFoundError if host resolving failed just retry it when it's asked next time. * #9784 catch Errors too Unfortunately, I don't know what error happened there, but I guess we also need to catch it. * log the error when resolving host name/address * #9784 log InterruptedException I think this really happens sometimes, and causes NoClassDefFoundError in WebDriverException. Fixes #9784
1 parent 0f71ad7 commit 9a5b98e

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

java/src/org/openqa/selenium/WebDriverException.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ public class WebDriverException extends RuntimeException {
3232
public static final String DRIVER_INFO = "Driver info";
3333
protected static final String BASE_SUPPORT_URL = "https://selenium.dev/exceptions/";
3434

35-
private static final String HOST_NAME = HostIdentifier.getHostName();
36-
private static final String HOST_ADDRESS = HostIdentifier.getHostAddress();
37-
3835
private final Map<String, String> extraInfo = new ConcurrentHashMap<>();
3936

4037
public WebDriverException() {
@@ -72,7 +69,6 @@ public String getMessage() {
7269
* Returns the simple message string of this exception.
7370
*
7471
* @return the simple message string of this exception.
75-
*
7672
* @see #getMessage()
7773
*/
7874
public String getRawMessage() {
@@ -96,7 +92,7 @@ private String createMessage(String originalMessageString) {
9692
public String getSystemInformation() {
9793
return String.format(
9894
"System info: host: '%s', ip: '%s', os.name: '%s', os.arch: '%s', os.version: '%s', java.version: '%s'",
99-
HOST_NAME, HOST_ADDRESS,
95+
HostIdentifier.getHostName(), HostIdentifier.getHostAddress(),
10096
System.getProperty("os.name"), System.getProperty("os.arch"),
10197
System.getProperty("os.version"), System.getProperty("java.version"));
10298
}

java/src/org/openqa/selenium/net/HostIdentifier.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.openqa.selenium.net;
1919

20+
import static java.util.logging.Level.WARNING;
21+
2022
import org.openqa.selenium.Platform;
2123

2224
import java.io.BufferedReader;
@@ -26,12 +28,16 @@
2628
import java.nio.charset.Charset;
2729
import java.util.Enumeration;
2830
import java.util.concurrent.TimeUnit;
31+
import java.util.logging.Level;
32+
import java.util.logging.Logger;
2933

3034
public class HostIdentifier {
31-
private static final String HOST_NAME;
32-
private static final String HOST_ADDRESS;
35+
private static final Logger log = Logger.getLogger(HostIdentifier.class.getName());
36+
37+
private static volatile String hostName;
38+
private static volatile String hostAddress;
3339

34-
static {
40+
private static String resolveHostName() {
3541
// Ideally, we'd use InetAddress.getLocalHost, but this does a reverse DNS lookup. On Windows
3642
// and Linux this is apparently pretty fast, so we don't get random hangs. On OS X it's
3743
// amazingly slow. That's less than ideal. Figure things out and cache.
@@ -49,29 +55,35 @@ public class HostIdentifier {
4955
process.waitFor(2, TimeUnit.SECONDS);
5056
}
5157
if (process.exitValue() == 0) {
52-
try (InputStreamReader isr = new InputStreamReader(process.getInputStream(), Charset.defaultCharset());
58+
try (InputStreamReader isr = new InputStreamReader(process.getInputStream(),
59+
Charset.defaultCharset());
5360
BufferedReader reader = new BufferedReader(isr)) {
5461
host = reader.readLine();
5562
}
5663
}
5764
} catch (InterruptedException e) {
65+
log.log(WARNING, "Failed to resolve host name", e);
5866
Thread.currentThread().interrupt();
5967
throw new RuntimeException(e);
60-
} catch (Exception e) {
68+
} catch (Throwable e) {
6169
// fall through
70+
log.log(WARNING, "Failed to resolve host name", e);
6271
}
6372
}
6473
if (host == null) {
6574
// Give up.
6675
try {
6776
host = InetAddress.getLocalHost().getHostName();
68-
} catch (Exception e) {
77+
} catch (Throwable e) {
6978
host = "Unknown"; // At least we tried.
79+
log.log(WARNING, "Failed to resolve host name", e);
7080
}
7181
}
7282

73-
HOST_NAME = host;
83+
return host;
84+
}
7485

86+
private static String resolveHostAddress() {
7587
String address = null;
7688
// Now for the IP address. We're going to do silly shenanigans on OS X only.
7789
if (Platform.getCurrent().is(Platform.MAC)) {
@@ -81,27 +93,35 @@ public class HostIdentifier {
8193
if (addresses.hasMoreElements()) {
8294
address = addresses.nextElement().getHostAddress();
8395
}
84-
} catch (Exception e) {
96+
} catch (Throwable e) {
8597
// Fall through and go the slow way.
98+
log.log(WARNING, "Failed to resolve host address", e);
8699
}
87100
}
88101
if (address == null) {
89102
// Alright. I give up.
90103
try {
91104
address = InetAddress.getLocalHost().getHostAddress();
92-
} catch (Exception e) {
105+
} catch (Throwable e) {
93106
address = "Unknown";
107+
log.log(WARNING, "Failed to resolve host address", e);
94108
}
95109
}
96110

97-
HOST_ADDRESS = address;
111+
return address;
98112
}
99113

100-
public static String getHostName() {
101-
return HOST_NAME;
114+
public static synchronized String getHostName() {
115+
if (hostName == null) {
116+
hostName = resolveHostName();
117+
}
118+
return hostName;
102119
}
103120

104-
public static String getHostAddress() {
105-
return HOST_ADDRESS;
121+
public static synchronized String getHostAddress() {
122+
if (hostAddress == null) {
123+
hostAddress = resolveHostAddress();
124+
}
125+
return hostAddress;
106126
}
107127
}

0 commit comments

Comments
 (0)