Selector bug?

Discussion in 'Java' started by Andersen, Jan 28, 2006.

  1. Andersen

    Andersen Guest

    I am doing non-blocking I/O with NIO when reading and writing to sockets.

    My Selector.select() call returns with result 0, and does not block,
    which it is supposed to do. What can be wrong? I am not doing wakeup()
    and no exceptions are being thrown.
     
    Andersen, Jan 28, 2006
    #1
    1. Advertising

  2. Andersen

    Andersen Guest

    Bug fixed.

    Not sure if the bug is due to an undocumented NIO feature or not.

    Here it is:
    I was creating new channels, setting interest ops WRITE, READ, and
    CONNECT. But in practise, I was connecting in blocking mode, ensuring
    that connect() finishes properly. Thereafter I would put the channel in
    non-blocking mode, and registering it with the above OPs...

    I.e., OP_CONNECT was set as interested bit, but I was actually never
    connect()ing with non-blocking IO. As a result, the select() was
    returning with 0.

    I now removed the OP_CONNECT and everything works perfectly.

    Notice that select() never ever returned with the SelectionKey saying
    isConnectable(), because I had already connected properly.
     
    Andersen, Jan 28, 2006
    #2
    1. Advertising

  3. Andersen wrote:
    >
    > I am doing non-blocking I/O with NIO when reading and writing to sockets.
    >
    > My Selector.select() call returns with result 0, and does not block,
    > which it is supposed to do. What can be wrong? I am not doing wakeup()
    > and no exceptions are being thrown.


    The remaining possibility, according to the documentation, is that the
    thread has been interrupted. The docs do no mention clearing the status.
    So presumably an interrupted selection thread will spin. You can check
    and clearly the interrupt status with the oddly named Thread.interrupted.

    http://download.java.net/jdk6/docs/api/java/lang/Thread.html#interrupted()

    OTOH, it's not exactly unknown for NIO to have bugs.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jan 28, 2006
    #3
  4. Andersen

    EJP Guest

    Andersen wrote:

    > Bug fixed.
    >
    > Not sure if the bug is due to an undocumented NIO feature or not.
    >
    > Here it is:
    > I was creating new channels, setting interest ops WRITE, READ, and
    > CONNECT. But in practise, I was connecting in blocking mode, ensuring
    > that connect() finishes properly. Thereafter I would put the channel in
    > non-blocking mode, and registering it with the above OPs...
    >
    > I.e., OP_CONNECT was set as interested bit, but I was actually never
    > connect()ing with non-blocking IO. As a result, the select() was
    > returning with 0.
    >
    > I now removed the OP_CONNECT and everything works perfectly.


    Leaving aside the issue of how best to use OP_WRITE, which has already
    been dealt with, you should never register for OP_WRITE *and* OP_CONNECT
    at the same time. If you want the connection completion event, register
    for OP_CONNECT *only*: when you get it, deregister OP_CONNECT and *then*
    register OP_READ and/or OP_WRITE.

    Reason: under the hood OP_WRITE and OP_CONNECT may be the same thing,
    and Java doesn't necessarily succeeded in pretending they are different.
    Ditto for OP_READ and OP_ACCEPT at the server side.
     
    EJP, Feb 7, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    4,309
    Douglas Sykora
    Feb 11, 2005
  2. Chris

    Color selector

    Chris, Dec 10, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    339
    Jose L Rodriguez
    Dec 10, 2003
  3. Brian Henry
    Replies:
    2
    Views:
    634
    Steven Cheng[MSFT]
    Jul 22, 2004
  4. Brian Henry
    Replies:
    3
    Views:
    6,099
  5. Gavin Kistner

    REXML bug - descendant-or-self selector

    Gavin Kistner, Sep 4, 2005, in forum: Ruby
    Replies:
    1
    Views:
    101
    Gavin Kistner
    Sep 5, 2005
Loading...

Share This Page