Skip to content

Commit c82f92e

Browse files
committed
[java] release underlying HttpClient resources #15710
1 parent 932cf7f commit c82f92e

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

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

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.openqa.selenium.remote.http.HttpHandler;
5858
import org.openqa.selenium.remote.http.HttpRequest;
5959
import org.openqa.selenium.remote.http.HttpResponse;
60+
import org.openqa.selenium.remote.http.WebSocket;
6061
import org.openqa.selenium.remote.service.DriverService;
6162

6263
/**
@@ -94,12 +95,7 @@ public class RemoteWebDriverBuilder {
9495
private final List<Capabilities> requestedCapabilities = new ArrayList<>();
9596
private final Map<String, Object> additionalCapabilities = new TreeMap<>();
9697
private final Map<String, Object> metadata = new TreeMap<>();
97-
private Function<ClientConfig, HttpHandler> handlerFactory =
98-
config -> {
99-
HttpClient.Factory factory = HttpClient.Factory.createDefault();
100-
HttpClient client = factory.createClient(config);
101-
return client.with(new CloseHttpClientFilter(factory, client));
102-
};
98+
private HttpClient.Factory clientFactory = HttpClient.Factory.createDefault();
10399
private ClientConfig clientConfig = ClientConfig.defaultConfig();
104100
private URI remoteHost = null;
105101
private DriverService driverService;
@@ -285,7 +281,25 @@ public RemoteWebDriverBuilder withDriverService(DriverService service) {
285281
/** visible for testing only */
286282
RemoteWebDriverBuilder connectingWith(Function<ClientConfig, HttpHandler> handlerFactory) {
287283
Require.nonNull("Handler factory", handlerFactory);
288-
this.handlerFactory = handlerFactory;
284+
this.clientFactory =
285+
new HttpClient.Factory() {
286+
@Override
287+
public HttpClient createClient(ClientConfig config) {
288+
HttpHandler handler = handlerFactory.apply(config);
289+
290+
return new HttpClient() {
291+
@Override
292+
public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) {
293+
throw new UnsupportedOperationException("sockets are not supported");
294+
}
295+
296+
@Override
297+
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
298+
return handler.execute(req);
299+
}
300+
};
301+
}
302+
};
289303
return this;
290304
}
291305

@@ -380,26 +394,37 @@ private WebDriver getRemoteDriver() {
380394
driverClientConfig = driverClientConfig.authenticateAs(credentials);
381395
}
382396

383-
HttpHandler client = handlerFactory.apply(driverClientConfig);
397+
HttpClient client = clientFactory.createClient(driverClientConfig);
384398
HttpHandler handler =
385399
Require.nonNull("Http handler", client)
386400
.with(
387401
new AddWebDriverSpecHeaders()
388402
.andThen(new ErrorFilter())
389-
.andThen(new DumpHttpExchangeFilter()));
403+
.andThen(new DumpHttpExchangeFilter())
404+
.andThen(new CloseHttpClientFilter(clientFactory, client)));
390405

391406
Either<SessionNotCreatedException, ProtocolHandshake.Result> result;
392407
try {
393408
result = new ProtocolHandshake().createSession(handler, getPayload());
394409
} catch (IOException e) {
395-
throw new SessionNotCreatedException("Unable to create new remote session.", e);
410+
try (client) {
411+
throw new SessionNotCreatedException("Unable to create new remote session.", e);
412+
}
396413
}
397414

398415
if (result.isRight()) {
399-
CommandExecutor executor = result.map(res -> createExecutor(handler, res));
400-
return new RemoteWebDriver(executor, new ImmutableCapabilities());
416+
try {
417+
CommandExecutor executor = result.map(res -> createExecutor(handler, res));
418+
return new RemoteWebDriver(executor, new ImmutableCapabilities());
419+
} catch (Throwable t) {
420+
try (client) {
421+
throw t;
422+
}
423+
}
401424
} else {
402-
throw result.left();
425+
try (client) {
426+
throw result.left();
427+
}
403428
}
404429
}
405430

0 commit comments

Comments
 (0)