fix: prevent idle-close from shutting down in-use consumer, and avoid NPE in per-user pool aspect - #708
Open
yyqdbngt wants to merge 1 commit into
Open
Conversation
… NPE in per-user pool aspect - AutoCloseConsumerWrapper: acquire the consumer and increment an in-use counter under the same lock, so the idle-close task can never shut down a consumer that a thread is about to use or is already using - MessageServiceImpl: release the consumer in finally blocks after use so the in-use counter returns to zero and idle close keeps working - MQAdminAspect: fall back to the default MQAdminExt pool when a method requires per-user isolation but no user info is present in the thread context (e.g. background/scheduled tasks), instead of throwing NPE
yyqdbngt
force-pushed
the
fix/consumer-race-and-aspect-npe
branch
from
July 31, 2026 15:33
ff16da4 to
d73fec3
Compare
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.
Motivation
Two concurrency / robustness bugs found while reviewing the message-query and admin-pool code:
Idle-close task could shut down a consumer while it is still in use
AutoCloseConsumerWrapperreturns the sharedDefaultMQPullConsumerand a background task closes it after 60s of idleness. The check-and-close runs on a separate scheduler thread with no knowledge of threads currently using the consumer, so a long-running query (e.g. scanning many queues inqueryMessageByTopic) could have the consumer shut down mid-use, making the pull fail with an exception. Also,getConsumerhanded out the consumer after releasing the lock, so the close could slip in between the null-check and the return.Fix: track an in-use counter.
getConsumernow acquires the consumer and increments the counter under the same lock used byclose(), and addsreleaseConsumer()which the callers invoke infinallyblocks. The idle-close task only proceeds when the counter is zero.MQAdminAspectcould NPE when per-user isolation is enabled but no user is in contextWhen
loginRequiredis on and the auth mode is notfile, every non-whitelistedMQAdminExtmethod tries to borrow a per-userMQAdminExtusingcurrentUserInfo.getUsername(). On background/scheduled threads there is no user inUserInfoContext, so this throws aNullPointerException. The aspect now falls back to the default pool when no user info is present.Verification
mvn compiler:compilepasses (BUILD SUCCESS).Diff
3 files changed, +50 / -20.