Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,29 @@ public void stop() {
@Override
public void tellThreadsToStop() {
super.tellThreadsToStop();
// stopThread() looks only through active threads, so wake idle pool threads directly.
// No OS interrupt here: a thread being released concurrently would get it mid-sample
for (DynamicThread thread : poolThreads) {
stopThread(thread.getThreadName(), true);
thread.stop();
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (thread) {
thread.notify();
}
}
}

@Override
public boolean verifyThreadsStopped() {
boolean parent = super.verifyThreadsStopped();
for (DynamicThread thread : poolThreads) {
if (thread.getOSThread() != null) {
try {
thread.getOSThread().join(WAIT_TO_DIE);
} catch (InterruptedException e) {
log.warn("Interrupted", e);
}
}
}
log.info("Verify shutdown thread counts: " + threads.size() + "/" + poolThreads.size());
return parent && poolThreads.isEmpty();
}
Expand All @@ -97,11 +112,16 @@ public boolean movedToPool(DynamicThread thread) {
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (thread) {
try {
thread.wait();
// releasedPoolThread() removes the thread before notify, so the guard also covers
// a release or stop that comes before this wait, and spurious wakeups
while (running && !thread.isStopping() && poolThreads.contains(thread)) {
thread.wait();
}
} catch (InterruptedException e) {
log.debug("Interrupted", e);
}
}
poolThreads.remove(thread);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cleanup is correct, but it works around the real defect a few lines above: thread.wait() runs once with no condition and no loop, so it's exposed to both lost wakeups (notify arrives before the thread reaches wait()) and spurious wakeups (the thread returns while still in poolThreads, which is what this line patches over).

Could you make it a guarded wait?

synchronized (thread) {
    try {
        while (running && !thread.isStopping() && poolThreads.contains(thread)) {
            thread.wait();
        }
    } catch (InterruptedException e) {
        log.debug("Interrupted", e);
    }
}
poolThreads.remove(thread);
  • releasedPoolThread() removes the thread from poolThreads before notifying, so a normal release exits the loop.
  • A graceful stop sets the flags and then notifies under the same monitor, so the waiter sees them (the monitor gives the needed happens-before), even if it hadn't started waiting yet.
  • stop() still breaks out via InterruptedException.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in fa58827: guarded wait exactly as suggested, with poolThreads.remove(thread) after the loop.

ThreadCountsAccessor.incrNumberOfThreads();
return running;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,48 @@ public void testFlowRefael() throws IOException {
atg.tellThreadsToStop();
atg.verifyThreadsStopped();
}
}

@Test
public void testStopWithIdlePoolThread() throws Exception {
ArrivalsThreadGroupEmul atg = startWithIdlePoolThread();
try {
atg.tellThreadsToStop();
assertTrue(atg.verifyThreadsStopped());
} finally {
atg.stop();
atg.testEnded();
}
}

@Test
public void testStopNowWithIdlePoolThread() throws Exception {
ArrivalsThreadGroupEmul atg = startWithIdlePoolThread();
try {
atg.stop();
assertTrue(atg.verifyThreadsStopped());
} finally {
atg.testEnded();
}
}

private static ArrivalsThreadGroupEmul startWithIdlePoolThread() throws InterruptedException {
ArrivalsThreadGroupEmul atg = new ArrivalsThreadGroupEmul();
atg.setName("TEST");
atg.setTargetLevel("1");
atg.setHold("60");
ListedHashTree tree = new ListedHashTree();
TestAction pauser = new TestAction();
pauser.setAction(TestAction.PAUSE);
pauser.setTarget(TestAction.THREAD);
pauser.setDuration("100");
tree.add(atg, pauser);
atg.testStarted();
atg.start(0, null, tree, null);
long deadline = System.currentTimeMillis() + 5000;
while (atg.poolThreads.isEmpty()) {
assertTrue("no thread has moved to the pool", System.currentTimeMillis() < deadline);
Thread.sleep(10);
}
return atg;
}
}
Loading