You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@artemis.apache.org by "Jakub Kosiorek (Jira)" <ji...@apache.org> on 2026/10/16 13:00:00 UTC

[jira] [Created] (ARTEMIS-6309) listConsumers management operation intermittently fails with NPE: Cannot invoke "ServerSession.getName()" because "this.session" is null — race between ServerConsumerImpl.close() nulling session and unsynchronized reads in ConsumerView/toJSONObject

Jakub Kosiorek created ARTEMIS-6309:
---------------------------------------

             Summary: listConsumers management operation intermittently fails with NPE: Cannot invoke "ServerSession.getName()" because "this.session" is null — race between ServerConsumerImpl.close() nulling session and unsynchronized reads in ConsumerView/toJSONObject
                 Key: ARTEMIS-6309
                 URL: https://issues.apache.org/jira/browse/ARTEMIS-6309
             Project: Artemis
          Issue Type: Bug
          Components: Broker, Management
    Affects Versions: 2.57.0
            Reporter: Jakub Kosiorek


h2. Environment
 * Apache ActiveMQ Artemis broker *2.57.0* (Linux, Docker; HA pair)
 * Client: {{artemis-jakarta-client}} 2.56.0, JMS management messages sent to {{{}activemq.management{}}}, resource {{broker}}
 * Trigger context: a monitoring application polls {{listConsumers}} (options {{{}"field":"address","operation":"CONTAINS","value":"<prefix>"{}}}, page=-1, pageSize=-1) every few seconds while JMS consumers on the broker normally connect and disconnect (rolling deployments, short-lived consumers)

h2. Symptoms

Intermittently the management reply carries the JMS property {{_AMQ_OperationSucceeded=false}} and a plain-text body instead of JSON:
{code:java}
Cannot invoke "org.apache.activemq.artemis.core.server.ServerSession.getName()" because "this.session" is null
{code}
No JSON is returned at all, so the *entire result set is lost* for that invocation — not just the affected consumer row.
h2. Reproduction (intermittent race; needs a loop to trigger reliably)
 # Start a broker; open a JMS connection to {{{}activemq.management{}}}.
 # Thread A: in a loop, send management messages invoking operation {{listConsumers}} on resource {{broker}} with options {{{}"field":"address","operation":"CONTAINS","value":"<any>"{}}}, -1, -1 (or simply {{listConsumers(null, -1, -1)}} / {{{}listAllConsumersAsJSON(){}}}).
 # Thread B: on a separate connection, in a loop, create a consumer (e.g. {{Session.createConsumer}} / {{createDurableConsumer}} on any address) and immediately close it (and the session/connection).
 # Within seconds-to-minutes, some {{listConsumers}} replies return {{_AMQ_OperationSucceeded=false}} with the NPE text above. Check {{artemis.log}} for the corresponding server-side stack trace ({{{}...ServerConsumerImpl.getSessionID/getSessionName{}}}).

Repro data from our side: 8 threads churning ~650k consumers over 10 minutes while one management thread probes every 20 ms → at least 7 failed replies with the message above. Same-family races were observed on the same broker for {{listQueues}} ({{{}"q" is null{}}}) and {{deleteQueue}} ({{{}"this.callback" is null{}}}).
h2. Code analysis (2.57.0)

Both NPE sites dereference the non-final, asynchronously nulled {{session}} field of {{{}org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl{}}}:
{code:java}
// ServerConsumerImpl.java:375
public String getSessionID()   { return this.session.getName(); }
// ServerConsumerImpl.java:1552
public String getSessionName() { return this.session.getName(); }
{code}
The field is nulled by {{close(boolean)}} in a scheduled executor task ({{{}ServerConsumerImpl.java:649{}}}: {{{}session = null;{}}}), i.e. asynchronously with respect to the consumer's removal from the session/management views.

Call sites that crash:
 * {{ActiveMQServerControlImpl.listConsumers(String,int,int)}} (:2574) → {{ConsumerView.toJson(ServerConsumer)}} ({{{}ConsumerView:52{}}}):

{code:java}
ServerSession session = server.getSessionByID(consumer.getSessionID()); // NPE here — inside the guard expression
//if session is not available then consumer is not valid state - ignore
if (session == null) { return null; }                                    // ...guard is applied one line too late
...
.add(ConsumerField.SESSION.getName(), toString(consumer.getSessionName())) // second unguarded dereference (:62)
{code}
 * {{ActiveMQServerControlImpl.listAllConsumersAsJSON()}} (:2771) → private {{toJSONObject(ServerConsumer)}} (:2798) → {{consumer.getSessionID()}} at :2804 — no guard at all.
 * Queue-level {{QueueControlImpl.listConsumersAsJSON()}} (:1969) → {{serverConsumer.getSessionID()}} — same exposure.

Note the intent in {{ConsumerView}} ("if session is not available ... ignore"): the code already wants to skip consumers whose session is gone — it just dereferences *before* checking.

This looks like a *regression of the fix for* ARTEMIS-1526 ("ActiveMQServerControl.listConsumers() can fail with NullPointer", fixed in 2.5.0): the same deref-then-guard pattern in {{ConsumerView.toJson}} is back.
h2. Suggested fix (one of)
 * Capture {{ServerConsumerImpl.session}} into a local variable once and null-check before use in {{{}getSessionID(){}}}/{{{}getSessionName(){}}} (cheapest, keeps semantics).
 * In {{{}ConsumerView.toJson{}}}, resolve the session via an immutable session id captured at consumer creation instead of {{{}consumer.getSessionID(){}}}, then apply the existing {{session == null}} skip — i.e. make the guard usable.
 * Alternatively, do not null {{session}} in {{close()}} (keep a final reference) since the object is discarded anyway — review memory implications.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@artemis.apache.org
For additional commands, e-mail: issues-help@artemis.apache.org