Skip to content

Update VirtualThread client and server correctness fixes - #2866

Open
mattrpav wants to merge 4 commits into
apache:mainfrom
mattrpav:amq-virtual-thread-per-runner
Open

mattrpav wants to merge 4 commits into
apache:mainfrom
mattrpav:amq-virtual-thread-per-runner

Conversation

@mattrpav

@mattrpav mattrpav commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Background

First iteration of Virtual Thread task runner created a VT per-wakeup. This moves to create a VT per-Task and is more inline with how the other runners work. This is specifically shows improvement for fast ack'n non-persistent consumers.

  • Update Virtual Thread Task Runner to be VT-per-task
  • Fix client-side usage of VT
  • Add tests

ref: client-side fixes was in previous PR [#2766]

@mattrpav mattrpav self-assigned this Nov 14, 2026
@mattrpav
mattrpav force-pushed the amq-virtual-thread-per-runner branch from 7d067d4 to 6c733f4 Compare November 14, 2026 19:05
@mattrpav
mattrpav force-pushed the amq-virtual-thread-per-runner branch from 6c733f4 to 62abafa Compare November 14, 2026 19:47
@mattrpav mattrpav changed the title Update VirtualThreadTaskRunner to optimize and have one virtual thread per-task Update VirtualThread client and server correctness fixes Nov 14, 2026
@jeanouii

jeanouii commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

2 items to address (runner termination on task failure, and explicit false vs the system property default), the rest looks good.

@mattrpav
mattrpav force-pushed the amq-virtual-thread-per-runner branch from 62abafa to 8a13d26 Compare December 3, 2026 00:17
@mattrpav mattrpav moved this from Backlog to In progress in Apache ActiveMQ v6.5.0 Dec 3, 2026
@mattrpav mattrpav moved this from In progress to Done in Apache ActiveMQ v6.5.0 Dec 3, 2026
@mattrpav mattrpav moved this from Done to In review in Apache ActiveMQ v6.5.0 Dec 3, 2026
jeanouii
jeanouii previously approved these changes Dec 3, 2026

@jeanouii jeanouii left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks Matt — both items addressed

Thread.currentThread().interrupt();
} catch (Throwable t) {
LOG.error("Virtual Thread task runner terminating on unexpected error: {}", task, t);
throw t;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking at the left side I think this can behave differently.
throw t will terminate the thread right? So the next wakup() won't do anything.

Previously the runner would requeue after a failure and therefor recover if I'm correct.

private final AtomicBoolean initDone = new AtomicBoolean(false);
private int maxThreadPoolSize = getDefaultMaximumPoolSize();
private RejectedExecutionHandler rejectedTaskHandler = null;
/** system property that turns on virtual thread task runners for every factory that is not told otherwise */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Line 112: similar to another PR for failover it would not work. You would need a Boolean to have 3 states

@jeanouii

jeanouii commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

LGTM

@cshannon

cshannon commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

I ran this through an AI audit for a sanity check and it reported 4 things to look into:

Reviewed 8a13d26

[P1] Closing an async session leaves its virtual dispatch thread parked

VirtualThreadTaskRunner.java:149–153
(

lock.lock();
try {
while (!pending.get() && !shutdown) {
wakeupCondition.await();
}
)

ActiveMQSession.dispose() closes the dispatch queue and removes the session without shutting down its runner. The parked worker then misses the connection’s session-stop pass, and orderly executor shutdown does not interrupt it. Repeatedly opening and closing async sessions can leave one parked worker per session that started dispatch. Shut down the runner during session disposal.

[P2] A listener that restores its interrupt flag can permanently stall dispatch

VirtualThreadTaskRunner.java:151–159
(

while (!pending.get() && !shutdown) {
wakeupCondition.await();
}
} finally {
lock.unlock();
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
)

If a message listener restores its interrupt flag and returns, the next idle await() throws and the worker exits. Subsequent wakeups signal a condition with no worker to receive them. Keep the runner available for future deliveries unless shutdown was requested.

[P2] The global virtual-thread flag can starve sessions on an injected executor

TaskRunnerFactory.java:177–183 (

init();
ExecutorService executor = executorRef.get();
if (executor != null) {
if (isVirtualThreadTaskRunner()) {
return createVirtualThreadTaskRunner(executor, task, maxIterationsPerRun);
} else {
return new PooledTaskRunner(executor, task, maxIterationsPerRun);
)

The effective flag now selects the persistent runner even when setExecutor() supplied the executor. With an injected single-thread pool, the first session holds the only worker even while idle, so a second session cannot dispatch. Preserve pooled runners for injected executors unless thread-per-task execution is explicitly supported.

[P2] The field type change breaks deserialization of existing connection factories

ActiveMQConnectionFactory.java:163–164
(

// null follows the system property, true or false is an explicit choice
private Boolean useVirtualThreadTaskRunner;
)

Changing this instance field from boolean to Boolean changes the computed serialVersionUID. ActiveMQConnectionFactory implements Externalizable and has no explicit UID, so an older stream is rejected before readExternal() can read its properties. Declare the pre-change UID and test deserialization of a fixture written by the previous version.

@cshannon

cshannon commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

@jeanouii - For the last report finding, about serialization, I wouldn't think it would be very common to serialize a Connection factory but it does support the Externalization interface, but I suppose it's possible.

@cshannon

cshannon commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

I pushed up a proposed commit with the fixes and tests: https://github.com/cshannon/activemq/tree/fix/pr-2866-review

525e322 fixes finding 1 (P1): closing an async JMS session leaves its dispatch thread parked. It shuts down the session’s runner on close, covering both the pre-existing dedicated-thread leak and the virtual-thread leak introduced by this PR

The other three findings are fixed in bf487b1.

mattrpav and others added 4 commits December 4, 2026 09:31
…ixes

One long-lived virtual thread per TaskRunner that parks between work,
replacing the thread per wakeup submission, with termination logged on
unexpected errors. No-arg shutdown() waits for the running iteration,
matching the pooled and dedicated runners.

Client-side fixes: load the java21 classes through this artifact's own
class loader so client factories work without setThreadClassLoader,
honor the system property in task runner selection the same as in
executor setup, create the failover reconnect task on first use so the
virtual thread setting reaches it, propagate the connection factory
flag to the failover transport, and keep the task runner settings
across serialization and JNDI.

Tests: task runner behavior (thread identity, wakeup delivery, shutdown
bounds and contract, failure logging, thread release) run through both
the factory flag and the system property, the client configuration
precedence matrix, session dispatch and failover reconnect on both
thread types.
Close the dispatch queue and detach its runner under the same lock used
for runner creation. Signal shutdown without waiting indefinitely while
session disposal holds locks that a listener may need.

Verify that an idle dedicated runner exits when its session closes,
while the connection is still open.

(cherry picked from commit 525e322)
Stop session runners on close and keep dispatch alive when a listener
restores its interrupt status. Use pooled runners with injected executors
so idle sessions do not monopolize bounded pools.

Preserve the connection factory serialization UID and cover deserialization
with a fixture from before the virtual thread changes.

(cherry picked from commit bf487b1)
@mattrpav

mattrpav commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

@cshannon I cherry-picked your commits to the branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

3 participants