Skip to content

Commit 7612405

Browse files
committed
[grid] close the httpclient after connecting the websocket failed
1 parent 5bac479 commit 7612405

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

java/src/org/openqa/selenium/grid/node/ProxyNodeWebsockets.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -226,24 +226,30 @@ private Consumer<Message> createWsEndPoint(
226226
LOG.info("Establishing connection to " + uri);
227227

228228
HttpClient client = clientFactory.createClient(ClientConfig.defaultConfig().baseUri(uri));
229-
WebSocket upstream =
230-
client.openSocket(
231-
new HttpRequest(GET, uri.toString()),
232-
new ForwardingListener(downstream, sessionConsumer, sessionId));
229+
try {
230+
WebSocket upstream =
231+
client.openSocket(
232+
new HttpRequest(GET, uri.toString()),
233+
new ForwardingListener(downstream, sessionConsumer, sessionId));
233234

234-
return (msg) -> {
235-
try {
236-
upstream.send(msg);
237-
} finally {
238-
if (msg instanceof CloseMessage) {
239-
try {
240-
client.close();
241-
} catch (Exception e) {
242-
LOG.log(Level.WARNING, "Failed to shutdown the client of " + uri, e);
235+
return (msg) -> {
236+
try {
237+
upstream.send(msg);
238+
} finally {
239+
if (msg instanceof CloseMessage) {
240+
try {
241+
client.close();
242+
} catch (Exception e) {
243+
LOG.log(Level.WARNING, "Failed to shutdown the client of " + uri, e);
244+
}
243245
}
244246
}
245-
}
246-
};
247+
};
248+
} catch (Exception e) {
249+
LOG.log(Level.WARNING, "Connecting to upstream websocket failed", e);
250+
client.close();
251+
throw e;
252+
}
247253
}
248254

249255
private static class ForwardingListener implements WebSocket.Listener {

java/src/org/openqa/selenium/grid/router/ProxyWebsocketsIntoGrid.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,29 @@ public Optional<Consumer<Message>> apply(String uri, Consumer<Message> downstrea
6868

6969
HttpClient client =
7070
clientFactory.createClient(ClientConfig.defaultConfig().baseUri(sessionUri));
71-
WebSocket upstream =
72-
client.openSocket(new HttpRequest(GET, uri), new ForwardingListener(downstream));
73-
74-
return Optional.of(
75-
(msg) -> {
76-
try {
77-
upstream.send(msg);
78-
} finally {
79-
if (msg instanceof CloseMessage) {
80-
try {
81-
client.close();
82-
} catch (Exception e) {
83-
LOG.log(Level.WARNING, "Failed to shutdown the client of " + sessionUri, e);
71+
try {
72+
WebSocket upstream =
73+
client.openSocket(new HttpRequest(GET, uri), new ForwardingListener(downstream));
74+
75+
return Optional.of(
76+
(msg) -> {
77+
try {
78+
upstream.send(msg);
79+
} finally {
80+
if (msg instanceof CloseMessage) {
81+
try {
82+
client.close();
83+
} catch (Exception e) {
84+
LOG.log(Level.WARNING, "Failed to shutdown the client of " + sessionUri, e);
85+
}
8486
}
8587
}
86-
}
87-
});
88-
88+
});
89+
} catch (Exception e) {
90+
LOG.log(Level.WARNING, "Connecting to upstream websocket failed", e);
91+
client.close();
92+
return Optional.empty();
93+
}
8994
} catch (NoSuchSessionException e) {
9095
LOG.warning("Attempt to connect to non-existent session: " + uri);
9196
return Optional.empty();

0 commit comments

Comments
 (0)