hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Oleg Kalnichevski (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (HTTPASYNC-86) Race condition in async client when a keep alive connection is reused in POST request method
Date Sun, 14 Dec 2014 13:20:13 GMT

    [ https://issues.apache.org/jira/browse/HTTPASYNC-86?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14245912#comment-14245912
] 

Oleg Kalnichevski commented on HTTPASYNC-86:
--------------------------------------------

I do not think this is a bug. ConnectionClosedException is perfectly legal and can be expected.
This problem is inherent in the HTTP protocol and the use of persistent connections. A persistent
connection can be terminated by any party (be it a user agent, an origin server or a proxy
of any sort) without any handshake at any point of time. It is entirely possible that a persistent
connection can become half-closed (slate) in a window of time between being released from
the pool and request execution (however short). There is absolutely nothing we can do about
it other than retrying the request with a different connection. HttpClient (blocking) can
also be affected by exactly the same race condition but it simply re-tried the request automatically
to compensate for the problem. HttpAsyncClient does not retry requests automatically at this
point. 

Oleg 

> Race condition in async client when a keep alive connection is reused in POST request
method
> --------------------------------------------------------------------------------------------
>
>                 Key: HTTPASYNC-86
>                 URL: https://issues.apache.org/jira/browse/HTTPASYNC-86
>             Project: HttpComponents HttpAsyncClient
>          Issue Type: Bug
>    Affects Versions: 4.0.2, 4.1-beta1
>         Environment: Ubuntu 12.04 64 bits Kernel 3.2.0-74
>            Reporter: Jose Dillet
>         Attachments: AsyncClientRaceConditionSample.java
>
>
> I have found a race condition in the async client when a POST request reuses a keep alive
connection. We have around 2%-4% of our runs in a Continuous Integration environment failing
because of this issue.
> I have found a consistent way of forcing the failure by using a debugger, so I can force
the events to happen in the order which uncovers the problem.
> I have tested it on httpasyncclient 4.0.2 / httpcore 4.3.2 and httpasyncclient 4.1-beta1
/ httpcore 4.4-beta1 and it happens in both versions (the lines provided in the steps to reproduce
are from version 4.0.2).
> How to reproduce:
> 1) Add these breakpoints:
> a - DefaultClientExchangeHandlerImpl:304
> b - DefaultClientExchangeHandlerImpl:168
> 2) Run AsyncClientRaceConditionSample sample program
> Processing first request:
> -  Program hits breakpoint a -> Resume
> -  Program hits breakpoint b -> Resume
> Processing second request:
> - Program hits breakpoints a and b (in different threads)
> - Resume a)
> - Resume b)
> (Probably adding a small sleep after DefaultClientExchangeHandlerImpl:303 only for the
testing would also uncover the issue).
> ConnectionClosedException is thrown with this stack trace:
> Exception in thread "main" java.util.concurrent.ExecutionException: org.apache.http.ConnectionClosedException:
Connection closed
> 	at org.apache.http.concurrent.BasicFuture.getResult(BasicFuture.java:68)
> 	at org.apache.http.concurrent.BasicFuture.get(BasicFuture.java:77)
> 	at AsyncClientRaceConditionSample.process(AsyncClientRaceConditionSample.java:81)
> 	at AsyncClientRaceConditionSample.main(AsyncClientRaceConditionSample.java:45)
> Caused by: org.apache.http.ConnectionClosedException: Connection closed
> 	at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.connectionAllocated(DefaultClientExchangeHandlerImpl.java:305)
> 	at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.access$000(DefaultClientExchangeHandlerImpl.java:63)
> 	at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl$1.completed(DefaultClientExchangeHandlerImpl.java:360)
> 	at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl$1.completed(DefaultClientExchangeHandlerImpl.java:357)
> 	at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:115)
> 	at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalPoolEntryCallback.completed(PoolingNHttpClientConnectionManager.java:465)
> 	at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalPoolEntryCallback.completed(PoolingNHttpClientConnectionManager.java:449)
> 	at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:115)
> 	at org.apache.http.nio.pool.AbstractNIOConnPool.fireCallbacks(AbstractNIOConnPool.java:443)
> 	at org.apache.http.nio.pool.AbstractNIOConnPool.lease(AbstractNIOConnPool.java:276)
> 	at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.requestConnection(PoolingNHttpClientConnectionManager.java:261)
> 	at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.requestConnection(DefaultClientExchangeHandlerImpl.java:351)
> 	at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.start(DefaultClientExchangeHandlerImpl.java:126)
> 	at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:129)
> 	at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:72)
> 	at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:102)
> 	at org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:87)
> 	at AsyncClientRaceConditionSample.process(AsyncClientRaceConditionSample.java:80)
> 	... 1 more
> Some analysis from my side:
> 1 - When the first request is processed, this is the execution order of relevant lines
(and the thread where it is processed):
> a - (I/O dispatcher 1) DefaultClientExchangeHandlerImpl<T>.connectionAllocated(NHttpClientConnection)
line: 303
> b - (I/O dispatcher 1) DefaultClientExchangeHandlerImpl<T>.connectionAllocated(NHttpClientConnection)
line: 304
> c - (I/O dispatcher 1) CPoolProxy.detach() line: 60
> d - (I/O dispatcher 1) DefaultClientExchangeHandlerImpl<T>.responseCompleted()
line: 168
> 2 - When the second request is processed c) might happen before b) which leads to the
problem:
> a - (main) DefaultClientExchangeHandlerImpl<T>.connectionAllocated(NHttpClientConnection)
line: 303
> c - (I/O dispatcher 1) CPoolProxy.detach() line: 60
> b - (main) DefaultClientExchangeHandlerImpl<T>.connectionAllocated(NHttpClientConnection)
line: 304
> e - (main) BasicFuture<T>.failed(Exception) line: 122
> d - (I/O dispatcher 1) DefaultClientExchangeHandlerImpl<T>.responseCompleted()
line: 168
> The problem is that in case 2 the connection can be closed before request is completed
because a) is processed in main thread and c) is processed in I/O dispatcher thread, so the
ordering b < c is not guaranteed.
> However in case 1 because all the events are processed in I/O dispatcher thread, the
ordering b < c is guaranteed (this seems to be true even if there are several dispatcher
threads).



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Mime
View raw message