Skip to content

ARTEMIS-6085 Fix NPE from race in MQTTStateManager.getSessionState#6480

Merged
jbertram merged 1 commit into
apache:mainfrom
k-krawczyk:ARTEMIS-6085
Jun 1, 2026
Merged

ARTEMIS-6085 Fix NPE from race in MQTTStateManager.getSessionState#6480
jbertram merged 1 commit into
apache:mainfrom
k-krawczyk:ARTEMIS-6085

Conversation

@k-krawczyk

Copy link
Copy Markdown
Contributor

What

Fixes a NullPointerException thrown from MQTTProtocolHandler#handleLinkStealing() (getSessionState(clientID).getSession()).

Root cause

MQTTStateManager.getSessionState(String) used a non-atomic containsKey() + get() on a ConcurrentHashMap. During link stealing (two connections sharing a client ID), removeSessionState() can run on another thread between the two calls, so get() returns null and the caller dereferences it with .getSession().

The race is still present on main.

Fix

Replace the containsKey()/get()/put() block with an atomic computeIfAbsent() — a get-or-create that can never return null. Behaviour is otherwise unchanged.

Test

Adds MQTTStateManagerTest, a concurrency regression test that runs getSessionState() against concurrent removeSessionState() on the same client ID and asserts a non-null result. It fails (NPE/null) on the pre-fix code in ~1s and passes after the fix. The full artemis-mqtt-protocol module suite is green (12/12).

JIRA: https://issues.apache.org/jira/browse/ARTEMIS-6085

getSessionState() used a non-atomic containsKey()/get() pair on a
ConcurrentHashMap. During link stealing (two connections sharing a
client ID) removeSessionState() can run between the two calls, so get()
returns null and handleLinkStealing() dereferences it via .getSession(),
throwing a NullPointerException.

Use computeIfAbsent() for an atomic get-or-create that never returns null.
Adds a concurrency regression test that reproduces the NPE.
@jbertram

jbertram commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

@k-krawczyk many thanks for the great contribution!

@jbertram jbertram merged commit cd396d0 into apache:main Jun 1, 2026
6 checks passed
@k-krawczyk k-krawczyk deleted the ARTEMIS-6085 branch June 1, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants