Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.ClassCastException: io.netty.buffer.AdvancedLeakAwareByteBuf cannot be cast to io.netty.handler.codec.http.FullHttpRequest #9471

Closed
kimifdw opened this issue Aug 16, 2019 · 6 comments · Fixed by #9472
Milestone

Comments

@kimifdw
Copy link

kimifdw commented Aug 16, 2019

Expected behavior

java.lang.ClassCastException: io.netty.buffer.AdvancedLeakAwareByteBuf cannot be cast to io.netty.handler.codec.http.FullHttpRequest java.lang.ClassCastException: io.netty.buffer.AdvancedLeakAwareByteBuf cannot be cast to io.netty.handler.codec.http.FullHttpRequest at io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandshakeHandler.channelRead(WebSocketServerProtocolHandshakeHandler.java:72) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:438) at io.netty.handler.codec.ByteToMessageDecoder.handlerRemoved(ByteToMessageDecoder.java:256) at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.remove0(CombinedChannelDuplexHandler.java:610) at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.remove(CombinedChannelDuplexHandler.java:595) at io.netty.channel.CombinedChannelDuplexHandler.handlerRemoved(CombinedChannelDuplexHandler.java:183) at io.netty.channel.AbstractChannelHandlerContext.callHandlerRemoved(AbstractChannelHandlerContext.java:972) at io.netty.channel.DefaultChannelPipeline.callHandlerRemoved0(DefaultChannelPipeline.java:638) at io.netty.channel.DefaultChannelPipeline.remove(DefaultChannelPipeline.java:481) at io.netty.channel.DefaultChannelPipeline.remove(DefaultChannelPipeline.java:427) at io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker$1.operationComplete(WebSocketServerHandshaker.java:226) at io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker$1.operationComplete(WebSocketServerHandshaker.java:221) at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:500) at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:474) at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:413) at io.netty.util.concurrent.DefaultPromise.addListener(DefaultPromise.java:149) at io.netty.channel.DefaultChannelPromise.addListener(DefaultChannelPromise.java:95) at io.netty.channel.DefaultChannelPromise.addListener(DefaultChannelPromise.java:30) at io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker.handshake(WebSocketServerHandshaker.java:221) at io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker.handshake(WebSocketServerHandshaker.java:169) at io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandshakeHandler.channelRead(WebSocketServerProtocolHandshakeHandler.java:91) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:438) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:328) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:315) at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:429) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:283) at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:253) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:287) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1421) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:930) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:697) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:632) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:549) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:511) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:918) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at java.lang.Thread.run(Thread.java:748)

Netty version

4.1.36.Final

JVM version (e.g. java -version)

openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

OS version (e.g. uname -a)

Linux iZuf6dx8xehwvqrela3zz3Z 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

@normanmaurer
Copy link
Member

You will need to share more details...

@kimifdw
Copy link
Author

kimifdw commented Aug 16, 2019

@normanmaurer this is netty's config.when client connnect to the server,it will happen

netty config ChannelInitializer voiceChannelInitializerWebsocket = new ChannelInitializer() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ChannelPipeline channelPipeline = ch.pipeline();
                // 心跳处理【5秒客户端未返回是触发】
                channelPipeline
                        .addLast("idleStateHandler", new IdleStateHandler(commonProperties.getReadTimeout(), 0, 0, TimeUnit.SECONDS))
                        .addLast(new HttpServerCodec())
                        .addLast(new HttpObjectAggregator(65536))
                        .addLast("auxWebSocket", new WebSocketServerProtocolHandler(commonProperties.getWebsocketPath(), null, true, 65536))
                        .addLast(voiceDecoderWebSocket, voiceEncoderWebSocket)
                        .addLast(auxBusinessEvent, "auxHandler", auxVoiceHandlerAdapter);
            }
        };
        channelServerFactory.createNioChannel(commonProperties.getHost(), commonProperties.getWebsocketPort(), voiceChannelInitializerWebsocket);

/**
* create nioCHannnel
*
* @param host
* @param port
* @param nioChannel
*/
public void createNioChannel(String host, int port, ChannelInitializer nioChannel) {
if (initFactory(host, port, EventLoopType.EventLoopTypeNIO, nioChannel)) {
LOGGER.info("start plain socket server service OK");
} else {
LOGGER.warn("start plain socket server service failed");
}
}

