簡體   English   中英

Netty SslHandler引發IllegalReferenceCountException

[英]Netty SslHandler throws IllegalReferenceCountException

我正在使用Netty 4.0.36.Final編寫客戶端-服務器應用程序

測試工程師之一在客戶端遇到此問題:

io.netty.handler.codec.DecoderException: io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:418)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:245)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:278)
    at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:278)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:962)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:528)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:485)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:399)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:371)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
    at java.base/java.lang.Thread.run(Unknown Source)
Caused by: io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1
    at io.netty.buffer.AbstractReferenceCountedByteBuf.release(AbstractReferenceCountedByteBuf.java:101)
    at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1072)
    at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:904)
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:387)
    ... 15 common frames omitted

渠道管道:

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(sslContext.newHandler(ch.alloc()));
    pipeline.addLast(new AsnMessageDecoder());
    pipeline.addLast(new AsnMessageEncoder());
    pipeline.addLast(dataChannelHandler);
}

在某個時候,我向管道添加了另一個處理程序:

channel.pipeline().addFirst("idleStateHandler", new IdleStateHandler(180, 0, 0));

有兩個ChannelInboundHandler

public class AsnMessageDecoder extends ReplayingDecoder {
private static final Logger log = LoggerFactory.getLogger(AsnMessageDecoder.class);
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    DERCoder coder = Asn.getDERCoder();
    coder.disableEncoderDebugging();
    coder.disableDecoderDebugging();
    coder.enableEncoderConstraints();
    coder.enableDecoderConstraints();
    coder.enableAutomaticEncoding();
    coder.enableAutomaticDecoding();
    coder.enableContainedValueEncoding();
    coder.enableContainedValueDecoding();
    try (ByteBufInputStream is = new ByteBufInputStream(in)) {
        out.add(coder.decode(is, new Message()));
    } catch (Exception e) {
        log.error("Failed to decode message", e);
    }
}

還有一個:

abstract class BaseChannelHandler<T extends ChannelContext> extends SimpleChannelInboundHandler<Message> {
private final Logger log = LoggerFactory.getLogger(getClass());

private final T channelContext;
private final ClientConfiguration clientConfiguration;
private final Writer writer;
private final ClientService service;

BaseChannelHandler(T channelContext,
                   ClientConfiguration clientConfiguration,
                   Writer writer,
                   ClientService service) {
    this.channelContext = channelContext;
    this.clientConfiguration = clientConfiguration;
    this.writer = writer;
    this.service = service;
}

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    log.debug("{} channelRegistered", getClass().getSimpleName());
}

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    log.debug("{} channelActive", getClass().getSimpleName());
    channelContext.connectState();
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
    log.debug("{} channelRead0 msg = {}", getClass().getSimpleName(), msg);
    channelContext.handleMessage(msg);
    if (clientConfiguration.isWritingOutputEnabled()) {
        writer.writeXml(msg);
    }
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    log.error(getClass().getSimpleName() + " exceptionCaught", cause);
    ctx.close();
    service.stop();
}

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    log.debug("{} channelInactive", getClass().getSimpleName());
}

@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
    log.debug("{} channelUnregistered", getClass().getSimpleName());
}

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        ctx.disconnect();
    }
}

我無法重現問題。 在我的本地計算機上,它工作正常。 看着wirweshark並沒有給我任何提示。 看起來像正常斷開連接。 我不太確定下一步該如何定位該問題。 您能給我一些解決該問題的建議嗎?

因此,這似乎是netty版本中的一個錯誤(該版本很舊)。 升級到4.1.24.Final修復了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM