JDK-1194877 : Can't kill threads waiting on I/O
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 1.0,1.0.2
  • Priority: P5
  • Status: Closed
  • Resolution: Won't Fix
  • OS: solaris_2.4,solaris_2.5
  • CPU: sparc
  • Submitted: 1995-03-02
  • Updated: 2014-10-28
  • Resolved: 1999-06-30
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Description
	If a thread is stuck waiting on some I/O, then there is no clean
	way to get rid of it quickly.

	If you close it's file, it won't notice and it will continue to
	wait on its condition variable.

	If you kill it using Thread.stop(), it will not receive that
	exception either since it is waiting on its condition variable.

	The only way to really kill it is to use Thread.stop() and then
	wait for it to receive more input from the file which will wake
	it up from its conditional wait.

	Here is a test case.  The reader thread will never die.

import java.io.InputStream;
import net.www.html.URL;

public class IOTest implements Runnable {
    InputStream in;

    public IOTest(InputStream is) {
	in = is;
    }

    public void run() {
	while (true) {
	    int ch = in.read();
	    System.out.write(ch);
	}
    }

    public static void main(String argv[]) {
	IOTest iot = new IOTest(new URL(null,
					"http://benden:8888/stock.dat/SUNW")
				.openStream());
	Thread t = new Thread(iot);
	t.start();
	Thread.sleep(10000);
	System.out.print("closing input...");
	System.out.flush();
	iot.in.close();
	System.out.println("done");
	Thread.sleep(5000);
	System.out.print("killing...");
	System.out.flush();
	t.stop();
	System.out.println("done");
    }
}

The description field as copied from bug report 1212384 follows:

fp.bugs 1276 - ###@###.### (Jon Payne)

Once a thread is blocked in read() it is not possible to kill that thread, or unblock it.

Description:
I have a piece of applet code which tries to stop another
thread which is blocked reading from a network connection.
It tries to unblock it by closing the network connection,
hoping that the read will return eof immediately.  Instead
it sits there forever blocked, the thread in PendingDeath
state.

Comments
SUGGESTED FIX When you close an I/O channel, it should wake up anyone waiting on that channel. When you kill a thread that is waiting on a condition variable, it should immediately wake up.
11-06-2004

PUBLIC COMMENTS Thread.interruupt does not awaken threads waiting on the various forms of I/O. If a thread is stuck waiting on some I/O, then there is no clean way to get rid of it quickly. If you close it's file, it won't notice and it will continue to wait on its condition variable. If you kill it using Thread.stop(), it will not receive that exception either since it is waiting on its condition variable. The only way to really kill it is to use Thread.stop() and then wait for it to receive more input from the file which will wake it up from its conditional wait.
10-06-2004

EVALUATION We need to implement interruptable I/O. Much of the infrastructure is already in place to do so, although the implementation will differ drastically on each platform. timothy.lindholm@Eng 1996-12-03 See also redundant bug 1258776 -- it has another test case. timothy.lindholm@Eng 1997-02-25 I don't think we need to implement this feature. First of all, the Thread.stop has been deprecated. The interruptable io is also facing deprecation. Theoretically, there is no way to kill a thread that is not cooperative, unless we use operating system thread primitive. But os primitive can not do any thread specific cleanup for the Java thread. Therefore, we don't have a nice way to implement it. For the last chance effort, we could use System.exit(). ###@###.### 1999-06-29
29-06-1999