Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
4.2.3
-
None
-
HttpAsyncClient 4.0-beta3
Description
An unhandled java.nio.channels.CancelledKeyException in AbstractIOReactor#processNewChannels() leads to a shutdown of the underlying IOReactor.
Here's the stacktrace:
org.apache.http.nio.reactor.IOReactorException: I/O dispatch worker terminated abnormally at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:382) at org.apache.http.impl.nio.conn.PoolingClientAsyncConnectionManager.execute(PoolingClientAsyncConnectionManager.java:108) at org.apache.http.impl.nio.client.AbstractHttpAsyncClient.doExecute(AbstractHttpAsyncClient.java:464) at org.apache.http.impl.nio.client.AbstractHttpAsyncClient.access$000(AbstractHttpAsyncClient.java:101) at org.apache.http.impl.nio.client.AbstractHttpAsyncClient$1.run(AbstractHttpAsyncClient.java:485) Caused by: java.nio.channels.CancelledKeyException at sun.nio.ch.SelectionKeyImpl.ensureValid(SelectionKeyImpl.java:73) at sun.nio.ch.SelectionKeyImpl.interestOps(SelectionKeyImpl.java:77) at org.apache.http.impl.nio.reactor.IOSessionImpl.<init>(IOSessionImpl.java:94) at org.apache.http.impl.nio.reactor.AbstractIOReactor.processNewChannels(AbstractIOReactor.java:406) at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:291) at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:106) at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:604) at java.lang.Thread.run(Thread.java:722)
This happens regularly under non-reproducible circumstances.
The particular line of code where the exception is not handled is AbstractIOReactor.java:406. The exception is thrown in the constructor of IOSessionImpl.java:94 in order of a canceled SelectionKey.
As a result of this unhandled exception, the AbstractMultiworkerIOReactor throws a IOReactorException in AbstractMultiworkerIOReactor.java:382 and is finally being shutdown.
Since this component is used by HttpAsyncClient, after this exception the instance of HttpAsyncClient is no longer usable.
The following patch works for us (AbstractIOReactor.java:406):
IOSession session = null; try { session = new IOSessionImpl(key, interestOpsCallback, sessionClosedCallback); } catch (CancelledKeyException ex) { key.attach(null); return; }