/**
* init channel factory
*
* @param host
* @param port
* @param type
* @param initializer
* @return
*/
private boolean initFactory(String host, int port, EventLoopType type, ChannelInitializer initializer) {
ServerBootstrap b = new ServerBootstrap();
if (type == EventLoopType.EventLoopTypeEPOLL) {
bossGroup = NettyEventLoopUtil.newEventLoopGroup(true, 1, new NamedThreadFactory("aux-epoll-server-boss", false));
workerGroup = NettyEventLoopUtil.newEventLoopGroup(true, Runtime.getRuntime().availableProcessors() * 2, new NamedThreadFactory("aux-epoll-server-worker", true));
serverSocketChannelClass = EpollServerSocketChannel.class;
} else {
bossGroup = NettyEventLoopUtil.newEventLoopGroup(false, 1, new NamedThreadFactory("aux-nio-server-boss", false));
workerGroup = NettyEventLoopUtil.newEventLoopGroup(false, Runtime.getRuntime().availableProcessors() * 2, new NamedThreadFactory("aux-nio-server-worker", true));
serverSocketChannelClass = NioServerSocketChannel.class;
}

    if (workerGroup instanceof NioEventLoopGroup) {
        ((NioEventLoopGroup) workerGroup).setIoRatio(70);
    } else if (workerGroup instanceof EpollEventLoopGroup) {
        ((EpollEventLoopGroup) workerGroup).setIoRatio(70);
    }

    b.group(bossGroup, workerGroup).channel(serverSocketChannelClass).childHandler(initializer)
            .option(ChannelOption.SO_REUSEADDR, true)
            .option(ChannelOption.SO_BACKLOG, Integer.MAX_VALUE)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.SO_KEEPALIVE, true)
            .childOption(ChannelOption.TCP_NODELAY, true)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

    try {
        ChannelFuture cf = b.bind(host, port).sync().addListener((ChannelFutureListener) future -> {
            final SocketAddress localAddress = future.channel().localAddress();
            if (localAddress instanceof InetSocketAddress) {
                InetSocketAddress inetAddress = (InetSocketAddress) localAddress;
                LOGGER.info("host=[{}],port=[{}]", inetAddress.getHostString(), inetAddress.getPort());
            }
        });
        if (cf.isSuccess()) {
            return true;
        }
    } catch (InterruptedException e) {
        LOGGER.warn("");
    }
    return false;
}

@normanmaurer
Copy link
Member

Please show the full stacktrace.

@kimifdw
Copy link
Author

kimifdw commented Aug 16, 2019

@normanmaurer I have show the full stacktrace in my first commment.Please click on the arrow to find what you want
image

normanmaurer added a commit that referenced this issue Aug 16, 2019
…g the handshake

Motivation:

We need to ensure we replace WebSocketServerProtocolHandshakeHandler before doing the actual handshake as the handshake itself may complete directly and so forward pending bytes through the pipeline.

Modifications:

Replace the handler before doing the actual handshake.

Result:

Fixes #9471.
@normanmaurer
Copy link
Member

@kimifdw can you check if #9472 fixes the issue for you ?

@normanmaurer normanmaurer added this to the 4.1.40.Final milestone Aug 16, 2019
@kimifdw
Copy link
Author

kimifdw commented Aug 16, 2019

ok.I will try it,thanks

normanmaurer added a commit that referenced this issue Aug 17, 2019
…g the handshake (#9472)

Motivation:

We need to ensure we replace WebSocketServerProtocolHandshakeHandler before doing the actual handshake as the handshake itself may complete directly and so forward pending bytes through the pipeline.

Modifications:

Replace the handler before doing the actual handshake.

Result:

Fixes #9471.
normanmaurer added a commit that referenced this issue Aug 17, 2019
…g the handshake (#9472)

Motivation:

We need to ensure we replace WebSocketServerProtocolHandshakeHandler before doing the actual handshake as the handshake itself may complete directly and so forward pending bytes through the pipeline.

Modifications:

Replace the handler before doing the actual handshake.

Result:

Fixes #9471.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants