Skip to content

Commit 97d56d0

Browse files
committed
[java] close the HttClient in case starting the session fails
1 parent f4a86a4 commit 97d56d0

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

java/src/org/openqa/selenium/grid/node/config/DriverServiceSessionFactory.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
146146
capabilities = removeCapability(capabilities, "browserVersion");
147147
}
148148

149+
HttpClient client = null;
149150
try {
150151
service.start();
151152

@@ -154,7 +155,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
154155

155156
ClientConfig clientConfig =
156157
ClientConfig.defaultConfig().readTimeout(sessionTimeout).baseUrl(serviceURL);
157-
HttpClient client = clientFactory.createClient(clientConfig);
158+
client = clientFactory.createClient(clientConfig);
158159

159160
Command command = new Command(null, DriverCommand.NEW_SESSION(capabilities));
160161

@@ -188,6 +189,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
188189
caps = readVncEndpoint(capabilities, caps);
189190

190191
span.addEvent("Driver service created session", attributeMap);
192+
final HttpClient fClient = client;
191193
return Either.right(
192194
new DefaultActiveSession(
193195
tracer,
@@ -201,8 +203,9 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
201203
Instant.now()) {
202204
@Override
203205
public void stop() {
204-
service.stop();
205-
client.close();
206+
try (fClient) {
207+
service.stop();
208+
}
206209
}
207210
});
208211
} catch (Exception e) {
@@ -217,7 +220,9 @@ public void stop() {
217220

218221
attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(), errorMessage);
219222
span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);
220-
service.stop();
223+
try (final HttpClient fClient = client) {
224+
service.stop();
225+
}
221226
return Either.left(new SessionNotCreatedException(errorMessage));
222227
}
223228
} catch (Exception e) {

java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
197197
String.format(
198198
"Unable to connect to docker server (container id: %s)", container.getId());
199199
LOG.warning(message);
200+
client.close();
200201
return Either.left(new RetrySessionRequestException(message));
201202
}
202203
LOG.info(String.format("Server is ready (container id: %s)", container.getId()));
@@ -222,6 +223,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
222223
container.stop(Duration.ofMinutes(1));
223224
String message = "Unable to create session: " + e.getMessage();
224225
LOG.log(Level.WARNING, message, e);
226+
client.close();
225227
return Either.left(new SessionNotCreatedException(message));
226228
}
227229

@@ -348,9 +350,10 @@ private Container startVideoContainer(
348350
Container videoContainer = docker.create(containerConfig);
349351
videoContainer.start();
350352
String videoContainerIp = runningInDocker ? videoContainer.inspect().getIp() : "localhost";
353+
URI videoContainerUrl = URI.create(String.format("http://%s:%s", videoContainerIp, videoPort));
354+
HttpClient videoClient =
355+
clientFactory.createClient(ClientConfig.defaultConfig().baseUri(videoContainerUrl));
351356
try {
352-
URL videoContainerUrl = new URL(String.format("http://%s:%s", videoContainerIp, videoPort));
353-
HttpClient videoClient = clientFactory.createClient(videoContainerUrl);
354357
LOG.fine(String.format("Waiting for video recording... (id: %s)", videoContainer.getId()));
355358
waitForServerToStart(videoClient, Duration.ofMinutes(1));
356359
} catch (Exception e) {
@@ -360,6 +363,8 @@ private Container startVideoContainer(
360363
"Unable to verify video recording started (container id: %s), %s",
361364
videoContainer.getId(), e.getMessage());
362365
LOG.warning(message);
366+
videoClient.close();
367+
return null;
363368
}
364369
LOG.info(String.format("Video container started (id: %s)", videoContainer.getId()));
365370
return videoContainer;

java/src/org/openqa/selenium/grid/node/relay/RelaySessionFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
215215
"Error while creating session with the service %s. %s", serviceUrl, e.getMessage());
216216
attributeMap.put(EXCEPTION_MESSAGE.getKey(), errorMessage);
217217
span.addEvent(EXCEPTION_EVENT.getKey(), attributeMap);
218+
client.close();
218219
return Either.left(new SessionNotCreatedException(errorMessage));
219220
}
220221
} catch (Exception e) {

0 commit comments

Comments
 (0)