ARTEMIS-6085 Fix NPE from race in MQTTStateManager.getSessionState#6480
Open
k-krawczyk wants to merge 1 commit into
Open
ARTEMIS-6085 Fix NPE from race in MQTTStateManager.getSessionState#6480k-krawczyk wants to merge 1 commit into
k-krawczyk wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes a
NullPointerExceptionthrown fromMQTTProtocolHandler#handleLinkStealing()(getSessionState(clientID).getSession()).Root cause
MQTTStateManager.getSessionState(String)used a non-atomiccontainsKey()+get()on aConcurrentHashMap. During link stealing (two connections sharing a client ID),removeSessionState()can run on another thread between the two calls, soget()returnsnulland the caller dereferences it with.getSession().The race is still present on
main.Fix
Replace the
containsKey()/get()/put()block with an atomiccomputeIfAbsent()— a get-or-create that can never returnnull. Behaviour is otherwise unchanged.Test
Adds
MQTTStateManagerTest, a concurrency regression test that runsgetSessionState()against concurrentremoveSessionState()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 fullartemis-mqtt-protocolmodule suite is green (12/12).JIRA: https://issues.apache.org/jira/browse/ARTEMIS-6085