Skip to content

Does the channel randomly drop packets? #778

@MikkoAbudo

Description

@MikkoAbudo

I found a problem, the packet was sent and returned success promise, but the server never received any packet about this.
Success:
02e3ca0f-433f-4f10-af9f-7af4178c81f2|1741440585811
true
ACTIVE
PREREAD
!!!
WROTE
!!!
WROTE
!!!
WROTE
!!!
WROTE
Fail:
234fd7ce-dcf6-42c6-acc0-76253471216c|1741440589633
true
ACTIVE
PREREAD
!!!
WROTE
Code1:
System.out.println(UUID.randomUUID() + "|" + System.currentTimeMillis());
System.out.println(ch.isActive());
ch.pipeline().addLast("hackfix",new ChannelInboundHandlerAdapter(){

                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                    System.out.println("PREREAD");
                    super.channelRead(ctx, msg);
                }

                public void channelActive(ChannelHandlerContext ctx) throws Exception {
                    ctx.read();
                    System.out.println("ACTIVE");
                    super.channelActive(ctx);
                }
            });
            handleProxyAdded(ch);

Code2:
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof ByteBuf) {
ByteBuf byteBuf = (ByteBuf) msg;
if (closed) {
Shared.rel(byteBuf);
return;
}

            if (channel == null) {
                // 添加空指针防护
                Queue<ByteBuf> localBuffers = this.buffers;
                if (localBuffers != null) {
                    System.out.println("CACHED");
                    localBuffers.offer(byteBuf);
                } else {
                    Shared.rel(byteBuf); // 直接释放
                }
                return;
            }

            System.out.println("!!!");
            channel.eventLoop().execute(() -> {
                if (channel.isOpen()) {
                    channel.writeAndFlush(msg).addListener(i -> {
                        System.out.println("WROTE");
                        if (!i.isSuccess()) {
                            Shared.rel(byteBuf); // 写入失败时释放
                            stopLogic(channel);
                            stopLogic(ctx.channel());
                            markClosed();
                        }
                    });//必须立马刷出, 否则会出现一些神奇的问题.
                } else {
                    Shared.rel(byteBuf);
                }
            });
        } else {
            super.channelRead(ctx, msg);
        }
        //不可能进入下一步retain
        //super.channelRead(ctx, msg);
    }

// thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions