Improve detection and handling of orphaned coroutine threads.

This commit is contained in:
James Roseborough
2012-01-21 05:26:41 +00:00
parent 61514aa02c
commit 26ed1ef392
9 changed files with 398 additions and 314 deletions

View File

@@ -47,7 +47,7 @@ public class AllTests {
vm.addTestSuite(MetatableTest.class);
vm.addTestSuite(LuaOperationsTest.class);
vm.addTestSuite(StringTest.class);
vm.addTestSuite(LuaThreadTest.class);
vm.addTestSuite(OrphanedThreadTest.class);
suite.addTest(vm);
// table tests

View File

@@ -1,134 +0,0 @@
/*******************************************************************************
* Copyright (c) 2011 Luaj.org. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
package org.luaj.vm2;
import java.lang.ref.WeakReference;
import junit.framework.TestCase;
import org.luaj.vm2.lib.OneArgFunction;
public class LuaThreadTest extends TestCase {
public void testMainThread() {
assertEquals( true, LuaThread.isMainThread( LuaThread.getRunning()) );
assertEquals( "running", LuaThread.getRunning().getStatus() );
assertEquals( LuaValue.FALSE, LuaThread.getRunning().resume(LuaValue.NONE).arg1() );
try {
LuaThread.yield(LuaThread.yield(LuaValue.NONE));
fail("did not throw lua error as expected");
} catch ( LuaError le ) {
}
}
public void testLuaThreadIsCollected() throws InterruptedException { System.out.println("testLuaThread - starting");
int originalInterval = LuaThread.GC_INTERVAL;
try {
LuaThread.GC_INTERVAL = 75;
TestRig rig = new TestRig();
assertEquals( "resumed 1 times, arg=test-arg", rig.resumeOnce() );
assertEquals( true, rig.isThreadReferenced() );
assertEquals( true, rig.isFunctionReferenced() );
assertEquals( true, rig.isArgReferenced() );
collectGarbage();
assertEquals( "resumed 2 times, arg=test-arg", rig.resumeOnce() );
assertEquals( true, rig.isThreadReferenced() );
assertEquals( true, rig.isFunctionReferenced() );
assertEquals( true, rig.isArgReferenced() );
Thread.sleep( 200 );
collectGarbage();
assertEquals( "resumed 3 times, arg=test-arg", rig.resumeOnce() );
assertEquals( true, rig.isThreadReferenced() );
assertEquals( true, rig.isFunctionReferenced() );
assertEquals( true, rig.isArgReferenced() );
// check that references are collected
// some time after lua thread is de-referenced
rig.weakenReference();
Thread.sleep( 200 );
collectGarbage();
assertEquals( false, rig.isThreadReferenced() );
assertEquals( false, rig.isFunctionReferenced() );
assertEquals( false, rig.isArgReferenced() );
} finally {
LuaThread.GC_INTERVAL = originalInterval;
}
}
static class TestRig {
LuaThread luaThread;
final WeakReference luaRef;
final WeakReference funcRef;
final WeakReference argRef;
TestRig() {
LuaValue a = new LuaUserdata( "test-arg" );
LuaValue f = new TestFunction();
luaThread = new LuaThread( f, new LuaTable() );
luaRef = new WeakReference( luaThread );
funcRef = new WeakReference( f );
argRef = new WeakReference( a );
}
public String resumeOnce() {
LuaThread t = (LuaThread) luaRef.get();
LuaValue a = (LuaValue) argRef.get();
return t==null? "no ref to lua thread":
a==null? "no ref to arg":
t.resume(a).arg(2).toString();
}
public void weakenReference() {
luaThread = null;
}
public Object isThreadReferenced() {
return null != luaRef.get();
}
public Object isFunctionReferenced() {
return null != funcRef.get();
}
public Object isArgReferenced() {
return null != argRef.get();
}
}
static class TestFunction extends OneArgFunction {
public LuaValue call(LuaValue arg) {
for ( int count=1; true; count++ ) {
LuaValue r = LuaValue.valueOf("resumed "+count+" times, arg="+arg);
Varargs v = LuaThread.yield( r );
arg = v.arg1();
}
}
}
static void collectGarbage() {
Runtime rt = Runtime.getRuntime();
rt.gc();
try {
Thread.sleep(20);
rt.gc();
Thread.sleep(20);
} catch ( Exception e ) {
e.printStackTrace();
}
rt.gc();
}
}

View File

@@ -0,0 +1,167 @@
/*******************************************************************************
* Copyright (c) 2012 Luaj.org. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
package org.luaj.vm2;
import java.io.ByteArrayInputStream;
import java.lang.ref.WeakReference;
import junit.framework.TestCase;
import org.luaj.vm2.LoadState;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.jse.JsePlatform;
public class OrphanedThreadTest extends TestCase {
LuaThread luathread;
WeakReference luathr_ref;
LuaValue function;
WeakReference func_ref;
LuaValue env;
protected void setUp() throws Exception {
LuaThread.thread_orphan_check_interval = 5;
env = JsePlatform.standardGlobals();
}
protected void tearDown() {
LuaThread.thread_orphan_check_interval = 30000;
}
public void testCollectOrphanedNormalThread() throws Exception {
function = new NormalFunction();
doTest(LuaValue.TRUE, LuaValue.ZERO);
}
public void testCollectOrphanedEarlyCompletionThread() throws Exception {
function = new EarlyCompletionFunction();
doTest(LuaValue.TRUE, LuaValue.ZERO);
}
public void testCollectOrphanedAbnormalThread() throws Exception {
function = new AbnormalFunction();
doTest(LuaValue.FALSE, LuaValue.valueOf("abnormal condition"));
}
public void testCollectOrphanedClosureThread() throws Exception {
String script =
"print('in closure, arg is '..(...))\n" +
"arg = coroutine.yield(1)\n" +
"print('in closure.2, arg is '..arg)\n" +
"arg = coroutine.yield(0)\n" +
"print('leakage in closure.3, arg is '..arg)\n" +
"return 'done'\n";
LuaC.install();
function = LoadState.load(new ByteArrayInputStream(script.getBytes()), "script", env);
doTest(LuaValue.TRUE, LuaValue.ZERO);
}
public void testCollectOrphanedPcallClosureThread() throws Exception {
String script =
"f = function(x)\n" +
" print('in pcall-closure, arg is '..(x))\n" +
" arg = coroutine.yield(1)\n" +
" print('in pcall-closure.2, arg is '..arg)\n" +
" arg = coroutine.yield(0)\n" +
" print('leakage in pcall-closure.3, arg is '..arg)\n" +
" return 'done'\n" +
"end\n" +
"print( 'pcall-closre.result:', pcall( f, ... ) )\n";
LuaC.install();
function = LoadState.load(new ByteArrayInputStream(script.getBytes()), "script", env);
doTest(LuaValue.TRUE, LuaValue.ZERO);
}
private void doTest(LuaValue status2, LuaValue value2) throws Exception {
luathread = new LuaThread(function, env);
luathr_ref = new WeakReference(luathread);
func_ref = new WeakReference(function);
assertNotNull(luathr_ref.get());
// resume two times
Varargs a = luathread.resume(LuaValue.valueOf("foo"));
assertEquals(LuaValue.TRUE, a.arg1());
assertEquals(LuaValue.ONE, a.arg(2));
a = luathread.resume(LuaValue.valueOf("bar"));
assertEquals(status2, a.arg1());
assertEquals(value2, a.arg(2));
// drop strong references
luathread = null;
function = null;
// gc
for (int i=0; i<100 && (luathr_ref.get() != null || func_ref.get() != null); i++) {
Runtime.getRuntime().gc();
Thread.sleep(5);
}
// check reference
assertNull(luathr_ref.get());
assertNull(func_ref.get());
}
static class NormalFunction extends OneArgFunction {
public LuaValue call(LuaValue arg) {
System.out.println("in normal.1, arg is "+arg);
arg = LuaThread.yield(ONE).arg1();
System.out.println("in normal.2, arg is "+arg);
arg = LuaThread.yield(ZERO).arg1();
System.out.println("in normal.3, arg is "+arg);
return NONE;
}
}
static class EarlyCompletionFunction extends OneArgFunction {
public LuaValue call(LuaValue arg) {
System.out.println("in early.1, arg is "+arg);
arg = LuaThread.yield(ONE).arg1();
System.out.println("in early.2, arg is "+arg);
return ZERO;
}
}
static class AbnormalFunction extends OneArgFunction {
public LuaValue call(LuaValue arg) {
System.out.println("in abnormal.1, arg is "+arg);
arg = LuaThread.yield(ONE).arg1();
System.out.println("in abnormal.2, arg is "+arg);
error("abnormal condition");
return ZERO;
}
}
static class ClosureFunction extends OneArgFunction {
public LuaValue call(LuaValue arg) {
System.out.println("in abnormal.1, arg is "+arg);
arg = LuaThread.yield(ONE).arg1();
System.out.println("in abnormal.2, arg is "+arg);
error("abnormal condition");
return ZERO;
}
}
}