Name: dk106046 Date: 02/26/2004
Operating System: Windows
Full JDK version: java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)
Testcase:
/* This test shows that keys registered with a selector are still valid after
the selector has been closed. The API doc for close states that:
Any uncancelled keys still associated with this selector are
invalidated, their channels are deregistered, and any other
resources associated with this selector are released.
*/
import java.util.*;
import java.nio.*;
import java.nio.channels.*;
public class SelectorCloseTest
{
public static void main( String[] args )
{
new SelectorCloseTest() . runTest();
}
public void runTest()
{
try {
// Open a selector and register four channels.
Selector selector = Selector.open();
Pipe pipe1 = Pipe.open();
Pipe pipe2 = Pipe.open();
pipe1.source().configureBlocking( false );
pipe1.sink(). configureBlocking( false );
pipe2.source().configureBlocking( false );
pipe2.sink(). configureBlocking( false );
SelectionKey[] k = new SelectionKey[4];
k[0] = pipe1.source().register( selector, SelectionKey.OP_READ );
k[1] = pipe1.sink(). register( selector, SelectionKey.OP_WRITE );
k[2] = pipe2.source().register( selector, SelectionKey.OP_READ );
k[3] = pipe2.sink(). register( selector, SelectionKey.OP_WRITE );
// This select will return immediately as sinks are ready for writing.
selector.select();
// Close the selector and check it is closed.
selector.close();
System.out.println( "selector.isOpen()=" + selector.isOpen() );
// All keys should have been invalidated and de-registered by the
// close. This doesn't work, they are still valid and registered.
System.out.println( "Number of registered keys=" + selector.keys().size() );
// Find how many keys are still valid.
int numValidKeys = 0;
for ( int i=0; i<4; i++ ) {
if ( k[i].isValid() ) {
++numValidKeys;
}
}
System.out.println( "Number of valid keys=" + numValidKeys );
} catch ( Exception e ) {
System.out.println( "runTest: " + e );
}
}
}
Prposed fix: the implmentation should cancel keys before closing the selector.
[This bug is being submitted as a courtesy, in order to maintain uniformity between Sun & IBM JDKs. It has been fixed in IBM JDKs.
Please contact ###@###.### if you have questions.]
======================================================================
- duplicates
-
JDK-4783178 (se) SelectionKey.isValid() returns true after selector is closed
-
- Closed
-