Fix JMS consumer leak + stuck dequeue on coord-DB latency#2579
Merged
Conversation
When a ScheduledMessageForwardingProcessor task is paused via becameUnresponsive -> pauseMessageProcessorTemporarily -> cleanupLocalResources, the cleanup nulls each JmsConsumer's connection/session/consumer fields but does not flip isAlive. Quartz pauseJob stops future triggers but does not interrupt the in-flight execute() of an already-running instance. That thread proceeds into JmsConsumer.receive(), passes the isAlive guard, and calls checkAndTryConnect() which sees the just-nulled fields and runs reconnect() -> JmsStore.getConsumer() -> a brand-new Connection/Session/MessageConsumer is opened on the broker. The Quartz job is paused immediately after, so this fresh JMS connection becomes orphaned: it is owned by a paused-task's JmsConsumer and nothing closes it. After the next ownership reshuffle, the new owner opens its own consumer too, and the orphaned one remains - producing the ConsumerCount climb above member.count on the broker side. On the resume path the same reused JmsConsumer instance also exhibits a stuck dequeue: the broker biases against the just-reconnected subscriber and receive() returns null indefinitely. Fix: - JmsConsumer.cleanup(): set isAlive=false as the first line, atomic with field nulling. Any concurrent receive() arriving after cleanup starts bails at the isAlive guard instead of reconnecting. - ForwardingService.tryToDispatchToEndpoint(): when messageConsumer is null or no longer alive, rebuild it via setMessageConsumer() before dispatch. This appends a brand-new JmsConsumer instance to the processor's list with a fresh consumerId on the broker side, which the broker treats as a new subscriber without the prior consumer's churn history. fixes: wso2/product-integrator-mi#4616 Fix ScheduledMessageProcessor fan-out orphan consumers on pause/resume ScheduledMessageProcessor.start() creates memberCount ForwardingService instances in a loop but overwrites a single `task` field each iteration, so terminate() reaches only the last one. The other N-1 keep isTerminated=false. During pauseAllLocallyRunningTasks, cleanupLocalResources() closes every consumer at once while those un-terminated workers are still live; they re-enter execute(), hit the re-init guard, and reconnect - appending a fresh consumer after the cleanup pass that nothing ever closes. That orphan is the broker ConsumerCount climb above member.count Fix: - ScheduledMessageProcessor: track every memberCount ForwardingService in a List; terminate() iterates and stops ALL of them, not just the last. - ForwardingService / FailoverForwardingService: gate the re-init reconnect guard on !isTerminated, so a terminated worker fired during the pause cascade does not rebuild a consumer. - ScheduledMessageProcessor.resumeRemotely(): reset isTerminated on every tracked instance, so the next fire after resume reconnects normally (the same instances are reused across pause/resume via the ntask static task map; without this the !isTerminated guard would brick every slot on resume). Fix JMS consumer cleanup during processor pause recovery Preserve broker reconnect retries while marking teardown consumers dead to avoid orphan JMS connections. Fixes: wso2/product-integrator-mi#4616
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (5)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@modules/core/src/main/java/org/apache/synapse/message/processor/impl/sampler/SamplingService.java`:
- Around line 154-174: The consumer rebuild in SamplingService is happening
before the messageProcessor.isDeactivated() active check, which can create a new
consumer during a paused tick and then immediately skip using it. Move the
messageConsumer null/alive rebuild logic inside the active branch guarded by
messageProcessor.isDeactivated(), keeping the existing setMessageConsumer(),
log.error, and log.warn behavior intact so rebuilds only happen when the sampler
is active.
In
`@modules/core/src/main/java/org/apache/synapse/message/store/impl/jms/JmsConsumer.java`:
- Around line 176-182: The isAlive() check in JmsConsumer can race with
cleanup() because it reads the session field multiple times. Update
JmsConsumer.isAlive() to capture session into a local snapshot once, check that
snapshot for null, and use that same reference for session.getAcknowledgeMode()
so concurrent cleanup cannot invalidate the check mid-execution.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1f9c83f3-e11f-4a2f-a6ff-dfc1dfb5a066
📒 Files selected for processing (5)
modules/core/src/main/java/org/apache/synapse/message/processor/impl/ScheduledMessageProcessor.javamodules/core/src/main/java/org/apache/synapse/message/processor/impl/failover/FailoverForwardingService.javamodules/core/src/main/java/org/apache/synapse/message/processor/impl/forwarder/ForwardingService.javamodules/core/src/main/java/org/apache/synapse/message/processor/impl/sampler/SamplingService.javamodules/core/src/main/java/org/apache/synapse/message/store/impl/jms/JmsConsumer.java
DedunuKarunarathne
approved these changes
Jul 9, 2026
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.
Purpose
When a ScheduledMessageForwardingProcessor task is paused via becameUnresponsive -> pauseMessageProcessorTemporarily -> cleanupLocalResources, the cleanup nulls each JmsConsumer's connection/session/consumer fields but does not flip isAlive. Quartz pauseJob stops future triggers but does not interrupt the in-flight execute() of an already-running instance. That thread proceeds into JmsConsumer.receive(), passes the isAlive guard, and calls checkAndTryConnect() which sees the just-nulled fields and runs reconnect() -> JmsStore.getConsumer() -> a brand-new Connection/Session/MessageConsumer is opened on the broker. The Quartz job is paused immediately after, so this fresh JMS connection becomes orphaned: it is owned by a paused-task's JmsConsumer and nothing closes it. After the next ownership reshuffle, the new owner opens its own consumer too, and the orphaned one remains - producing the ConsumerCount climb above member.count on the broker side. On the resume path the same reused JmsConsumer instance also exhibits a stuck dequeue: the broker biases against the just-reconnected subscriber and receive() returns null indefinitely.
Fix:
fixes: wso2/product-integrator-mi#4616
Fix ScheduledMessageProcessor fan-out orphan consumers on pause/resume
ScheduledMessageProcessor.start() creates memberCount ForwardingService instances in a loop but overwrites a single
taskfield each iteration, so terminate() reaches only the last one. The other N-1 keep isTerminated=false. During pauseAllLocallyRunningTasks, cleanupLocalResources() closes every consumer at once while those un-terminated workers are still live; they re-enter execute(), hit the re-init guard, and reconnect - appending a fresh consumer after the cleanup pass that nothing ever closes. That orphan is the broker ConsumerCount climb above member.countFix:
Fix JMS consumer cleanup during processor pause recovery
Preserve broker reconnect retries while marking teardown consumers dead to avoid orphan JMS connections.
Fixes: wso2/product-integrator-mi#4616