Fixes bad bug in Java thread handling that caused memory/resource leak.
authorJan Vrany <jan.vrany@fit.cvut.cz>
Fri, 08 Aug 2014 11:02:10 +0100
changeset 3214 c38fcee7b0da
parent 3213 d3094b384e55
child 3215 1f63ee2cdd97
child 3218 93a3efa06bd1
Fixes bad bug in Java thread handling that caused memory/resource leak. Threads created for outsude Java system (for example those created when Thread#currentThread is called from within ordinary Smalltalk process) were never removed from `Threads` map and thus retained forever. This leaks not only memory but also resources (like files or sockets) if reachavle from Thread instances. This commit fixes this by adding an exit action whenever a new Thread is added to the `Java.Threads` map.
Java.st
JavaClass.st
JavaNativeMethodImpl_OpenJDK7.st
JavaProcess.st
JavaRef2.st
JavaThreadingTests.st
abbrev.stc
experiments/experiments.rc
libjava.rc
stx_libjava.st
tools/JavaLintService.st
tools/abbrev.stc
tools/tools.rc
--- a/Java.st	Fri Aug 08 07:36:58 2014 +0100
+++ b/Java.st	Fri Aug 08 11:02:10 2014 +0100
@@ -1529,15 +1529,18 @@
 !Java class methodsFor:'threads'!
 
 addThread: jThread for: stProcess
-    ThreadsAccess
-	critical: [
-	    self assert: (Threads includesKey: jThread) not.
-	    jThread == 0 ifTrue: [self breakPoint:#mh].
-	    Threads at: jThread put: stProcess.
-	]
+    ThreadsAccess critical: [
+        self assert: (Threads includesKey: jThread) not.
+        jThread == 0 ifTrue: [self breakPoint:#mh].
+        Threads at: jThread put: stProcess.
+        stProcess addExitAction: [ 
+            self removeThread: jThread for: stProcess
+        ].
+    ]
 
     "Created: / 26-08-1997 / 19:53:57 / cg"
     "Created: / 09-12-2011 / 12:47:10 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 08-08-2014 / 08:57:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 initializeCurrentThread
@@ -1553,39 +1556,31 @@
 !
 
 removeThread: jThread for: stProcess
-ThreadsAccess
-	critical: [
-    self assert: (Threads includesKey: jThread).
-    Threads removeKey: jThread.]
+    ThreadsAccess critical: [
+        self assert: (Threads includesKey: jThread).
+        self assert: (Threads at: jThread) == stProcess.
+        Threads removeKey: jThread.
+    ]
 
     "Created: / 26-08-1997 / 19:53:57 / cg"
     "Created: / 09-12-2011 / 12:47:32 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
-!
-
-removeThread: jThread ifAbsent: block
-    ThreadsAccess
-	critical: [
-
-	    Threads removeKey: jThread ifAbsent: block
-	]
-
-    "Created: / 26-08-1997 / 19:53:57 / cg"
-    "Created: / 09-12-2011 / 12:51:52 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 08-08-2014 / 08:58:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 threadForStProcess: stProcess
-    ThreadsAccess critical: [ ^Threads keyAtValue: stProcess ].
+    ^ ThreadsAccess critical: [ Threads keyAtValue: stProcess ].
 
     "Created: / 26-08-1997 / 19:53:57 / cg"
     "Created: / 09-12-2011 / 12:50:58 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 08-08-2014 / 09:25:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 threadForStProcess: stProcess ifAbsent: block
-    ThreadsAccess
-	critical: [ |result| result := Threads keyAtValue: stProcess ifAbsent: block. result == 0 ifTrue: [self breakPoint:#mh]. ^ result ].
+    ^ ThreadsAccess critical: [ Threads keyAtValue: stProcess ifAbsent: block ].
 
     "Created: / 26-08-1997 / 19:53:57 / cg"
     "Created: / 09-12-2011 / 12:53:11 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified (format): / 08-08-2014 / 09:25:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 threads
@@ -1597,17 +1592,19 @@
 !
 
 threadsAt: key
-    ^Threads at: key.
+    ^ ThreadsAccess critical:[ Threads at: key ]
 
     "Created: / 26-08-1997 / 19:53:57 / cg"
     "Created: / 09-12-2011 / 12:31:56 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 08-08-2014 / 09:24:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 threadsAt: key ifAbsent: block
-    ThreadsAccess critical: [ ^ Threads at: key ifAbsent: block ]
+    ^ ThreadsAccess critical: [ Threads at: key ifAbsent: block ]
 
     "Created: / 26-08-1997 / 19:53:57 / cg"
     "Created: / 09-12-2011 / 12:49:22 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 08-08-2014 / 09:24:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 threadsDo: block
--- a/JavaClass.st	Fri Aug 08 07:36:58 2014 +0100
+++ b/JavaClass.st	Fri Aug 08 11:02:10 2014 +0100
@@ -1770,55 +1770,8 @@
     extern OBJ ___new();
     RETURN ( ___new(self) );
 %}.
-    "
-     memory allocation failed.
-     When we arrive here, there was no memory, even after
-     a garbage collect.
-     This means, that the VM wanted to get some more memory from the
-     Operatingsystem, which was not kind enough to give it.
-     Bad luck - you should increase the swap space on your machine.
-    "
-    ^ ObjectMemory allocationFailureSignal raise.
-
-
-    "/ Following code is original Smalltalk implementation"
-
-
-"/    | newJavaObject  
-"/      sz            "{ Class: SmallInteger }" 
-"/      offs          "{ Class: SmallInteger }"
-"/    |
-"/
-"/    (accessFlags bitAnd: ACX_ABSTRACT_OR_INTERFACE) ~~ 0 ifTrue: [ 
-"/        JavaVM throwInstantiationExceptionFor: self.
-"/        ^ nil 
-"/    ].            
-"/    newJavaObject := super basicNew.
-"/    offs := JavaObject instSize.
-"/    offs > 0 ifTrue:[
-"/        "Assume first is lockword"
-"/        newJavaObject instVarAt: 1 put: 0.
-"/    ].
-"/    initValues notNil ifTrue: [ 
-"/        sz := self instSize.
-"/        offs + 1  to: sz do: [:i | newJavaObject instVarAt: i put: (initValues at: i - offs) ].
-"/    ].
-"/    self hasFinalize ifTrue:[
-"/        newJavaObject registerForFinalization
-"/    ].
-"/
-"/
-"/    ^ newJavaObject
-
-
-    "
-     (Java classNamed:'java.lang.String') basicNew inspect
-     (Java classNamed:'java.lang.String') newCleared inspect
-     (Java classNamed:'java.lang.String') new inspect
-    "
-
-    "Created: / 02-11-2012 / 21:02:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 21-01-2014 / 10:51:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+
+    "Modified: / 08-08-2014 / 09:57:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 initValueFor:instVarName
--- a/JavaNativeMethodImpl_OpenJDK7.st	Fri Aug 08 07:36:58 2014 +0100
+++ b/JavaNativeMethodImpl_OpenJDK7.st	Fri Aug 08 11:02:10 2014 +0100
@@ -183,6 +183,15 @@
     ^ JavaVM unimplementedNativeMethodSignal raise
 ! !
 
+!JavaNativeMethodImpl_OpenJDK7 class methodsFor:'native - java.nio'!
+
+_java_nio_MappedByteBuffer_force0: this _: a1 _: a2 _: a3 _: a4 _: a5 
+
+    <javanative: 'java/nio/MappedByteBuffer' name: 'force0(Ljava/io/FileDescriptor;JJ)V'>
+
+    ^ JavaVM unimplementedNativeMethodSignal raise
+! !
+
 !JavaNativeMethodImpl_OpenJDK7 class methodsFor:'native - java.util.zip'!
 
 _java_util_zip_ZipFile_getCommentBytes: this _: jzentry _: a2
--- a/JavaProcess.st	Fri Aug 08 07:36:58 2014 +0100
+++ b/JavaProcess.st	Fri Aug 08 11:02:10 2014 +0100
@@ -256,13 +256,12 @@
 "/                                        screenUpdaterClass instVarNamed:'updater' put:nil.
 "/                                    ].
                 
-                Java removeThread:javaThreadObject ifAbsent:[].
             ]
         ]
 
     "Created: / 15-12-2010 / 11:06:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 09-12-2011 / 12:51:55 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
-    "Modified: / 12-06-2014 / 11:57:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 08-08-2014 / 09:03:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaProcess class methodsFor:'documentation'!
--- a/JavaRef2.st	Fri Aug 08 07:36:58 2014 +0100
+++ b/JavaRef2.st	Fri Aug 08 11:02:10 2014 +0100
@@ -232,7 +232,7 @@
 
     "Created: / 08-04-2011 / 11:42:43 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
     "Modified: / 08-04-2011 / 17:39:28 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
-    "Modified: / 20-01-2014 / 09:47:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 07-08-2014 / 14:38:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 invalidateForClass: internalJavaClassName 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/JavaThreadingTests.st	Fri Aug 08 11:02:10 2014 +0100
@@ -0,0 +1,95 @@
+"
+ COPYRIGHT (c) 1996-2011 by Claus Gittinger
+
+ New code and modifications done at SWING Research Group [1]:
+
+ COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
+                            SWING Research Group, Czech Technical University in Prague
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+
+ [1] Code written at SWING Research Group contains a signature
+     of one of the above copright owners. For exact set of such code,
+     see the differences between this version and version stx:libjava
+     as of 1.9.2010
+"
+"{ Package: 'stx:libjava' }"
+
+TestCase subclass:#JavaThreadingTests
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Languages-Java-Tests'
+!
+
+!JavaThreadingTests class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 1996-2011 by Claus Gittinger
+
+ New code and modifications done at SWING Research Group [1]:
+
+ COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
+                            SWING Research Group, Czech Technical University in Prague
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+
+ [1] Code written at SWING Research Group contains a signature
+     of one of the above copright owners. For exact set of such code,
+     see the differences between this version and version stx:libjava
+     as of 1.9.2010
+
+"
+! !
+
+!JavaThreadingTests class methodsFor:'accessing'!
+
+resources
+
+  ^ Array 
+        with: JavaInitializedResource 
+        with: JavaLibrariesResource
+        with: JavaTestsResource
+
+    "Created: / 30-03-2012 / 13:38:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaThreadingTests methodsFor:'tests'!
+
+test_regression_01
+    "Make sure that java.lang.Thread (& corresponding Smalltalk Process) is removed from
+     Java:Threads map when thread finished (to avoid resource leak)"
+
+    | thread threadProcess activeProcess blocker |
+
+    blocker := Semaphore new.
+    [ 
+        thread := JAVA java lang Thread currentThread.
+        "/ Use Java classVarAt: hack to avoud need for access methods not otherwise
+        "/ required. They should be cleaned.
+        threadProcess := (Java classVarAt: #Threads) at: thread.
+        activeProcess := Processor activeProcess.
+        blocker signal.
+    ] fork.
+    blocker wait.
+    self assert: thread notNil.
+    self assert: threadProcess notNil.
+    self assert: threadProcess == activeProcess.
+    "/ Give exist actions chance to run.
+    Delay waitForMilliseconds: 100.
+    self assert: ((Java classVarAt: #Threads) includesKey: thread) not.
+
+    "Created: / 08-08-2014 / 09:33:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
--- a/abbrev.stc	Fri Aug 08 07:36:58 2014 +0100
+++ b/abbrev.stc	Fri Aug 08 11:02:10 2014 +0100
@@ -70,6 +70,7 @@
 JavaSourceFileWriter JavaSourceFileWriter stx:libjava 'Languages-Java-Support' 0
 JavaTestCaseProxy JavaTestCaseProxy stx:libjava 'Languages-Java-Tests-Proxies' 3
 JavaTestsLoader JavaTestsLoader stx:libjava 'Languages-Java-Tests' 0
+JavaThreadingTests JavaThreadingTests stx:libjava 'Languages-Java-Tests' 1
 JavaTopView JavaTopView stx:libjava 'Languages-Java-Views-Support' 2
 JavaUTF8Tests JavaUTF8Tests stx:libjava 'Languages-Java-Tests' 1
 JavaUnresolvedCompilationError JavaUnresolvedCompilationError stx:libjava 'Languages-Java-Support' 1
--- a/experiments/experiments.rc	Fri Aug 08 07:36:58 2014 +0100
+++ b/experiments/experiments.rc	Fri Aug 08 11:02:10 2014 +0100
@@ -25,7 +25,7 @@
       VALUE "LegalCopyright", "Copyright Claus Gittinger 1988-2014\nCopyright eXept Software AG 1998-2014\0"
       VALUE "ProductName", "Smalltalk/X\0"
       VALUE "ProductVersion", "6.2.4.0\0"
-      VALUE "ProductDate", "Thu, 07 Aug 2014 00:45:28 GMT\0"
+      VALUE "ProductDate", "Fri, 08 Aug 2014 09:52:58 GMT\0"
     END
 
   END
--- a/libjava.rc	Fri Aug 08 07:36:58 2014 +0100
+++ b/libjava.rc	Fri Aug 08 11:02:10 2014 +0100
@@ -25,7 +25,7 @@
       VALUE "LegalCopyright", "Copyright Claus Gittinger 1988-2011\nCopyright eXept Software AG 1998-2011\nCopyright Jan Vrany, Jan Kurs and Marcel Hlopko\n          SWING Research Group, Czech Technical University In Prague\0"
       VALUE "ProductName", "Smalltalk/X\0"
       VALUE "ProductVersion", "6.2.4.0\0"
-      VALUE "ProductDate", "Thu, 07 Aug 2014 00:45:25 GMT\0"
+      VALUE "ProductDate", "Fri, 08 Aug 2014 09:52:55 GMT\0"
     END
 
   END
--- a/stx_libjava.st	Fri Aug 08 07:36:58 2014 +0100
+++ b/stx_libjava.st	Fri Aug 08 11:02:10 2014 +0100
@@ -375,6 +375,7 @@
         JavaSourceFileWriter
         (JavaTestCaseProxy autoload)
         JavaTestsLoader
+        (JavaThreadingTests autoload)
         JavaTopView
         (JavaUTF8Tests autoload)
         JavaUnresolvedCompilationError
--- a/tools/JavaLintService.st	Fri Aug 08 07:36:58 2014 +0100
+++ b/tools/JavaLintService.st	Fri Aug 08 11:02:10 2014 +0100
@@ -104,10 +104,10 @@
      must be redefined to return true in subclasses (but each class must do it only
      for itself - not for subclasses"
 
-    ^ self == JavaLintService
+    ^ (self == JavaLintService)
 
     "Created: / 24-07-2013 / 11:35:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 16-09-2013 / 09:56:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 08-08-2014 / 06:53:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaLintService class methodsFor:'testing'!
--- a/tools/abbrev.stc	Fri Aug 08 07:36:58 2014 +0100
+++ b/tools/abbrev.stc	Fri Aug 08 11:02:10 2014 +0100
@@ -5,6 +5,7 @@
 JavaCodeBundleEditor JavaCodeBundleEditor stx:libjava/tools 'Languages-Java-Tools' 1
 JavaCodeLibraryEditor JavaCodeLibraryEditor stx:libjava/tools 'Languages-Java-Tools' 1
 JavaCompiler JavaCompiler stx:libjava/tools 'Languages-Java-Support-Compiling' 0
+JavaCompilerTests JavaCompilerTests stx:libjava/tools 'Languages-Java-Tests-Compiling' 1
 JavaLintAnnotation JavaLintAnnotation stx:libjava/tools 'Languages-Java-Tools-Editor-Lint' 0
 JavaLintHighlighter JavaLintHighlighter stx:libjava/tools 'Languages-Java-Tools-Editor-Lint' 0
 JavaLintPopupWindow JavaLintPopupWindow stx:libjava/tools 'Languages-Java-Tools-Editor-Lint' 1
@@ -24,4 +25,3 @@
 GroovyScanner GroovyScanner stx:libjava/tools 'Languages-Groovy-Tools-Source' 3
 JavaLexicalHighlighter JavaLexicalHighlighter stx:libjava/tools 'Languages-Java-Tools-Source' 0
 GroovySourceHighlighter GroovySourceHighlighter stx:libjava/tools 'Languages-Groovy-Tools-Source' 0
-JavaCompilerTests JavaCompilerTests stx:libjava/tools 'Languages-Java-Tests-Compiling' 1
--- a/tools/tools.rc	Fri Aug 08 07:36:58 2014 +0100
+++ b/tools/tools.rc	Fri Aug 08 11:02:10 2014 +0100
@@ -25,7 +25,7 @@
       VALUE "LegalCopyright", "Copyright Claus Gittinger 1988-2011\nCopyright eXept Software AG 1998-2011\0"
       VALUE "ProductName", "Smalltalk/X\0"
       VALUE "ProductVersion", "6.2.4.0\0"
-      VALUE "ProductDate", "Thu, 07 Aug 2014 13:39:16 GMT\0"
+      VALUE "ProductDate", "Fri, 08 Aug 2014 09:53:01 GMT\0"
     END
 
   END