Skip to content

Commit dba2671

Browse files
cgamachebarancev
authored andcommitted
Adding close connection support to HttpClient to combat hanging open files
Added a .close() method to org.openqa.selenium.remote.http.HttpClient interface. Added a call to the client's .close() method from the HttpCommandExecutor class when a org.openqa.selenium.remote.DriverCommand.QUIT is sent. Implemented the .close() method in org.openqa.selenium.remote.internal.ApacheHttpClient which taps the connectionManager and closes idle connections. Fixes SeleniumHQ#1080 Signed-off-by: Alexei Barantsev <[email protected]>
1 parent c09fa0e commit dba2671

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ public Response execute(Command command) throws IOException {
147147
String sessionId = HttpSessionId.getSessionId(httpResponse.getTargetHost());
148148
response.setSessionId(sessionId);
149149
}
150+
if (QUIT.equals(command.getName())) {
151+
client.close();
152+
}
150153
return response;
151154
} catch (UnsupportedCommandException e) {
152155
if (e.getMessage() == null || "".equals(e.getMessage())) {

java/client/src/org/openqa/selenium/remote/http/HttpClient.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ public interface HttpClient {
3434
* @throws IOException if an I/O error occurs.
3535
*/
3636
HttpResponse execute(HttpRequest request, boolean followRedirects) throws IOException;
37-
37+
3838
/**
39-
* Creates HttpClient instances.
40-
*/
39+
* Closes the connections associated with this client.
40+
*
41+
* @throws IOException if an I/O error occurs.
42+
*/
43+
void close() throws IOException;
44+
4145
interface Factory {
4246

4347
/**

java/client/src/org/openqa/selenium/remote/internal/ApacheHttpClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.net.URI;
4949
import java.net.URISyntaxException;
5050
import java.net.URL;
51+
import java.util.concurrent.TimeUnit;
5152

5253
public class ApacheHttpClient implements org.openqa.selenium.remote.http.HttpClient {
5354

@@ -253,4 +254,10 @@ private static synchronized HttpClientFactory getDefaultHttpClientFactory() {
253254
return defaultClientFactory;
254255
}
255256
}
257+
258+
@Override
259+
public void close() throws IOException {
260+
client.getConnectionManager().closeIdleConnections(0, TimeUnit.SECONDS);
261+
}
262+
256263
}

0 commit comments

Comments
 (0)