Skip to content

Issue on Netty4: client cannot reconnect #591

Closed

Description

@fredericBregier

I try to get into it, and of course, while I am trying to convert Netty3 based code to Netty4, I try also the examples and check some functionalities.
One of the first I saw with an issue was also noted on the https://netty.io/Documentation/New+and+Noteworthy page as one comment.

I take the Echo example, add in the server side a limit of "ping-ping" like this:

public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) {
    limit --;
    if (limit > 0) {
        ByteBuf out = ctx.nextOutboundByteBuffer();
        out.discardReadBytes();
        out.writeBytes(in);
        ctx.flush();
    } else {
        // end
        ctx.close();
    }
}

Then in the client, I added the following at the end of the run method:

        // Start the client. Original one
        ChannelFuture f = b.connect().sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();

        // and now try to reconnect NEW Code
        System.out.println("try to restart");
        // Start the client.
        f = b.connect().sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();

And I get the following output:

sept. 10, 2012 4:06:03 PM io.netty.handler.logging.LoggingHandler
Infos: [id: 0xfedfd297] REGISTERED
sept. 10, 2012 4:06:03 PM io.netty.handler.logging.LoggingHandler
Infos: [id: 0xfedfd297, /0.0.0.0:3780 => /127.0.0.1:8080] ACTIVE
try to restart
Exception in thread "main" sept. 10, 2012 4:06:03 PM io.netty.handler.logging.LoggingHandler
Infos: [id: 0xfedfd297, /0.0.0.0:3780 :> /127.0.0.1:8080] INACTIVE
java.lang.IllegalStateException: channel already registered: [id: 0xfedfd297, /0.0.0.0:3780 :> /127.0.0.1:8080]
    at io.netty.bootstrap.Bootstrap.init(Bootstrap.java:185)
    at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:162)
    at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:152)
    at io.netty.example.echo.EchoClient.run(EchoClient.java:73)
    at io.netty.example.echo.EchoClient.main(EchoClient.java:102)
sept. 10, 2012 4:06:03 PM io.netty.handler.logging.LoggingHandler
Infos: [id: 0xfedfd297, /0.0.0.0:3780 :> /127.0.0.1:8080] UNREGISTERED

Do I missed something in the logic?

