Selector bug?

A

Andersen

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.
 
A

Andersen

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.
 
T

Thomas Hawtin

Andersen said:
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
 
E

EJP

Andersen said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top