Skip to content

Commit 28070d2

Browse files
asolntsevpujaganidiemol
authored
#9784 use shutdown() instead of shutdownNow() (#10063)
* #9784 use `shutdown()` instead of `shutdownNow()` it waits for completion of already started tasks. Otherwise it may causes errors in those tasks, including succeeding NoClassDefFoundError. * #9784 shutdown gracefully the executor service Co-authored-by: Puja Jagani <[email protected]> Co-authored-by: Diego Molina <[email protected]>
1 parent d7ae19b commit 28070d2

6 files changed

Lines changed: 48 additions & 12 deletions

File tree

java/src/org/openqa/selenium/concurrent/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ java_library(
55
srcs = glob(["*.java"]),
66
visibility = [
77
"//java/src/org/openqa/selenium/grid:__subpackages__",
8+
"//java/src/org/openqa/selenium/remote:__subpackages__"
89
],
910
deps = [
1011
"//java/src/org/openqa/selenium:core",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.openqa.selenium.concurrent;
2+
3+
import static java.util.concurrent.TimeUnit.SECONDS;
4+
import static java.util.logging.Level.WARNING;
5+
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.logging.Logger;
8+
9+
public class ExecutorServices {
10+
11+
private static final Logger LOG = Logger.getLogger(ExecutorServices.class.getName());
12+
13+
public static void shutdownGracefully(String name, ExecutorService service) {
14+
service.shutdown();
15+
try {
16+
if (!service.awaitTermination(5, SECONDS)) {
17+
LOG.warning(String.format("Failed to shutdown %s", name));
18+
service.shutdownNow();
19+
}
20+
} catch (InterruptedException e) {
21+
Thread.currentThread().interrupt();
22+
LOG.log(WARNING, String.format("Failed to shutdown %s", name), e);
23+
service.shutdownNow();
24+
}
25+
}
26+
}

java/src/org/openqa/selenium/grid/sessionqueue/local/LocalNewSessionQueue.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
44
import static java.util.concurrent.TimeUnit.MILLISECONDS;
5+
import static org.openqa.selenium.concurrent.ExecutorServices.shutdownGracefully;
56

67
import com.google.common.annotations.VisibleForTesting;
78

@@ -85,6 +86,7 @@
8586
description = "New session queue")
8687
public class LocalNewSessionQueue extends NewSessionQueue implements Closeable {
8788

89+
private static final String NAME = "Local New Session Queue";
8890
private final EventBus bus;
8991
private final SlotMatcher slotMatcher;
9092
private final Duration requestTimeout;
@@ -95,7 +97,7 @@ public class LocalNewSessionQueue extends NewSessionQueue implements Closeable {
9597
private final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(r -> {
9698
Thread thread = new Thread(r);
9799
thread.setDaemon(true);
98-
thread.setName("Local New Session Queue");
100+
thread.setName(NAME);
99101
return thread;
100102
});
101103

@@ -407,7 +409,7 @@ public boolean isReady() {
407409

408410
@Override
409411
public void close() throws IOException {
410-
service.shutdownNow();
412+
shutdownGracefully(NAME, service);
411413
}
412414

413415
private void failDueToTimeout(RequestId reqId) {
@@ -418,7 +420,7 @@ private class Data {
418420
public final Instant endTime;
419421
public Either<SessionNotCreatedException, CreateSessionResponse> result;
420422
private boolean complete;
421-
private CountDownLatch latch = new CountDownLatch(1);
423+
private final CountDownLatch latch = new CountDownLatch(1);
422424

423425
public Data(Instant enqueued) {
424426
this.endTime = enqueued.plus(requestTimeout);

java/src/org/openqa/selenium/remote/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ java_library(
4848
],
4949
deps = [
5050
"//java/src/org/openqa/selenium:core",
51+
"//java/src/org/openqa/selenium/concurrent",
5152
"//java/src/org/openqa/selenium/devtools",
5253
"//java/src/org/openqa/selenium/io",
5354
"//java/src/org/openqa/selenium/json",

java/src/org/openqa/selenium/remote/service/DriverCommandExecutor.java

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

1818
package org.openqa.selenium.remote.service;
1919

20+
import static org.openqa.selenium.concurrent.ExecutorServices.shutdownGracefully;
21+
2022
import com.google.common.annotations.VisibleForTesting;
2123
import com.google.common.base.Throwables;
2224

@@ -46,10 +48,11 @@
4648
*/
4749
public class DriverCommandExecutor extends HttpCommandExecutor implements Closeable {
4850

51+
private static final String NAME = "Driver Command Executor";
4952
private final DriverService service;
5053
private final ExecutorService executorService = Executors.newFixedThreadPool(2, r -> {
5154
Thread thread = new Thread(r);
52-
thread.setName("Driver Command Executor");
55+
thread.setName(NAME);
5356
thread.setDaemon(true);
5457
return thread;
5558
});
@@ -131,7 +134,7 @@ public Response execute(Command command) throws IOException {
131134
Thread.currentThread().interrupt();
132135
throw new WebDriverException("Timed out waiting for driver server to stop.", e);
133136
} finally {
134-
executorService.shutdownNow();
137+
close();
135138
}
136139

137140
} else {
@@ -166,6 +169,6 @@ Response invokeExecute(Command command) throws IOException {
166169

167170
@Override
168171
public void close() {
169-
executorService.shutdownNow();
172+
shutdownGracefully(NAME, executorService);
170173
}
171174
}

java/src/org/openqa/selenium/remote/service/DriverService.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
package org.openqa.selenium.remote.service;
1919

20+
import static java.util.Collections.emptyMap;
21+
import static java.util.concurrent.TimeUnit.SECONDS;
22+
import static org.openqa.selenium.concurrent.ExecutorServices.shutdownGracefully;
23+
2024
import com.google.common.collect.ImmutableMap;
2125

2226
import org.openqa.selenium.Beta;
@@ -46,9 +50,6 @@
4650
import java.util.concurrent.TimeoutException;
4751
import java.util.concurrent.locks.ReentrantLock;
4852

49-
import static java.util.Collections.emptyMap;
50-
import static java.util.concurrent.TimeUnit.SECONDS;
51-
5253
/**
5354
* Manages the life and death of a native executable driver server.
5455
*
@@ -60,10 +61,12 @@
6061
*/
6162
public class DriverService implements Closeable {
6263

64+
private static final String NAME = "Driver Service Executor";
6365
protected static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(20);
66+
6467
private final ExecutorService executorService = Executors.newFixedThreadPool(2, r -> {
6568
Thread thread = new Thread(r);
66-
thread.setName("Driver Service Executor");
69+
thread.setName(NAME);
6770
thread.setDaemon(true);
6871
return thread;
6972
});
@@ -286,7 +289,7 @@ public void stop() {
286289
} finally {
287290
process = null;
288291
lock.unlock();
289-
executorService.shutdownNow();
292+
close();
290293
}
291294

292295
if (toThrow != null) {
@@ -308,7 +311,7 @@ protected OutputStream getOutputStream() {
308311

309312
@Override
310313
public void close() {
311-
executorService.shutdownNow();
314+
shutdownGracefully(NAME, executorService);
312315
}
313316

314317
private enum StartOrDie {

0 commit comments

Comments
 (0)