Skip to content

Commit f0f8def

Browse files
committed
Update how the WebDriverException gathers system info
On OS X, a call to `InetAddress.getLocalHost()` is astonishly slow. Rather than pay that price every time, use some heuristics and a bit of luck to try and figure out the correct values.
1 parent 4b6a297 commit f0f8def

1 file changed

Lines changed: 87 additions & 15 deletions

File tree

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

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,99 @@
1919

2020
import org.openqa.selenium.internal.BuildInfo;
2121

22+
import java.io.BufferedReader;
23+
import java.io.IOException;
24+
import java.io.InputStreamReader;
2225
import java.net.InetAddress;
26+
import java.net.NetworkInterface;
27+
import java.net.SocketException;
2328
import java.net.UnknownHostException;
29+
import java.util.Enumeration;
2430
import java.util.HashMap;
2531
import java.util.Map;
32+
import java.util.concurrent.TimeUnit;
2633

2734
public class WebDriverException extends RuntimeException {
2835

2936
public static final String SESSION_ID = "Session ID";
3037
public static final String DRIVER_INFO = "Driver info";
3138
protected static final String BASE_SUPPORT_URL = "http://seleniumhq.org/exceptions/";
3239

40+
private final static String HOST_NAME;
41+
private final static String HOST_ADDRESS;
42+
3343
private Map<String, String> extraInfo = new HashMap<>();
3444

45+
static {
46+
// Ideally, we'd use InetAddress.getLocalHost, but this does a reverse DNS lookup. On Windows
47+
// and Linux this is apparently pretty fast, so we don't get random hangs. On OS X it's
48+
// amazingly slow. That's less than ideal. Figure things out and cache. We can't rely on
49+
// Platform since that depends on this class, but fortunately there's only one place we have to
50+
// worry about slow lookups.
51+
52+
String current = System.getProperty("os.name");
53+
String host = System.getenv("HOSTNAME"); // Most OSs
54+
if (host == null) {
55+
host = System.getenv("COMPUTERNAME"); // Windows
56+
}
57+
if (host == null && "Mac OS X".equals(current)) {
58+
try {
59+
Process process = Runtime.getRuntime().exec("hostname");
60+
61+
if (!process.waitFor(2, TimeUnit.SECONDS)) {
62+
process.destroyForcibly();
63+
}
64+
if (process.exitValue() == 0) {
65+
try (InputStreamReader isr = new InputStreamReader(process.getInputStream());
66+
BufferedReader reader = new BufferedReader(isr)) {
67+
host = reader.readLine();
68+
}
69+
}
70+
} catch (IOException ignored) {
71+
// Do nothing and fall through
72+
} catch (InterruptedException e) {
73+
Thread.currentThread().interrupt();
74+
throw new RuntimeException(e);
75+
}
76+
}
77+
if (host == null) {
78+
// Give up.
79+
try {
80+
host = InetAddress.getLocalHost().getHostName();
81+
} catch (UnknownHostException e) {
82+
host = "Unknown"; // At least we tried.
83+
}
84+
}
85+
86+
HOST_NAME = host;
87+
88+
String address = null;
89+
// Now for the IP address. We're going to do silly shenanigans on OS X only.
90+
if ("Mac OS X".equals(current)) {
91+
try {
92+
NetworkInterface en0 = NetworkInterface.getByName("en0");
93+
Enumeration<InetAddress> addresses = en0.getInetAddresses();
94+
while (addresses.hasMoreElements()) {
95+
InetAddress inetAddress = addresses.nextElement();
96+
address = inetAddress.getHostAddress();
97+
break;
98+
}
99+
} catch (SocketException e) {
100+
// Fall through and go the slow way.
101+
}
102+
}
103+
if (address == null) {
104+
// Alright. I give up.
105+
try {
106+
address = InetAddress.getLocalHost().getHostAddress();
107+
} catch (UnknownHostException e) {
108+
address = "Unknown";
109+
}
110+
}
111+
112+
HOST_ADDRESS = address;
113+
}
114+
35115
public WebDriverException() {
36116
super();
37117
}
@@ -66,21 +146,13 @@ private String createMessage(String originalMessageString) {
66146
}
67147

68148
public String getSystemInformation() {
69-
String host = "N/A";
70-
String ip = "N/A";
71-
72-
try{
73-
host = InetAddress.getLocalHost().getHostName();
74-
ip = InetAddress.getLocalHost().getHostAddress();
75-
} catch (UnknownHostException throw_away) {}
76-
77149
return String.format("System info: host: '%s', ip: '%s', os.name: '%s', os.arch: '%s', os.version: '%s', java.version: '%s'",
78-
host,
79-
ip,
80-
System.getProperty("os.name"),
81-
System.getProperty("os.arch"),
82-
System.getProperty("os.version"),
83-
System.getProperty("java.version"));
150+
HOST_NAME,
151+
HOST_ADDRESS,
152+
System.getProperty("os.name"),
153+
System.getProperty("os.arch"),
154+
System.getProperty("os.version"),
155+
System.getProperty("java.version"));
84156
}
85157

86158
public String getSupportUrl() {
@@ -108,7 +180,7 @@ public void addInfo(String key, String value) {
108180
}
109181

110182
public String getAdditionalInformation() {
111-
if (! extraInfo.containsKey(DRIVER_INFO)) {
183+
if (!extraInfo.containsKey(DRIVER_INFO)) {
112184
extraInfo.put(DRIVER_INFO, "driver.version: " + getDriverName(getStackTrace()));
113185
}
114186

0 commit comments

Comments
 (0)