JDK-6318568 : (se) Selector.select() should return something when interrupted
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_10
  • CPU: sparc
  • Submitted: 2005-09-01
  • Updated: 2010-08-05
  • Resolved: 2005-11-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6Resolved
Related Reports
Duplicate :  
Relates :  
Description
When Thread.interrupt is executed for a thread in Selector.select(), the interrupted thread just terminates.

REPRODUCE : 
 Compile the attached test program and launch "java BID5100121_1"

---Test Program : BID5100121_1.java -----
import java.net.*;
import java.io.*;
import java.util.*;
import java.nio.*;
import java.nio.channels.*;

public class BID5100121_1 extends Thread {
    static Thread main_thread;
    private int type;
    static boolean flag = false;
    public static void main( String args[] ) throws Exception {
        if ( args.length == 0 ) {
          flag = true;
        }
        Selector selector = Selector.open();
        ServerSocketChannel server = ServerSocketChannel.open();
        server.configureBlocking(false);
        InetSocketAddress addr = new InetSocketAddress(9998);
        server.socket().bind(addr);
        server.register( selector, SelectionKey.OP_ACCEPT);

        main_thread = Thread.currentThread();
        
        new BID5100121_1(1).start();
        String message=null;        

	try {
            int n = selector.select();
            if ( n == 0 ) {
                System.out.println("TimeOut");
            }
	    else {
	        System.out.println("select():"+n);
	    }
        }
	catch(Throwable e) {
	    e.printStackTrace();
	}
        System.out.println("end");
    }

    BID5100121_1( int t ) {
        type = t;
    }

    public void run() {
        try {
            Thread.sleep(3000);
        }
        catch(Exception e ) {
            e.printStackTrace();
        }

        System.out.println("interrupt");

        try {
	    main_thread.interrupt();
        }
        catch(Exception e ) {
            e.printStackTrace();
        }
       
    }
}
                    
-----------------------------------------

PROBLEM :
 The main thread executes select() and another thread interrupts the main thread.
 Launching "java BID5100121", I see the following messages.

chive[42]% java BID5100121_1
interrupt
chive[43]% 

 It seems that the interrupted main thread just terminates and following operations are cancelled.
 
REQUEST :
 IOException should appear, or select() should return something (ex. returns 0)

Comments
EVALUATION -- 5100121 fixed the interrupt issue for poll-Selector. As part of the forward port to mustang, Lew has agreed to fix this for the /dev/poll Selector too.
09-11-2005

EVALUATION -- Thread.interrupt is specified to unblock a thread blocked in the Selector just as if the Selector's wakeup method was invoked. On Solaris, due to the legacy interruptable I/O support, the underlying poll (or ioctl if /dev/poll is used) is being interrupted. This is because the select to thrown an IOException rather than behave like a wakeup. The reason the submitter test case appears to behave bizarrely is that it doesn't clear the interrupt flag (via Thread.interrupt) and this is causing the printing of the stack trace for the IOException to be interrupted. The fix for this bug will also fix the issue of spurious wakeups when the poll is interrupted by process signals.
21-09-2005

EVALUATION This issue can be reproduced on Solaris but not on other operating systems. This seems to be because there is support for the legacy interruptable I/O in addition to setting the interrupt flag and waking up the selector. As a result the poll or ioctl (depending on which selector provider is used) is interrupted (EINTR) and is throwning an IOException with a pending interrupt. Running a fastdebug build with -XX:+TraceExceptions shows that the interrupt flag is causing InterruptedIOException to be thrown in subsequent to print a message.
01-09-2005