@@ -315,14 +315,17 @@ protected final <T> void writeRequest(final Channel channel,
315315final NettyResponseFuture <T >future ,
316316final HttpRequest nettyRequest ) {
317317try {
318+ /**
319+ * If the channel is dead because it was pooled and the remote server decided to close it,
320+ * we need to try to recover in order to prevent failing a valid request.
321+ */
318322if (!channel .isOpen () || !channel .isConnected ()) {
319323if (!remotelyClosed (channel ,future )) {
320324abort (future ,new ConnectException ());
321- return ;
322325 }else {
323326log .debug ("Request {} has been recovered" ,nettyRequest );
324- return ;
325327 }
328+ return ;
326329 }
327330
328331Body body =null ;
@@ -344,7 +347,6 @@ protected final <T> void writeRequest(final Channel channel,
344347 }
345348 }
346349
347-
348350if (TransferCompletionHandler .class .isAssignableFrom (future .getAsyncHandler ().getClass ())) {
349351
350352FluentCaseInsensitiveStringsMap h =new FluentCaseInsensitiveStringsMap ();
@@ -1629,6 +1631,10 @@ public void operationComplete(ChannelFuture cf) {
16291631 }
16301632future .touch ();
16311633
1634+ /**
1635+ * We need to make sure we aren't in the middle of an authorization process before publishing events
1636+ * as we will re-publish again the same event after the authorization, causing unpredictable behavior.
1637+ */
16321638Realm realm =future .getRequest ().getRealm () !=null ?future .getRequest ().getRealm () :NettyAsyncHttpProvider .this .getConfig ().getRealm ();
16331639boolean startPublishing =future .isInAuth ()
16341640 ||realm ==null