Activity

  1. normanmaurer commented on Sep 10, 2012

    @normanmaurer
    Member

    Thats a bug.. I will look into this.

    Sent from my iPhone. Excuse any typos....

    Am 10.09.2012 um 16:16 schrieb Frédéric Brégier [email protected]:

    I try to get into it, and of course, while I am trying to convert Netty3 based code to Netty4, I try also the examples and check some functionalities.
    One of the first I saw with an issue was also noted on the https://netty.io/Documentation/New+and+Noteworthy page as one comment.

    I take the Echo example, add in the server side a limit of "ping-ping" like this:

    public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) {
    limit --;
    if (limit > 0) {
    ByteBuf out = ctx.nextOutboundByteBuffer();
    out.discardReadBytes();
    out.writeBytes(in);
    ctx.flush();
    } else {
    // end
    ctx.close();
    }
    }
    Then in the client, I added the following at the end of the run method:

        // Start the client. Original one
        ChannelFuture f = b.connect().sync();
    
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    
        // and now try to reconnect NEW Code
        System.out.println("try to restart");
        // Start the client.
        f = b.connect().sync();
    
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    

    And I get the following output:

    sept. 10, 2012 4:06:03 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xfedfd297] REGISTERED
    sept. 10, 2012 4:06:03 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xfedfd297, /0.0.0.0:3780 => /127.0.0.1:8080] ACTIVE
    try to restart
    Exception in thread "main" sept. 10, 2012 4:06:03 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xfedfd297, /0.0.0.0:3780 :> /127.0.0.1:8080] INACTIVE
    java.lang.IllegalStateException: channel already registered: [id: 0xfedfd297, /0.0.0.0:3780 :> /127.0.0.1:8080]
    at io.netty.bootstrap.Bootstrap.init(Bootstrap.java:185)
    at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:162)
    at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:152)
    at io.netty.example.echo.EchoClient.run(EchoClient.java:73)
    at io.netty.example.echo.EchoClient.main(EchoClient.java:102)
    sept. 10, 2012 4:06:03 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xfedfd297, /0.0.0.0:3780 :> /127.0.0.1:8080] UNREGISTERED
    Do I missed something in the logic?

    —
    Reply to this email directly or view it on GitHub.

  2. normanmaurer commented on Sep 11, 2012

    @normanmaurer
    Member

    @fredericBregier please check #594 and let me know what you think..

  3. fredericBregier commented on Sep 11, 2012

    @fredericBregier
    MemberAuthor

    @normanmaurer I just check right now (from the branch bootstrap_refactor) with the very same example, and now it works.

    From the log, I obtain the following :

    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xc944f012] REGISTERED
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xc944f012, /127.0.0.1:62021 => /127.0.0.1:8080] ACTIVE
    sept. 11, 2012 6:53:25 PM io.netty.example.echo.EchoClient
    Infos: Start retry connect
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xc944f012, /127.0.0.1:62021 :> /127.0.0.1:8080] INACTIVE
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xc944f012, /127.0.0.1:62021 :> /127.0.0.1:8080] UNREGISTERED
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xfc7b4c6d] REGISTERED
    sept. 11, 2012 6:53:25 PM io.netty.example.echo.EchoClient
    Infos: End of connect again
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xfc7b4c6d, /127.0.0.1:62022 => /127.0.0.1:8080] ACTIVE
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xfc7b4c6d, /127.0.0.1:62022 :> /127.0.0.1:8080] INACTIVE
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xfc7b4c6d, /127.0.0.1:62022 :> /127.0.0.1:8080] UNREGISTERED
    

    The corresponding code (end of Echo client) is as follow:

            // Start the client.
            ChannelFuture f = b.connect().sync();
    
            // Wait until the connection is closed.
            f.channel().closeFuture().sync();
            InternalLogger logger = InternalLoggerFactory.getInstance(EchoClient.class);
            logger.info("Start retry connect");
            // Start the client.
            f = b.connect().sync();
            logger.info("End of connect again");
            // Wait until the connection is closed.
            f.channel().closeFuture().sync();
    

    So my question is : is it normal that logging shows INACTIVE and UNREGISTERED "after" the logging "Start retry connect" while it is after a closeFuture().sync() ? There is no arm there, but I just want to know if for you it is ok ?

  4. normanmaurer commented on Sep 12, 2012

    @normanmaurer
    Member

    Need to check… But at least it works :)

    What do you think about the changes ? Do you think they still provide a nice fluent api for bootstrapping ?

    Am 11.09.2012 um 18:56 schrieb Frédéric Brégier [email protected]:

    @normanmaurer I just check right now with the very same example, and now it works.

    From the log, I obtain the following :

    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xc944f012] REGISTERED
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xc944f012, /127.0.0.1:62021 => /127.0.0.1:8080] ACTIVE
    sept. 11, 2012 6:53:25 PM io.netty.example.echo.EchoClient
    Infos: Start retry connect
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xc944f012, /127.0.0.1:62021 :> /127.0.0.1:8080] INACTIVE
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xc944f012, /127.0.0.1:62021 :> /127.0.0.1:8080] UNREGISTERED
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xfc7b4c6d] REGISTERED
    sept. 11, 2012 6:53:25 PM io.netty.example.echo.EchoClient
    Infos: End of connect again
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xfc7b4c6d, /127.0.0.1:62022 => /127.0.0.1:8080] ACTIVE
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xfc7b4c6d, /127.0.0.1:62022 :> /127.0.0.1:8080] INACTIVE
    sept. 11, 2012 6:53:25 PM io.netty.handler.logging.LoggingHandler
    Infos: [id: 0xfc7b4c6d, /127.0.0.1:62022 :> /127.0.0.1:8080] UNREGISTERED
    The corresponding code (end of Echo client) is as follow:

        // Start the client.
        ChannelFuture f = b.connect().sync();
    
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
        InternalLogger logger = InternalLoggerFactory.getInstance(EchoClient.class);
        logger.info("Start retry connect");
        // Start the client.
        f = b.connect().sync();
        logger.info("End of connect again");
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    

    So my question is : is it normal that logging shows INACTIVE and UNREGISTERED "after" the logging "Start retry connect" while it is after a closeFuture().sync() ? There is no arm there, but I just want to know if for you it is ok ?

    —
    Reply to this email directly or view it on GitHub.

  5. fredericBregier commented on Sep 12, 2012

    @fredericBregier
    MemberAuthor

    As far as I test it, It is ok.
    Concerning the name, it is at least consistent with previous API (3.X) from the name point of view.

  6. normanmaurer commented on Sep 12, 2012

    @normanmaurer
    Member

    Well with 3.x you would have used ConnectionlessBootstrap ;)

    2012/9/12 Frédéric Brégier [email protected]

    As far as I test it, It is ok.
    Concerning the name, it is at least consistent with previous API (3.X)
    from the name point of view.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/591#issuecomment-8483301.

  7. added a commit that references this issue on Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions