Skip to content

Commit 11af789

Browse files
committed
Belt-and-braces closing of the http client from the remote webdriver builder
1 parent 1f73248 commit 11af789

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

java/client/src/org/openqa/selenium/remote/RemoteWebDriverBuilder.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,30 @@ public class RemoteWebDriverBuilder {
9797
private final List<Capabilities> requestedCapabilities = new ArrayList<>();
9898
private final Map<String, Object> additionalCapabilities = new TreeMap<>();
9999
private final Map<String, Object> metadata = new TreeMap<>();
100-
private Function<ClientConfig, HttpHandler> handlerFactory = config -> HttpClient.Factory.createDefault().createClient(config);
100+
private Function<ClientConfig, HttpHandler> handlerFactory =
101+
config -> {
102+
HttpClient.Factory factory = HttpClient.Factory.createDefault();
103+
HttpClient client = factory.createClient(config);
104+
return client.with(
105+
next -> req -> {
106+
try {
107+
return client.execute(req);
108+
} finally {
109+
if (req.getMethod() == DELETE) {
110+
HttpSessionId.getSessionId(req.getUri()).ifPresent(id -> {
111+
if (("/session/" + id).equals(req.getUri())) {
112+
try {
113+
client.close();
114+
} catch (IOException e) {
115+
LOG.log(WARNING, "Swallowing exception while closing http client", e);
116+
}
117+
factory.cleanupIdleClients();
118+
}
119+
});
120+
}
121+
}
122+
});
123+
};
101124
private ClientConfig clientConfig = ClientConfig.defaultConfig();
102125
private URI remoteHost = null;
103126
private DriverService driverService;

0 commit comments

Comments
 (0)