Skip to content

Fix JMS consumer leak + stuck dequeue on coord-DB latency#2579

Merged
malakaganga merged 1 commit into
wso2:masterfrom
malakaganga:fix_msmp_taskmon
Jul 9, 2026
Merged

Fix JMS consumer leak + stuck dequeue on coord-DB latency#2579
malakaganga merged 1 commit into
wso2:masterfrom
malakaganga:fix_msmp_taskmon

Conversation

@malakaganga

Copy link
Copy Markdown
Contributor

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:

  • 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

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
@malakaganga malakaganga requested a review from chanikag as a code owner July 9, 2026 11:43
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1f9c83f3-e11f-4a2f-a6ff-dfc1dfb5a066

📥 Commits

Reviewing files that changed from the base of the PR and between 02df111 and 61d77dd.

📒 Files selected for processing (5)
  • modules/core/src/main/java/org/apache/synapse/message/processor/impl/ScheduledMessageProcessor.java
  • modules/core/src/main/java/org/apache/synapse/message/processor/impl/failover/FailoverForwardingService.java
  • modules/core/src/main/java/org/apache/synapse/message/processor/impl/forwarder/ForwardingService.java
  • modules/core/src/main/java/org/apache/synapse/message/processor/impl/sampler/SamplingService.java
  • modules/core/src/main/java/org/apache/synapse/message/store/impl/jms/JmsConsumer.java
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 02df111 and 61d77dd.

📒 Files selected for processing (5)
  • modules/core/src/main/java/org/apache/synapse/message/processor/impl/ScheduledMessageProcessor.java
  • modules/core/src/main/java/org/apache/synapse/message/processor/impl/failover/FailoverForwardingService.java
  • modules/core/src/main/java/org/apache/synapse/message/processor/impl/forwarder/ForwardingService.java
  • modules/core/src/main/java/org/apache/synapse/message/processor/impl/sampler/SamplingService.java
  • modules/core/src/main/java/org/apache/synapse/message/store/impl/jms/JmsConsumer.java

@malakaganga malakaganga merged commit c86e33f into wso2:master Jul 9, 2026
2 of 3 checks passed
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.

[MI 4.0.0] Increasing the consumer count at the ActiveMQ

3 participants