From f9500d93ca28b5b2d50b9fa9b939abe68a7acc67 Mon Sep 17 00:00:00 2001 From: Egor Baranov Date: Sat, 24 Oct 2026 07:45:58 +0300 Subject: [PATCH 1/2] Custom Thread Groups - Stop idle pool threads of Arrivals Thread Group --- .../threads/arrivals/ArrivalsThreadGroup.java | 15 ++++++- .../arrivals/ArrivalsThreadGroupTest.java | 45 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/plugins/casutg/src/main/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroup.java b/plugins/casutg/src/main/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroup.java index ecdf5a54e..4631c8d98 100644 --- a/plugins/casutg/src/main/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroup.java +++ b/plugins/casutg/src/main/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroup.java @@ -71,14 +71,25 @@ public void stop() { @Override public void tellThreadsToStop() { super.tellThreadsToStop(); + // stopThread() looks only through active threads, so wake idle pool threads directly for (DynamicThread thread : poolThreads) { - stopThread(thread.getThreadName(), true); + thread.stop(); + thread.interruptOSThread(); } } @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(); } @@ -102,6 +113,8 @@ public boolean movedToPool(DynamicThread thread) { log.debug("Interrupted", e); } } + // releasedPoolThread() has already removed it, but a thread woken by stop/interrupt has not + poolThreads.remove(thread); ThreadCountsAccessor.incrNumberOfThreads(); return running; } diff --git a/plugins/casutg/src/test/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroupTest.java b/plugins/casutg/src/test/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroupTest.java index 841d26875..b38500529 100644 --- a/plugins/casutg/src/test/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroupTest.java +++ b/plugins/casutg/src/test/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroupTest.java @@ -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; + } +} From fa58827e94e7cf06a81682f23da1fad67ba3e3b7 Mon Sep 17 00:00:00 2001 From: Egor Baranov Date: Sun, 25 Oct 2026 12:42:15 +0300 Subject: [PATCH 2/2] Custom Thread Groups - Guarded pool wait, wake idle threads with notify on graceful stop --- .../threads/arrivals/ArrivalsThreadGroup.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/plugins/casutg/src/main/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroup.java b/plugins/casutg/src/main/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroup.java index 4631c8d98..0b7c35a80 100644 --- a/plugins/casutg/src/main/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroup.java +++ b/plugins/casutg/src/main/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroup.java @@ -71,10 +71,14 @@ public void stop() { @Override public void tellThreadsToStop() { super.tellThreadsToStop(); - // stopThread() looks only through active threads, so wake idle pool threads directly + // 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) { thread.stop(); - thread.interruptOSThread(); + //noinspection SynchronizationOnLocalVariableOrMethodParameter + synchronized (thread) { + thread.notify(); + } } } @@ -108,12 +112,15 @@ 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); } } - // releasedPoolThread() has already removed it, but a thread woken by stop/interrupt has not poolThreads.remove(thread); ThreadCountsAccessor.incrNumberOfThreads(); return running;