JDK-7013961 : Threads attached via JNI attach prevent daemon ThreadGroups from being destroyed
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2011-01-21
  • Updated: 2011-05-18
  • Resolved: 2011-05-18
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 7
7 b130Fixed
Related Reports
Relates :  
Relates :  
Description
When a thread attaches via JNI AttachCurrentThread(AsDaemon) the java.lang.Thread constructor is invoked, which calls init(), which calls group.addUnstarted(). Normally start() will call group.threadStarting() which calls add(t) and decrements the nUnstartedThreads count. But the JNI attach makes a direct call to group.add(t). Which means nUnstartedThreads is wrong and daemon ThreadGroups will never be destroyed when the last thread exits.

Comments
EVALUATION Changeset: cd9a302f2806 Author: chegar Date: 2011-02-09 09:53 +0000 URL: http://hg.openjdk.java.net/jdk7/tl/jdk/rev/cd9a302f2806 7013961: Threads attached via JNI attach prevent daemon ThreadGroups from being destroyed Reviewed-by: dholmes ! src/share/classes/java/lang/Thread.java ! src/share/classes/java/lang/ThreadGroup.java
09-02-2011

EVALUATION I think the best, and most straight forward, way to resolve this issue is to change it in the libraries. ThreadGroup.threadStarting() was added as part of CR 6379235 to notify the thread group that the given thread was being started. Originally threadStarting() simply invoked add(Thread). The groups unstarted thread count was decremented by threadStarted(), if the thread was successfully started. I believe threadStarting() was only added to give symmetry (in terms of method name) with threadStarted(). Since CR 6988618 the unstarted thread count is now decremented just before the thread is started, i.e. in threadStarting(). What I am proposing is to remove threadStarting(), decrement the unstarted count in add, and then have Thread.start invoke add() directly. This closer resembles what we have in JDK6. http://cr.openjdk.java.net/~chegar/7013961/webrev.00/webrev/
08-02-2011

PUBLIC COMMENTS It is a simple matter to have the VM invoke g.add(t) for JDK6 and g.threadStarting(t) for JDK7+. That might be simpler than trying to rework the Java-level logic given there is no way to rell the difference between a starting thread and an attaching thread.
24-01-2011