diff --git a/java/src/org/openqa/selenium/grid/node/config/DriverServiceSessionFactory.java b/java/src/org/openqa/selenium/grid/node/config/DriverServiceSessionFactory.java index 211672f870bed..dbf5a16638da2 100644 --- a/java/src/org/openqa/selenium/grid/node/config/DriverServiceSessionFactory.java +++ b/java/src/org/openqa/selenium/grid/node/config/DriverServiceSessionFactory.java @@ -146,6 +146,7 @@ public Either apply(CreateSessionRequest sess capabilities = removeCapability(capabilities, "browserVersion"); } + HttpClient client = null; try { service.start(); @@ -154,7 +155,7 @@ public Either 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)); @@ -188,6 +189,7 @@ public Either apply(CreateSessionRequest sess caps = readVncEndpoint(capabilities, caps); span.addEvent("Driver service created session", attributeMap); + final HttpClient fClient = client; return Either.right( new DefaultActiveSession( tracer, @@ -201,8 +203,9 @@ public Either apply(CreateSessionRequest sess Instant.now()) { @Override public void stop() { - service.stop(); - client.close(); + try (fClient) { + service.stop(); + } } }); } catch (Exception e) { @@ -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) { diff --git a/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java b/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java index 9a681c1fdd434..48f567f576268 100644 --- a/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java +++ b/java/src/org/openqa/selenium/grid/node/docker/DockerSessionFactory.java @@ -197,6 +197,7 @@ public Either 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())); @@ -222,6 +223,7 @@ public Either 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)); } @@ -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) { @@ -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; diff --git a/java/src/org/openqa/selenium/grid/node/relay/RelaySessionFactory.java b/java/src/org/openqa/selenium/grid/node/relay/RelaySessionFactory.java index 86860ab0919e1..fc0c9849ece97 100644 --- a/java/src/org/openqa/selenium/grid/node/relay/RelaySessionFactory.java +++ b/java/src/org/openqa/selenium/grid/node/relay/RelaySessionFactory.java @@ -215,6 +215,7 @@ public Either 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) {