Skip to content

Commit 69afd91

Browse files
committed
[grid] prevent UrlChecker leaking threads on timeout
1 parent 4f3e972 commit 69afd91

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.openqa.selenium.net;
1919

2020
import static java.util.concurrent.TimeUnit.MILLISECONDS;
21-
import static java.util.concurrent.TimeUnit.NANOSECONDS;
2221

2322
import java.io.IOException;
2423
import java.net.HttpURLConnection;
@@ -55,7 +54,7 @@ public class UrlChecker {
5554

5655
public void waitUntilAvailable(long timeout, TimeUnit unit, final URL... urls)
5756
throws TimeoutException {
58-
long start = System.nanoTime();
57+
long start = System.currentTimeMillis();
5958
LOG.fine("Waiting for " + Arrays.toString(urls));
6059
try {
6160
Future<Void> callback =
@@ -64,10 +63,7 @@ public void waitUntilAvailable(long timeout, TimeUnit unit, final URL... urls)
6463
HttpURLConnection connection = null;
6564

6665
long sleepMillis = MIN_POLL_INTERVAL_MS;
67-
while (true) {
68-
if (Thread.interrupted()) {
69-
throw new InterruptedException();
70-
}
66+
while (!Thread.interrupted()) {
7167
for (URL url : urls) {
7268
try {
7369
LOG.fine("Polling " + url);
@@ -87,13 +83,19 @@ public void waitUntilAvailable(long timeout, TimeUnit unit, final URL... urls)
8783
sleepMillis =
8884
(sleepMillis >= MAX_POLL_INTERVAL_MS) ? sleepMillis : sleepMillis * 2;
8985
}
86+
throw new InterruptedException();
9087
});
91-
callback.get(timeout, unit);
88+
try {
89+
callback.get(timeout, unit);
90+
} finally {
91+
// if already completed cancel is ignored
92+
callback.cancel(true);
93+
}
9294
} catch (java.util.concurrent.TimeoutException e) {
9395
throw new TimeoutException(
9496
String.format(
9597
"Timed out waiting for %s to be available after %d ms",
96-
Arrays.toString(urls), MILLISECONDS.convert(System.nanoTime() - start, NANOSECONDS)),
98+
Arrays.toString(urls), System.currentTimeMillis() - start),
9799
e);
98100
} catch (InterruptedException e) {
99101
Thread.currentThread().interrupt();
@@ -105,7 +107,7 @@ public void waitUntilAvailable(long timeout, TimeUnit unit, final URL... urls)
105107

106108
public void waitUntilUnavailable(long timeout, TimeUnit unit, final URL url)
107109
throws TimeoutException {
108-
long start = System.nanoTime();
110+
long start = System.currentTimeMillis();
109111
LOG.fine("Waiting for " + url);
110112
try {
111113
Future<Void> callback =
@@ -114,7 +116,7 @@ public void waitUntilUnavailable(long timeout, TimeUnit unit, final URL url)
114116
HttpURLConnection connection = null;
115117

116118
long sleepMillis = MIN_POLL_INTERVAL_MS;
117-
while (true) {
119+
while (!Thread.interrupted()) {
118120
try {
119121
LOG.fine("Polling " + url);
120122
connection = connectToUrl(url);
@@ -133,13 +135,19 @@ public void waitUntilUnavailable(long timeout, TimeUnit unit, final URL url)
133135
sleepMillis =
134136
(sleepMillis >= MAX_POLL_INTERVAL_MS) ? sleepMillis : sleepMillis * 2;
135137
}
138+
throw new InterruptedException();
136139
});
137-
callback.get(timeout, unit);
140+
try {
141+
callback.get(timeout, unit);
142+
} finally {
143+
// if already completed cancel is ignored
144+
callback.cancel(true);
145+
}
138146
} catch (java.util.concurrent.TimeoutException e) {
139147
throw new TimeoutException(
140148
String.format(
141149
"Timed out waiting for %s to become unavailable after %d ms",
142-
url, MILLISECONDS.convert(System.nanoTime() - start, NANOSECONDS)),
150+
url, System.currentTimeMillis() - start),
143151
e);
144152
} catch (InterruptedException | ExecutionException e) {
145153
throw new RuntimeException(e);

0 commit comments

Comments
 (0)