JDK-4203988 : (thread) Threads not garbage collected if constructor fails with an exception
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.1.7,1.3.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,windows_2000
  • CPU: generic,x86
  • Submitted: 1999-01-19
  • Updated: 2006-07-20
  • Resolved: 2006-07-20
Related Reports
Duplicate :  
Description
Name: diC59631			Date: 01/19/99


This problem has been verified on JDK1.1.7beta2 for
Digital Unix 4.0c from DEC, on JDK1.1.5K on SunOS 5.5, 
and on Linux_JDK_1.1.5_v7_sbb_unified.  It does not seem
to exist on Sun JDK/Symantec JIT on Win32.

If a thread is created and throws an exception in its
constructor, or if the constructor returns but the thread
is never started, the thread is never garbage collected
(even when invoking System.gc() explicitly).  The first case is
especially egregious, because there is no valid reference to 
do anything to the thread with.

Simple code triggering this problem:

public class test {
  public static void main(String[] args) {
    Broken foo=null;
    while(true){
      try{
	foo = new Broken();
	foo.start();
      }
      catch (Exception e){
	System.err.println("caught exception");
      }
      System.gc();
    }
  }
}

class Broken extends Thread{
  Broken()
    throws Exception {
      sleep(1000);
      throw new Exception("hi");
  }

  public void run(){
  }
}

Attaching a debugger to this code will show an ever
increasing list of zombie threads:
 9. (Broken)0x6e314698           Thread-5  zombie                              
10. (Broken)0x6e303c68           Thread-6  zombie                              
11. (Broken)0x6e314658           Thread-7  zombie                              
12. (Broken)0x6e314620           Thread-8  zombie                              
13. (Broken)0x6e3146c0           Thread-9  zombie                              
14. (Broken)0x6e314238           Thread-10 zombie                              
15. (Broken)0x6e3146f0           Thread-15 zombie                              
16. (Broken)0x6e313fc8           Thread-16 zombie                              

The problem will also be encountered if the
exception is not thrown and the start() call is
never made.
(Review ID: 41109)
======================================================================

Comments
EVALUATION It is possible to fix this problem without changing the ThreadGroup spec. Specifically, we can refrain from adding a Thread to a ThreadGroup until start() is called. This is precisely the solution that was implemented for 4533087. Closing this issue as duplicate.
20-07-2006

WORK AROUND Name: diC59631 Date: 01/19/99 Catch exceptions inside thread constructors and set a flag that the run() method checks to see if the thread was set up properly. ======================================================================
04-08-2004

EVALUATION The problem is caused by ThreadGroup. The Thread constructor insert itself into thread group, even the subclass's constructor throws an exception. The thread object is still reachable from the thread group. Only the normal thread exit can remove the thread from the thread group. There is no way to fix this bug without changing the ThreadGroup spec. We need to re-eveluate the entire ThreadGroup design before making any spec change. ###@###.### 1999-05-19 We need some spec clarification. ###@###.### 1999-05-24
19-05-1999