Skip to content

Commit

Permalink
[java] close the HttClient in case starting the session fails
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Jul 30, 2024
1 parent f4a86a4 commit 97d56d0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
capabilities = removeCapability(capabilities, "browserVersion");
}

HttpClient client = null;
try {
service.start();

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

ClientConfig clientConfig =
ClientConfig.defaultConfig().readTimeout(sessionTimeout).baseUrl(serviceURL);
HttpClient client = clientFactory.createClient(clientConfig);
client = clientFactory.createClient(clientConfig);

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

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

span.addEvent("Driver service created session", attributeMap);
final HttpClient fClient = client;
return Either.right(
new DefaultActiveSession(
tracer,
Expand All @@ -201,8 +203,9 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
Instant.now()) {
@Override
public void stop() {
service.stop();
client.close();
try (fClient) {
service.stop();
}
}
});
} catch (Exception e) {
Expand All @@ -217,7 +220,9 @@ public void stop() {

attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(), errorMessage);
span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);
service.stop();
try (final HttpClient fClient = client) {
service.stop();
}
return Either.left(new SessionNotCreatedException(errorMessage));
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
String.format(
"Unable to connect to docker server (container id: %s)", container.getId());
LOG.warning(message);
client.close();
return Either.left(new RetrySessionRequestException(message));
}
LOG.info(String.format("Server is ready (container id: %s)", container.getId()));
Expand All @@ -222,6 +223,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
container.stop(Duration.ofMinutes(1));
String message = "Unable to create session: " + e.getMessage();
LOG.log(Level.WARNING, message, e);
client.close();
return Either.left(new SessionNotCreatedException(message));
}

Expand Down Expand Up @@ -348,9 +350,10 @@ private Container startVideoContainer(
Container videoContainer = docker.create(containerConfig);
videoContainer.start();
String videoContainerIp = runningInDocker ? videoContainer.inspect().getIp() : "localhost";
URI videoContainerUrl = URI.create(String.format("http://%s:%s", videoContainerIp, videoPort));
HttpClient videoClient =
clientFactory.createClient(ClientConfig.defaultConfig().baseUri(videoContainerUrl));
try {
URL videoContainerUrl = new URL(String.format("http://%s:%s", videoContainerIp, videoPort));
HttpClient videoClient = clientFactory.createClient(videoContainerUrl);
LOG.fine(String.format("Waiting for video recording... (id: %s)", videoContainer.getId()));
waitForServerToStart(videoClient, Duration.ofMinutes(1));
} catch (Exception e) {
Expand All @@ -360,6 +363,8 @@ private Container startVideoContainer(
"Unable to verify video recording started (container id: %s), %s",
videoContainer.getId(), e.getMessage());
LOG.warning(message);
videoClient.close();
return null;
}
LOG.info(String.format("Video container started (id: %s)", videoContainer.getId()));
return videoContainer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
"Error while creating session with the service %s. %s", serviceUrl, e.getMessage());
attributeMap.put(EXCEPTION_MESSAGE.getKey(), errorMessage);
span.addEvent(EXCEPTION_EVENT.getKey(), attributeMap);
client.close();
return Either.left(new SessionNotCreatedException(errorMessage));
}
} catch (Exception e) {
Expand Down

0 comments on commit 97d56d0

Please sign in to comment.