Skip to content

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

Open
k-krawczyk wants to merge 1 commit into
apache:mainfrom
k-krawczyk:ARTEMIS-6085
Open

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

Conversation

@k-krawczyk
Copy link
Copy Markdown

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.
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.

1 participant