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

NettyServerHandler类的一个不恰当的对btyebuf的引用释放 #2

Open
zzzczh opened this issue May 17, 2021 · 2 comments
Open

Comments

@zzzczh
Copy link

zzzczh commented May 17, 2021

NettyServerHandler类的channelRead0最后会对msg进行引用释放,这是不正确的。如下打印一下方法返回值可见是false。跟进方法源码会发现因为类型都不对,会直接返回false。

@Override
 protected void channelRead0(ChannelHandlerContext ctx, RpcRequest msg) throws Exception {
     try {
         if(msg.getHeartBeat()) {
             logger.info("接收到客户端心跳包...");
             return;
         }
         logger.info("服务器接收到请求: {}", msg);
         Object result = requestHandler.handle(msg);
         if (ctx.channel().isActive() && ctx.channel().isWritable()) {
             ctx.writeAndFlush(RpcResponse.success(result, msg.getRequestId()));
         } else {
             logger.error("通道不可写");
         }
     } finally {
         boolean isok = ReferenceCountUtil.release(msg);
         logger.info(String.valueOf(isok));
     }
 }

正确的做法应该是在上一个入站handler即最后使用btyebuf的地方进行释放

@zzzczh
Copy link
Author

zzzczh commented May 25, 2021

补充:查看上一个入站handler继承的replaying解码器发现自动释放了bytebuf,所以正确的改法应该是去掉这句释放代码即可。

@Poison02
Copy link

你好,我是初学者,SimpleChannelInboundHandler 这个类本来就会自动释放的吧?原文中finally代码块最后是将引用计数释放了,应该不会有什么问题吧?没有恶意,只是想学习一下!😁

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

No branches or pull requests

2 participants