GeezerVison is showing github.com — Netty is throwing NoSuchElementException on 4.1.118.Final during AsyncHttpClient calls. · Issue #14839 · netty/netty
Netty is throwing NoSuchElementException on 4.1.118.Final during AsyncHttpClient calls. #14839DescriptionI am using https://github.com/AsyncHttpClient/async-http-client 3.0.1 which uses Netty 4.1.118.Final .
I can periodically see the following exception when the channel is flushed
Caused by: java.util.NoSuchElementException: null
at java.base/java.util.ArrayDeque.removeFirst(ArrayDeque.java:362)
at java.base/java.util.ArrayDeque.remove(ArrayDeque.java:523)
at io.netty.handler.stream.ChunkedWriteHandler.doFlush(ChunkedWriteHandler.java:287)
at io.netty.handler.stream.ChunkedWriteHandler.flush(ChunkedWriteHandler.java:146)
at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:937)
in IDEA I see this stack trace leading to this
doFlush:287, ChunkedWriteHandler (io.netty.handler.stream)
flush:146, ChunkedWriteHandler (io.netty.handler.stream)
invokeFlush0:937, AbstractChannelHandlerContext (io.netty.channel)
invokeWriteAndFlush:957, AbstractChannelHandlerContext (io.netty.channel)
write:982, AbstractChannelHandlerContext (io.netty.channel)
writeAndFlush:950, AbstractChannelHandlerContext (io.netty.channel)
writeAndFlush:969, DefaultChannelPipeline (io.netty.channel)
writeAndFlush:310, AbstractChannel (io.netty.channel)
write:87, NettyBodyBody (org.asynchttpclient.netty.request.body)
writeRequest:404, NettyRequestSender (org.asynchttpclient.netty.request)
writeRequest:78, NettyConnectListener (org.asynchttpclient.netty.channel)
onSuccess:150, NettyConnectListener (org.asynchttpclient.netty.channel)
onSuccess:91, NettyChannelConnector$1 (org.asynchttpclient.netty.channel)
operationComplete:28, SimpleChannelFutureListener (org.asynchttpclient.netty)
operationComplete:22, SimpleChannelFutureListener (org.asynchttpclient.netty)
notifyListener0:590, DefaultPromise (io.netty.util.concurrent)
notifyListeners0:583, DefaultPromise (io.netty.util.concurrent)
notifyListenersNow:559, DefaultPromise (io.netty.util.concurrent)
notifyListeners:492, DefaultPromise (io.netty.util.concurrent)
setValue0:636, DefaultPromise (io.netty.util.concurrent)
setSuccess0:625, DefaultPromise (io.netty.util.concurrent)
trySuccess:105, DefaultPromise (io.netty.util.concurrent)
trySuccess:84, DefaultChannelPromise (io.netty.channel)
fulfillConnectPromise:305, AbstractNioChannel$AbstractNioUnsafe (io.netty.channel.nio)
finishConnect:340, AbstractNioChannel$AbstractNioUnsafe (io.netty.channel.nio)
processSelectedKey:784, NioEventLoop (io.netty.channel.nio)
processSelectedKeysOptimized:732, NioEventLoop (io.netty.channel.nio)
processSelectedKeys:658, NioEventLoop (io.netty.channel.nio)
run:562, NioEventLoop (io.netty.channel.nio)
run:998, SingleThreadEventExecutor$4 (io.netty.util.concurrent)
run:74, ThreadExecutorMap$2 (io.netty.util.internal)
run:30, FastThreadLocalRunnable (io.netty.util.concurrent)
run:840, Thread (java.lang)
Debugging reveals that this code assumes that queue has a value when in fact it does not
if (endOfInput) {
// We need to remove the element from the queue before we call writeAndFlush() as this operation
// may cause an action that also touches the queue.
queue.remove();
}
it seems to need a if (! queue.isEmpty()) guard on it
Seems related to #11046 but not the same issue.
I tried to make a reproduction but could not - but in our in house application a certain class of interaction causes this every time.
I have also tried on 4.2.0-RC2 and I get the same problem. Activitynormanmaurer commented on Feb 19, 2025 I don't think the guard is the correct thing todo here... From a quick look it sounds more like something that is caused by reentrancy. Let me dig a bit deeper. bbakerman commented on Mar 9, 2025 I also suspect reentrancy - we have code paths here in our integration tests that work well and then kinda the same code not working and I think its about how the connections get closed and what events are firing and when yawkat commented on Mar 10, 2025 Which ChunkedInput implementation is triggering this? wuxiansen commented on Mar 13, 2025 When endOfInput is true, it means the entire data stream has been processed, and the queue might already be empty, so there's no need to handle it. Just wrap queue.remove() into a method like this:
private void removeIfNotEmpty() {
if (!queueIsEmpty()) {
queue.remove();
}
}
Replace queue.remove() with removeIfNotEmpty() in the doFlush method. This way, we only remove an item when the queue isn't empty.
@normanmaurer @bbakerman yawkat commented on Mar 17, 2025 While that would certainly solve this error, it would be nice to understand why the queue is empty in the first place, in case the reentrancy causes other issues. Every remove is preceded by a successful peek. normanmaurer commented on Mar 17, 2025 Yeah... I need to understand first why it happens, just adding a null check does not sound right bbakerman commented on Apr 10, 2025
While that would certainly solve this error, it would be nice to understand why the queue is empty in the first place, in case the reentrancy causes other issues. Every remove is preceded by a successful peek.
Sorry I cant make a simple reproduction - I tried and it never happens - but in our complex system (The Atlassian GraphQl gateway project) we can make it happen in our integration tests all the time - not on every request but on certain requests
We are blocked in getting to 3.x because of this bug. We have a VULN (on the cookie jar) problem calling us to go to 3.x normanmaurer commented on Nov 14, 2025 @bbakerman can I somehow run this integration tests ?
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|