Skip to content

Commit 18bddf9

Browse files
CustomConnectHandler: properly close
1 parent 54bb6a4 commit 18bddf9

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

src/main/java/com/testingbot/tunnel/proxy/CustomConnectHandler.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ protected void connectToServer(HttpServletRequest request, String host, int port
9494

9595
private void connectToProxy(HttpServletRequest request, String host, int port, Promise<SocketChannel> promise) {
9696
SocketChannel channel = null;
97+
Selector selector = null;
9798
try {
9899
channel = SocketChannel.open();
99100
channel.socket().setTcpNoDelay(true);
100101
channel.configureBlocking(false);
101102
channel.connect(newConnectAddress(proxyHost, proxyPort));
102103

103-
final Selector selector = Selector.open();
104+
selector = Selector.open();
104105
channel.register(selector, SelectionKey.OP_CONNECT);
105106
while (selector.select() > 0) {
106107
final Set<SelectionKey> keys = selector.selectedKeys();
@@ -154,12 +155,7 @@ private void connectToProxy(HttpServletRequest request, String host, int port, P
154155
proxyHost, proxyPort, host, port);
155156
}
156157

157-
try {
158-
selector.close();
159-
} catch (final IOException e) {
160-
LOG.error("Error closing selector: {}", e.getMessage(), e);
161-
}
162-
158+
selector.close();
163159
promise.succeeded(channel);
164160
return;
165161
}
@@ -175,6 +171,13 @@ private void connectToProxy(HttpServletRequest request, String host, int port, P
175171
LOG.error("Error closing channel after failure: {}", t.getMessage(), t);
176172
}
177173
}
174+
if (selector != null) {
175+
try {
176+
selector.close();
177+
} catch (IOException t) {
178+
LOG.error("Error closing selector after failure: {}", t.getMessage(), t);
179+
}
180+
}
178181
promise.failed(x);
179182
}
180183
}

0 commit comments

Comments
 (0)