Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Selector bug?

Thread view: 
Andersen - 28 Jan 2006 21:43 GMT
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.
Thomas Hawtin - 28 Jan 2006 22:09 GMT
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Andersen - 28 Jan 2006 22:20 GMT
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.
EJP - 07 Feb 2006 04:35 GMT
> Bug fixed.
>
[quoted text clipped - 11 lines]
>
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.