8282704: runtime/Thread/StopAtExit.java may leak memory
Reviewed-by: dholmes, alanb
This commit is contained in:
parent
80415e04c5
commit
3f923b82c3
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -23,11 +23,13 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @bug 8167108 8266130
|
* @bug 8167108 8266130 8282704
|
||||||
* @summary Stress test java.lang.Thread.stop() at thread exit.
|
* @summary Stress test java.lang.Thread.stop() at thread exit.
|
||||||
|
* @modules java.base/java.lang:open
|
||||||
* @run main/othervm StopAtExit
|
* @run main/othervm StopAtExit
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@ -38,6 +40,10 @@ public class StopAtExit extends Thread {
|
|||||||
public CountDownLatch exitSyncObj = new CountDownLatch(1);
|
public CountDownLatch exitSyncObj = new CountDownLatch(1);
|
||||||
public CountDownLatch startSyncObj = new CountDownLatch(1);
|
public CountDownLatch startSyncObj = new CountDownLatch(1);
|
||||||
|
|
||||||
|
public StopAtExit(ThreadGroup group, Runnable target) {
|
||||||
|
super(group, target);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
@ -72,11 +78,18 @@ public class StopAtExit extends Thread {
|
|||||||
System.out.println("About to execute for " + timeMax + " seconds.");
|
System.out.println("About to execute for " + timeMax + " seconds.");
|
||||||
|
|
||||||
long count = 0;
|
long count = 0;
|
||||||
|
long manualDestroyCnt = 0;
|
||||||
|
long manualTerminateCnt = 0;
|
||||||
long start_time = System.currentTimeMillis();
|
long start_time = System.currentTimeMillis();
|
||||||
while (System.currentTimeMillis() < start_time + (timeMax * 1000)) {
|
while (System.currentTimeMillis() < start_time + (timeMax * 1000)) {
|
||||||
count++;
|
count++;
|
||||||
|
|
||||||
StopAtExit thread = new StopAtExit();
|
// Use my own ThreadGroup so the thread count is known and make
|
||||||
|
// it a daemon ThreadGroup so it is automatically destroyed when
|
||||||
|
// the thread is terminated.
|
||||||
|
ThreadGroup myTG = new ThreadGroup("myTG-" + count);
|
||||||
|
myTG.setDaemon(true);
|
||||||
|
StopAtExit thread = new StopAtExit(myTG, null);
|
||||||
thread.start();
|
thread.start();
|
||||||
try {
|
try {
|
||||||
// Wait for the worker thread to get going.
|
// Wait for the worker thread to get going.
|
||||||
@ -107,9 +120,52 @@ public class StopAtExit extends Thread {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new Error("Unexpected: " + e);
|
throw new Error("Unexpected: " + e);
|
||||||
}
|
}
|
||||||
|
// This stop() call happens after the join() so it should do
|
||||||
|
// nothing, but let's make sure.
|
||||||
thread.stop();
|
thread.stop();
|
||||||
|
|
||||||
|
if (myTG.activeCount() != 0) {
|
||||||
|
// If the ThreadGroup still has a count, then the thread
|
||||||
|
// received the async exception while in exit() so we need
|
||||||
|
// to do a manual terminate.
|
||||||
|
manualTerminateCnt++;
|
||||||
|
try {
|
||||||
|
threadTerminated(myTG, thread);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new Error("threadTerminated() threw unexpected: " + e);
|
||||||
|
}
|
||||||
|
int activeCount = myTG.activeCount();
|
||||||
|
if (activeCount != 0) {
|
||||||
|
throw new Error("threadTerminated() did not clean up " +
|
||||||
|
"worker thread: count=" + activeCount);
|
||||||
|
}
|
||||||
|
if (!myTG.isDestroyed()) {
|
||||||
|
throw new Error("threadTerminated() did not destroy " +
|
||||||
|
myTG.getName());
|
||||||
|
}
|
||||||
|
} else if (!myTG.isDestroyed()) {
|
||||||
|
// If the ThreadGroup does not have a count, but is not
|
||||||
|
// yet destroyed, then the thread received the async
|
||||||
|
// exception while the thread was in the later stages of
|
||||||
|
// its threadTerminated() call so we need to do a manual
|
||||||
|
// destroy.
|
||||||
|
manualDestroyCnt++;
|
||||||
|
try {
|
||||||
|
myTG.destroy();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new Error("myTG.destroy() threw unexpected: " + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (manualDestroyCnt != 0) {
|
||||||
|
System.out.println("Manually destroyed ThreadGroup " +
|
||||||
|
manualDestroyCnt + " times.");
|
||||||
|
}
|
||||||
|
if (manualTerminateCnt != 0) {
|
||||||
|
System.out.println("Manually terminated Thread " +
|
||||||
|
manualTerminateCnt + " times.");
|
||||||
|
}
|
||||||
System.out.println("Executed " + count + " loops in " + timeMax +
|
System.out.println("Executed " + count + " loops in " + timeMax +
|
||||||
" seconds.");
|
" seconds.");
|
||||||
|
|
||||||
@ -120,6 +176,13 @@ public class StopAtExit extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void threadTerminated(ThreadGroup group, Thread thread) throws Exception {
|
||||||
|
// ThreadGroup.threadTerminated() is package private:
|
||||||
|
Method method = ThreadGroup.class.getDeclaredMethod("threadTerminated", Thread.class);
|
||||||
|
method.setAccessible(true);
|
||||||
|
method.invoke(group, thread);
|
||||||
|
}
|
||||||
|
|
||||||
public static void usage() {
|
public static void usage() {
|
||||||
System.err.println("Usage: " + PROG_NAME + " [time_max]");
|
System.err.println("Usage: " + PROG_NAME + " [time_max]");
|
||||||
System.err.println("where:");
|
System.err.println("where:");
|
||||||
|
Loading…
Reference in New Issue
Block a user