JDK-6851688 : Hung up in applet application
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 5.0u11
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_vista
  • CPU: x86
  • Submitted: 2009-06-16
  • Updated: 2011-01-19
  • Resolved: 2009-08-19
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.
Other Other Other JDK 6 JDK 7
5.0u23-revFixed 5.0u24-revFixed 5.0u25Fixed 6u18-revFixed 7 b70Fixed
Description
The problem(abort/hung up) was found in customer's applet application.

JDK : 5.0u11
OS  : Windows Vista

After analyzing the dump, Fujitsu found the thread where the problem
occurred used JNIEnv* that belonged to another thread.

---
[j2se/src/windows/native/sun/windows/awt_Toolkit.cpp]
  ...
JNIEnv* AwtToolkit::m_env;
HANDLE AwtToolkit::m_thread;

void AwtToolkit::SetEnv(JNIEnv *env) {
    if (m_env != NULL) { // If already cashed (by means of embeddedInit() call).
        return;
    }
    m_thread = GetCurrentThread();
    m_env = env;
}

JNIEnv* AwtToolkit::GetEnv() {
    return (m_env == NULL || m_thread != GetCurrentThread()) ?
        (JNIEnv*)JNU_GetEnv(jvm, JNI_VERSION_1_2) : m_env;
}
---

The above snippet is where JNIEnv * was used that was set by another thread.
The code uses Windows API GetCurrentThread() that always returns a
constant according to the following page.

http://msdn.microsoft.com/en-us/library/ms683182.aspx
---
GetCurrentThread()
 Return Value
   The return value is a pseudo handle for the current thread.
 Remarks
   A pseudo handle is a special constant that is interpreted
                        ~~~~~~~~~~~~~~~~
   as the current thread handle. The calling thread can use this handle
   to specify itself whenever a thread handle is required.
   Pseudo handles are not inherited by child processes.
---

It is wrong that the AWT code uses the return value of
GetCurrentThread() as thread identity. GetCurrentThreadId() should be
used if Windows API is used.

Additionally, we found the some codes where call AwtToolkit::GetEnv()
and get returned JNIEnv* uses JNU_GetEnv(). These should to be fixed to
call JNU_GetEnv(jvm, JNI_VERSION_1_2).

Comments
SUGGESTED FIX --- old/src/windows/native/sun/windows/awt_Toolkit.cpp Fri Jul 31 15:21:54 2009 +++ new/src/windows/native/sun/windows/awt_Toolkit.cpp Fri Jul 31 15:21:53 2009 @@ -1596,18 +1596,18 @@ } JNIEnv* AwtToolkit::m_env; -HANDLE AwtToolkit::m_thread; +DWORD AwtToolkit::m_threadId; void AwtToolkit::SetEnv(JNIEnv *env) { if (m_env != NULL) { // If already cashed (by means of embeddedInit() call). return; } - m_thread = GetCurrentThread(); + m_threadId = GetCurrentThreadId(); m_env = env; } JNIEnv* AwtToolkit::GetEnv() { - return (m_env == NULL || m_thread != GetCurrentThread()) ? + return (m_env == NULL || m_threadId != GetCurrentThreadId()) ? (JNIEnv*)JNU_GetEnv(jvm, JNI_VERSION_1_2) : m_env; } --- old/src/windows/native/sun/windows/awt_Toolkit.h Fri Jul 31 15:21:59 2009 +++ new/src/windows/native/sun/windows/awt_Toolkit.h Fri Jul 31 15:21:57 2009 @@ -442,7 +442,7 @@ private: static JNIEnv *m_env; - static HANDLE m_thread; + static DWORD m_threadId; public: static void SetEnv(JNIEnv *env); static JNIEnv* GetEnv();
31-07-2009

EVALUATION It is wrong that the AWT code uses the return value of GetCurrentThread() as thread identity. GetCurrentThreadId() should be used if Windows API is used.
16-06-2009