Skip to content

Commit

Permalink
[grid] close the httpclient after connecting the websocket failed
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Aug 18, 2024
1 parent 5bac479 commit 7612405
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
36 changes: 21 additions & 15 deletions java/src/org/openqa/selenium/grid/node/ProxyNodeWebsockets.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,24 +226,30 @@ private Consumer<Message> createWsEndPoint(
LOG.info("Establishing connection to " + uri);

HttpClient client = clientFactory.createClient(ClientConfig.defaultConfig().baseUri(uri));
WebSocket upstream =
client.openSocket(
new HttpRequest(GET, uri.toString()),
new ForwardingListener(downstream, sessionConsumer, sessionId));
try {
WebSocket upstream =
client.openSocket(
new HttpRequest(GET, uri.toString()),
new ForwardingListener(downstream, sessionConsumer, sessionId));

return (msg) -> {
try {
upstream.send(msg);
} finally {
if (msg instanceof CloseMessage) {
try {
client.close();
} catch (Exception e) {
LOG.log(Level.WARNING, "Failed to shutdown the client of " + uri, e);
return (msg) -> {
try {
upstream.send(msg);
} finally {
if (msg instanceof CloseMessage) {
try {
client.close();
} catch (Exception e) {
LOG.log(Level.WARNING, "Failed to shutdown the client of " + uri, e);
}
}
}
}
};
};
} catch (Exception e) {
LOG.log(Level.WARNING, "Connecting to upstream websocket failed", e);
client.close();
throw e;
}
}

private static class ForwardingListener implements WebSocket.Listener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,29 @@ public Optional<Consumer<Message>> apply(String uri, Consumer<Message> downstrea

HttpClient client =
clientFactory.createClient(ClientConfig.defaultConfig().baseUri(sessionUri));
WebSocket upstream =
client.openSocket(new HttpRequest(GET, uri), new ForwardingListener(downstream));

return Optional.of(
(msg) -> {
try {
upstream.send(msg);
} finally {
if (msg instanceof CloseMessage) {
try {
client.close();
} catch (Exception e) {
LOG.log(Level.WARNING, "Failed to shutdown the client of " + sessionUri, e);
try {
WebSocket upstream =
client.openSocket(new HttpRequest(GET, uri), new ForwardingListener(downstream));

return Optional.of(
(msg) -> {
try {
upstream.send(msg);
} finally {
if (msg instanceof CloseMessage) {
try {
client.close();
} catch (Exception e) {
LOG.log(Level.WARNING, "Failed to shutdown the client of " + sessionUri, e);
}
}
}
}
});

});
} catch (Exception e) {
LOG.log(Level.WARNING, "Connecting to upstream websocket failed", e);
client.close();
return Optional.empty();
}
} catch (NoSuchSessionException e) {
LOG.warning("Attempt to connect to non-existent session: " + uri);
return Optional.empty();
Expand Down

0 comments on commit 7612405

Please sign in to comment.