|
17 | 17 | import static com.ning.http.client.providers.netty.util.HttpUtils.useProxyConnect;
|
18 | 18 | import static com.ning.http.util.AsyncHttpProviderUtils.getDefaultPort;
|
19 | 19 | import static com.ning.http.util.AsyncHttpProviderUtils.requestTimeout;
|
| 20 | +import static com.ning.http.util.AsyncHttpProviderUtils.REMOTELY_CLOSED_EXCEPTION; |
20 | 21 | import static com.ning.http.util.ProxyUtils.avoidProxy;
|
21 | 22 | import static com.ning.http.util.ProxyUtils.getProxyServer;
|
22 | 23 |
|
@@ -207,28 +208,35 @@ private <T> ListenableFuture<T> sendRequestWithCachedChannel(Request request, Ur
|
207 | 208 | future.attachChannel(channel, false);
|
208 | 209 |
|
209 | 210 | LOGGER.debug("Using cached Channel {}\n for request \n{}\n", channel, future.getNettyRequest().getHttpRequest());
|
210 |
| - Channels.setAttribute(channel, future); |
211 | 211 |
|
212 |
| - try { |
213 |
| - writeRequest(future, channel); |
214 |
| - } catch (Exception ex) { |
215 |
| - // write request isn't supposed to throw Exceptions |
216 |
| - LOGGER.debug("writeRequest failure", ex); |
217 |
| - if (ex.getMessage() != null && ex.getMessage().contains("SSLEngine")) { |
218 |
| - // FIXME what is this for? https://github.com/AsyncHttpClient/async-http-client/commit/a847c3d4523ccc09827743e15b17e6bab59c553b |
219 |
| - // can such an exception happen as we write async? |
220 |
| - LOGGER.debug("SSLEngine failure", ex); |
221 |
| - future = null; |
222 |
| - } else { |
223 |
| - try { |
224 |
| - asyncHandler.onThrowable(ex); |
225 |
| - } catch (Throwable t) { |
226 |
| - LOGGER.warn("doConnect.writeRequest()", t); |
| 212 | + if (Channels.isChannelValid(channel)) { |
| 213 | + Channels.setAttribute(channel, future); |
| 214 | + |
| 215 | + try { |
| 216 | + writeRequest(future, channel); |
| 217 | + } catch (Exception ex) { |
| 218 | + // write request isn't supposed to throw Exceptions |
| 219 | + LOGGER.debug("writeRequest failure", ex); |
| 220 | + if (ex.getMessage() != null && ex.getMessage().contains("SSLEngine")) { |
| 221 | + // FIXME what is this for? https://github.com/AsyncHttpClient/async-http-client/commit/a847c3d4523ccc09827743e15b17e6bab59c553b |
| 222 | + // can such an exception happen as we write async? |
| 223 | + LOGGER.debug("SSLEngine failure", ex); |
| 224 | + future = null; |
| 225 | + } else { |
| 226 | + try { |
| 227 | + asyncHandler.onThrowable(ex); |
| 228 | + } catch (Throwable t) { |
| 229 | + LOGGER.warn("doConnect.writeRequest()", t); |
| 230 | + } |
| 231 | + IOException ioe = new IOException(ex.getMessage()); |
| 232 | + ioe.initCause(ex); |
| 233 | + throw ioe; |
227 | 234 | }
|
228 |
| - IOException ioe = new IOException(ex.getMessage()); |
229 |
| - ioe.initCause(ex); |
230 |
| - throw ioe; |
231 | 235 | }
|
| 236 | + } else { |
| 237 | + // bad luck, the channel was closed in-between |
| 238 | + // there's a very good chance onClose was already notified but the future wasn't already registered |
| 239 | + handleUnexpectedClosedChannel(channel, future); |
232 | 240 | }
|
233 | 241 | return future;
|
234 | 242 | }
|
@@ -413,6 +421,14 @@ public void abort(Channel channel, NettyResponseFuture<?> future, Throwable t) {
|
413 | 421 | }
|
414 | 422 | }
|
415 | 423 |
|
| 424 | + public void handleUnexpectedClosedChannel(Channel channel, NettyResponseFuture<?> future) { |
| 425 | + if (future.isDone()) |
| 426 | + channelManager.closeChannel(channel); |
| 427 | + |
| 428 | + else if (!retry(future)) |
| 429 | + abort(channel, future, REMOTELY_CLOSED_EXCEPTION); |
| 430 | + } |
| 431 | + |
416 | 432 | public boolean retry(NettyResponseFuture<?> future) {
|
417 | 433 |
|
418 | 434 | if (isClosed())
|
|
0 commit comments