azure-sdk-for-java: [BUG] Service Bus SDK hangs processing multiple sessions
Describe the bug
While using com.azure:azure-messaging-servicebus on version 7.15.0-beta.3 and 7.14.4 to process messages from 2 or more concurrent
sessions, the threads processing work on other non-idle sessions are interrupted when a session idles out.
Under some circumstances that interrupt appears to break the non-idle receiving session causing it to no longer process messages.
This is evidenced by the log of the example showing that it is producing but not consuming messages. 7.14.4 does appear
to recover eventually but still interrupts unrelated work.
Exception or Stack Trace
Complete log for this trace can be found here
11:02:18.092 [receiver-1-2] ERROR c.t.a.s.bug.TriggerWorkInterrupt - Got Error: USER_CALLBACK thrive-jgoodwin-test.servicebus.windows.net test-topic-1/subscriptions/Jon-Goodwins-MacBook-Pro-LOCAL
com.azure.messaging.servicebus.ServiceBusException: java.lang.InterruptedException
at com.azure.messaging.servicebus.ServiceBusProcessorClient$1.onNext(ServiceBusProcessorClient.java:424)
at com.azure.messaging.servicebus.ServiceBusProcessorClient$1.onNext(ServiceBusProcessorClient.java:398)
at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.runAsync(FluxPublishOn.java:440)
at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.run(FluxPublishOn.java:527)
at reactor.core.scheduler.ImmediateScheduler$ImmediateSchedulerWorker.schedule(ImmediateScheduler.java:84)
at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.trySchedule(FluxPublishOn.java:312)
at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.onNext(FluxPublishOn.java:237)
at com.azure.messaging.servicebus.FluxTrace$TracingSubscriber.hookOnNext(FluxTrace.java:62)
at com.azure.messaging.servicebus.FluxTrace$TracingSubscriber.hookOnNext(FluxTrace.java:35)
at reactor.core.publisher.BaseSubscriber.onNext(BaseSubscriber.java:160)
at reactor.core.publisher.FluxFlatMap$FlatMapMain.tryEmit(FluxFlatMap.java:544)
at reactor.core.publisher.FluxFlatMap$FlatMapInner.onNext(FluxFlatMap.java:985)
at reactor.core.publisher.MonoFlatMapMany$FlatMapManyInner.onNext(MonoFlatMapMany.java:250)
at reactor.core.publisher.FluxDoFinally$DoFinallySubscriber.onNext(FluxDoFinally.java:113)
at reactor.core.publisher.FluxPeek$PeekSubscriber.onNext(FluxPeek.java:200)
at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:122)
at reactor.core.publisher.SerializedSubscriber.onNext(SerializedSubscriber.java:99)
at reactor.core.publisher.FluxTakeUntilOther$TakeUntilMainSubscriber.onNext(FluxTakeUntilOther.java:243)
at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.runAsync(FluxPublishOn.java:440)
at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.run(FluxPublishOn.java:527)
at reactor.core.scheduler.ImmediateScheduler$ImmediateSchedulerWorker.schedule(ImmediateScheduler.java:84)
at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.trySchedule(FluxPublishOn.java:312)
at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.onNext(FluxPublishOn.java:237)
at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:210)
at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:210)
at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.runAsync(FluxPublishOn.java:440)
at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.run(FluxPublishOn.java:527)
at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:84)
at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:37)
at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: reactor.core.Exceptions$ReactiveException: java.lang.InterruptedException
at reactor.core.Exceptions.propagate(Exceptions.java:396)
at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:91)
at reactor.core.publisher.Mono.block(Mono.java:1742)
at com.azure.messaging.servicebus.ServiceBusReceivedMessageContext.complete(ServiceBusReceivedMessageContext.java:104)
at com.thrive.azure.servicebus.bug.TriggerWorkInterrupt.processMessage(TriggerWorkInterrupt.java:28)
at com.azure.messaging.servicebus.ServiceBusProcessorClient$1.onNext(ServiceBusProcessorClient.java:422)
... 36 common frames omitted
Caused by: java.lang.InterruptedException: null
at java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1048)
at java.base/java.util.concurrent.CountDownLatch.await(CountDownLatch.java:230)
at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:87)
... 40 common frames omitted
To Reproduce
The entire example application used to reproduce this bug can be found here
- Construct a processor with a handle method that takes a long time to process some messages.
In this case messages from the
constantsession, for maximum reproducibility it should be roughly the idle timeout or longer. Then in a loop: - Send a message with a session
constant - Send a message with a session
intermittent - Wait 30s or so
You’ll be able to observe the intermittent session time out; and then the work on the constant session be interrupted. If done for long enough eventually you’ll see that messages are sent but no longer processed on either session.
Code Snippet
A full example application that triggers this bug can be found at this repository.
The processor configuration is:
new ServiceBusClientBuilder()
.fullyQualifiedNamespace(Config.FULLY_QUALIFIED_NAMESPACE)
.credential(Config.CREDENTIAL)
.sessionProcessor()
.topicName(Config.TOPIC)
.subscriptionName(Config.SUBSCRIPTION)
.disableAutoComplete()
.processMessage(TriggerWorkInterrupt::processMessage)
.processError(TriggerWorkInterrupt::processError)
.receiveMode(ServiceBusReceiveMode.PEEK_LOCK)
.subQueue(SubQueue.NONE)
.maxConcurrentSessions(2)
.maxConcurrentCalls(1)
.prefetchCount(0)
.sessionIdleTimeout(SESSION_IDLE_TIMEOUT) // 5 seconds
.buildProcessorClient();
ProcessMessage just needs to sleep for a bit to emulate some IO operation etc.
static void processMessage(ServiceBusReceivedMessageContext msg) {
try {
LOG.info("Started handling message for session: {} message: {}", msg.getMessage().getSessionId(), msg.getMessage().getMessageId());
if (msg.getMessage().getSessionId().equals("constant")) {
LOG.info("message on constant session sleeping");
Thread.sleep(SESSION_IDLE_TIMEOUT.toMillis());
}
msg.complete();
LOG.info("Finished handling message for session: {} message: {}", msg.getMessage().getSessionId(), msg.getMessage().getMessageId());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
Expected behavior
The lifecycle of a given session’s receiver should not impact other sessions. The processor should not get into a state where it is running but not processing work.
Setup (please complete the following information):
-
OS: MacOs Monterey 12.6.3 (M1 Mac pro)
-
IDE: Intellij
-
Library/Libraries: [e.g. com.azure:azure-core:1.16.0 (groupId:artifactId:version)]
com.azure:azure-messaging-servicebus:7.15.0-beta.3or7.14.4com.azure:azure-core-amqp:jar:2.9.0-beta.5(transitive from the above)com.azure:azure-identity:1.10.1
-
Java version: 17.0.6
-
App Server/Environment: Ran via intellij
-
Frameworks: None
Information Checklist
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report
- Bug Description Added
- Repro Steps Added
- Setup information Added
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 24 (16 by maintainers)
Hey @anuchandy,
Haven’t seen this since.
We did identify 1 root cause which was crossplane modifying ServiceBus resources which was causing connection drops. Since fixing that and increasing the resources of our pods, we’ve been seeing this ~5 times a day.
Appreciate the help, that should be enough info for us to dig around!
Hi @rileymichael, Unfortunately, I don’t have insight into broker internals to comment on all runtime conditions that lead to force-detach. From the SDK side, the client listens to such detach (and other transient errors) and reacts to it as mentioned earlier.
One possible factor is the Service Bus namespace’s throttling, meaning that “other” entities (queue, topic etc…) on the shared namespace will contribute to the total load on the namespace. As a note, the max-concurrent-calls (times concurrent-sessions) implicitly affect the number of calls made to the service concurrently. IIUC, it’s set to 1 for this specific topic? But similar load adds up over other entities that use the same or higher settings.
Hello @Jw0x47, thanks for testing the beta.4 and happy to hear the new implementation is passing your tests.
As of today, per our testing, there are no known issues in the beta.4 Session or Non-Session Processor based receiving. So go ahead and use it 😃
Sharing some thoughts in general - overall concurrency in theory is “max-concurrent-sessions X max-concurrent-calls-per-session”. Often this is overlooked, where there may be few cores in the host to support the desired level of concurrency (in the world of micro services, low core is norm). It’s the queueing and switching of work to deliver message to the handler takes its toll, along with no fairness guarantees by the thread pool. It’s not always clear, because the CPU doesn’t get spiked up unless part of the application’s processing is CPU-intensive. So, with low capacity, we’ll see some session processing moving along in what looks like a natural pattern while others can appear to stall, timeout. In such cases, usually heap dumps show a long queue of works waiting due to low capacity.
So, I would suggest putting an upper limit for concurrency, say, ~20 ([max-concurrent-sessions=20], [max-concurrent-sessions=5, max-concurrent-calls=4]) in one ProcessorClient instance. If needed, scale session processing by scaling ProcessorClient instances across pods.