azure-sdk-for-java: [BUG] spring-cloud-azure-starter-servicebus-jms - com.azure.core.amqp.exception.AmqpException: onSessionRemoteClose amqp: connection:forced The connection was closed by container because it did not have any active links in the past 300000 milliseconds
Describe the bug We are using the ServiceBusSenderClient to send messages to a service bus topic. When there is no message send for 15 minutes we see that the connection is freed but results in een NullPointerException afterwards as a new session is created on the already closed connection.
Exception or Stack Trace
2024-09-02 13:12:13.557 DEBUG [spring-app-name,,] 80959 --- [tem-execution-4] b.d.s.c.j.o.service.OutboxItemProcessor : Processing outbox item: 4089 with aggregateId: 6049
2024-09-02 13:27:37.216 ERROR [spring-app-name,,] 80959 --- [ctor-executor-1] reactor.core.publisher.Operators : Operator called default onErrorDropped
reactor.core.Exceptions$ErrorCallbackNotImplemented: com.azure.core.amqp.exception.AmqpException: onSessionRemoteClose connectionId[MF_27dedd_1725282731155], entityName[service-bus-topic-name] condition[Error{condition=amqp:connection:forced, description='The connection was closed by container 'e571b398fba34f83bfc494365a9a6210_G4' because it did not have any active links in the past 300000 milliseconds. TrackingId:e571b398fba34f83bfc494365a9a6210_G4, SystemTracker:gateway10, Timestamp:2024-09-02T13:27:39', info=null}], errorContext[NAMESPACE: service-bus-name.servicebus.windows.net. ERROR CONTEXT: N/A, PATH: ervice-bus-topic-name]
Caused by: com.azure.core.amqp.exception.AmqpException: onSessionRemoteClose connectionId[MF_27dedd_1725282731155], entityName[ervice-bus-topic-name] condition[Error{condition=amqp:connection:forced, description='The connection was closed by container 'e571b398fba34f83bfc494365a9a6210_G4' because it did not have any active links in the past 300000 milliseconds. TrackingId:e571b398fba34f83bfc494365a9a6210_G4, SystemTracker:gateway10, Timestamp:2024-09-02T13:27:39', info=null}], errorContext[NAMESPACE: service-bus-name.servicebus.windows.net. ERROR CONTEXT: N/A, PATH: ervice-bus-topic-name]
at com.azure.core.amqp.implementation.ExceptionUtil.toException(ExceptionUtil.java:90)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Assembly trace from producer [reactor.core.publisher.FluxDistinctUntilChanged] :
reactor.core.publisher.Flux.distinctUntilChanged(Flux.java:4744)
com.azure.core.amqp.implementation.handler.Handler.getEndpointStates(Handler.java:77)
Error has been observed at the following site(s):
*__Flux.distinctUntilChanged ⇢ at com.azure.core.amqp.implementation.handler.Handler.getEndpointStates(Handler.java:77)
|_ Flux.map ⇢ at com.azure.core.amqp.implementation.ReactorSession.<init>(ReactorSession.java:141)
|_ Flux.doOnError ⇢ at com.azure.core.amqp.implementation.ReactorSession.<init>(ReactorSession.java:148)
|_ Flux.doOnComplete ⇢ at com.azure.core.amqp.implementation.ReactorSession.<init>(ReactorSession.java:148)
|_ Flux.cache ⇢ at com.azure.core.amqp.implementation.ReactorSession.<init>(ReactorSession.java:148)
|_ Flux.cache ⇢ at com.azure.core.amqp.implementation.ReactorSession.<init>(ReactorSession.java:148)
Original Stack Trace:
at com.azure.core.amqp.implementation.ExceptionUtil.toException(ExceptionUtil.java:90)
at com.azure.core.amqp.implementation.handler.SessionHandler.onSessionRemoteClose(SessionHandler.java:139)
at org.apache.qpid.proton.engine.BaseHandler.handle(BaseHandler.java:152)
at org.apache.qpid.proton.engine.impl.EventImpl.dispatch(EventImpl.java:108)
at org.apache.qpid.proton.reactor.impl.ReactorImpl.dispatch(ReactorImpl.java:324)
at org.apache.qpid.proton.reactor.impl.ReactorImpl.process(ReactorImpl.java:291)
at com.azure.core.amqp.implementation.ReactorExecutor.run(ReactorExecutor.java:91)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:68)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:28)
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:840)
2024-09-02 13:27:37.223 ERROR [spring-app-name,,] 80959 --- [ctor-executor-1] reactor.core.publisher.Operators : Operator called default onErrorDropped
reactor.core.Exceptions$ErrorCallbackNotImplemented: com.azure.core.amqp.exception.AmqpException: onSessionRemoteClose connectionId[MF_27dedd_1725282731155], entityName[cbs-session] condition[Error{condition=amqp:connection:forced, description='The connection was closed by container 'e571b398fba34f83bfc494365a9a6210_G4' because it did not have any active links in the past 300000 milliseconds. TrackingId:e571b398fba34f83bfc494365a9a6210_G4, SystemTracker:gateway10, Timestamp:2024-09-02T13:27:39', info=null}], errorContext[NAMESPACE: service-bus-name.servicebus.windows.net. ERROR CONTEXT: N/A, PATH: cbs-session]
Caused by: com.azure.core.amqp.exception.AmqpException: onSessionRemoteClose connectionId[MF_27dedd_1725282731155], entityName[cbs-session] condition[Error{condition=amqp:connection:forced, description='The connection was closed by container 'e571b398fba34f83bfc494365a9a6210_G4' because it did not have any active links in the past 300000 milliseconds. TrackingId:e571b398fba34f83bfc494365a9a6210_G4, SystemTracker:gateway10, Timestamp:2024-09-02T13:27:39', info=null}], errorContext[NAMESPACE: service-bus-name.servicebus.windows.net. ERROR CONTEXT: N/A, PATH: cbs-session]
at com.azure.core.amqp.implementation.ExceptionUtil.toException(ExceptionUtil.java:90)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Assembly trace from producer [reactor.core.publisher.FluxDistinctUntilChanged] :
reactor.core.publisher.Flux.distinctUntilChanged(Flux.java:4744)
com.azure.core.amqp.implementation.handler.Handler.getEndpointStates(Handler.java:77)
Error has been observed at the following site(s):
*__Flux.distinctUntilChanged ⇢ at com.azure.core.amqp.implementation.handler.Handler.getEndpointStates(Handler.java:77)
|_ Flux.map ⇢ at com.azure.core.amqp.implementation.ReactorSession.<init>(ReactorSession.java:141)
|_ Flux.doOnError ⇢ at com.azure.core.amqp.implementation.ReactorSession.<init>(ReactorSession.java:148)
|_ Flux.doOnComplete ⇢ at com.azure.core.amqp.implementation.ReactorSession.<init>(ReactorSession.java:148)
|_ Flux.cache ⇢ at com.azure.core.amqp.implementation.ReactorSession.<init>(ReactorSession.java:148)
|_ Flux.cache ⇢ at com.azure.core.amqp.implementation.ReactorSession.<init>(ReactorSession.java:148)
Original Stack Trace:
at com.azure.core.amqp.implementation.ExceptionUtil.toException(ExceptionUtil.java:90)
at com.azure.core.amqp.implementation.handler.SessionHandler.onSessionRemoteClose(SessionHandler.java:139)
at org.apache.qpid.proton.engine.BaseHandler.handle(BaseHandler.java:152)
at org.apache.qpid.proton.engine.impl.EventImpl.dispatch(EventImpl.java:108)
at org.apache.qpid.proton.reactor.impl.ReactorImpl.dispatch(ReactorImpl.java:324)
at org.apache.qpid.proton.reactor.impl.ReactorImpl.process(ReactorImpl.java:291)
at com.azure.core.amqp.implementation.ReactorExecutor.run(ReactorExecutor.java:91)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:68)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:28)
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:840)
2024-09-02 13:27:39.023 ERROR [spring-app-name,,] 80959 --- [ parallel-10] c.a.c.a.i.AmqpChannelProcessor : {"az.sdk.message":"Retry attempts exhausted or exception was not retriable.","exception":"Cannot invoke \"java.util.List.add(Object)\" because \"this._sessions\" is null","connectionId":"MF_27dedd_1725282731155","entityPath":"$cbs","tryCount":1}
2024-09-02 13:27:39.024 ERROR [spring-app-name,,] 80959 --- [ parallel-10] reactor.core.publisher.Operators : Operator called default onErrorDropped
reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.NullPointerException: Cannot invoke "java.util.List.add(Object)" because "this._sessions" is null
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.add(Object)" because "this._sessions" is null
at org.apache.qpid.proton.engine.impl.ConnectionImpl.session(ConnectionImpl.java:91)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Assembly trace from producer [reactor.core.publisher.MonoMapFuseable] :
reactor.core.publisher.Mono.map(Mono.java:3482)
com.azure.core.amqp.implementation.ReactorConnection.createSession(ReactorConnection.java:307)
Error has been observed at the following site(s):
*_____________Mono.map ⇢ at com.azure.core.amqp.implementation.ReactorConnection.createSession(ReactorConnection.java:307)
|_ Mono.flatMap ⇢ at com.azure.core.amqp.implementation.ReactorConnection.createSession(ReactorConnection.java:340)
|_ Mono.cast ⇢ at com.azure.core.amqp.implementation.ReactorConnection.createRequestResponseChannel(ReactorConnection.java:441)
|_ Mono.map ⇢ at com.azure.core.amqp.implementation.ReactorConnection.createRequestResponseChannel(ReactorConnection.java:442)
|_ Mono.doOnNext ⇢ at com.azure.core.amqp.implementation.ReactorConnection.createRequestResponseChannel(ReactorConnection.java:446)
|_ Mono.repeat ⇢ at com.azure.core.amqp.implementation.ReactorConnection.createRequestResponseChannel(ReactorConnection.java:454)
*_________Mono.flatMap ⇢ at com.azure.core.amqp.implementation.ReactorConnection.closeAsync(ReactorConnection.java:504)
|_ Mono.doFinally ⇢ at com.azure.core.amqp.implementation.ReactorConnection.closeAsync(ReactorConnection.java:536)
*__Mono.whenDelayError ⇢ at com.azure.core.amqp.implementation.ReactorConnection.closeAsync(ReactorConnection.java:535)
*____________Mono.then ⇢ at com.azure.core.amqp.implementation.ReactorConnection.closeAsync(ReactorConnection.java:541)
*____________Mono.then ⇢ at com.azure.core.amqp.implementation.ReactorConnection.closeAsync(ReactorConnection.java:544)
Original Stack Trace:
at org.apache.qpid.proton.engine.impl.ConnectionImpl.session(ConnectionImpl.java:91)
at org.apache.qpid.proton.engine.impl.ConnectionImpl.session(ConnectionImpl.java:39)
at com.azure.core.amqp.implementation.ReactorConnection.lambda$createSession$15(ReactorConnection.java:311)
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708)
at com.azure.core.amqp.implementation.ReactorConnection.lambda$createSession$16(ReactorConnection.java:308)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:113)
at reactor.core.publisher.MonoPeekTerminal$MonoTerminalPeekSubscriber.onNext(MonoPeekTerminal.java:180)
at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.request(MonoIgnoreThen.java:164)
at reactor.core.publisher.MonoPeekTerminal$MonoTerminalPeekSubscriber.request(MonoPeekTerminal.java:139)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:171)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.request(MonoFlatMap.java:194)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:171)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:171)
at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.request(FluxPeekFuseable.java:144)
at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.request(Operators.java:2331)
at com.azure.core.amqp.implementation.AmqpChannelProcessor.requestUpstream(AmqpChannelProcessor.java:336)
at com.azure.core.amqp.implementation.AmqpChannelProcessor.lambda$onError$4(AmqpChannelProcessor.java:230)
at reactor.core.publisher.LambdaMonoSubscriber.onNext(LambdaMonoSubscriber.java:171)
at reactor.core.publisher.MonoDelay$MonoDelayRunnable.propagateDelay(MonoDelay.java:270)
at reactor.core.publisher.MonoDelay$MonoDelayRunnable.run(MonoDelay.java:285)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:68)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:28)
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:840)
To Reproduce
- Create a serviceBusSenderClient instance
- send a message to the service bus topic
- Wait 15 minutes and the exception logging occurs
Code Snippet
public void execute() {
ServiceBusSenderClient serviceBusSenderClient = serviceBusClientBuilder
.connectionString("connectionString")
.sender()
.topicName("topicName")
.buildClient();
ServiceBusMessage serviceBusMessage = new ServiceBusMessage("payload");
serviceBusMessage.getRawAmqpMessage().getApplicationProperties().put(TYPE_ID, "messageType");
serviceBusMessage.setMessageId("messageId");
serviceBusSenderClient.sendMessage(serviceBusMessage);
}
Expected behavior No error logging should occur
Screenshots
When the doFree method is called on the connection the _sessions variable is set to null but afterwards the connection seems to be reused to create a new session which results in the NullPointerException.
Setup (please complete the following information):
- OS: Azure Spring Apps / Linux
- IDE: n/a
- Library/Libraries: com.azure.spring:spring-cloud-azure-starter-servicebus-jms, we use version 5.13.0
- Java version: 17
- App Server/Environment: Apache Tomcat from Spring Boot 3.3
- Frameworks: Spring Boot 3.3.1
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 45 (25 by maintainers)
Hello @robsonkades, I think the issue is that the configuration “com.azure.core.amqp.cache” is not enabled. Since the application indirectly uses version 7.17.4 through the Spring library and cannot set it directly in the builder, you can set the system property “com.azure.core.amqp.cache” to true.
Hi @czoromba1, glad to hear you’re unblocked with Event Hubs v2 stack. To answer your question - we currently plan to make the v2 stack the default for Event Hubs in the first quarter of 2025, eliminating the need for applications to opt-in.
Hi @czoromba1, thanks for reaching out. For Event Hubs, it’s a different configuration. Could you try setting “com.azure.messaging.eventhubs.v2” to “true” instead?
Thank you @josebarros2025 @anuchandy. We aren’t having this issue anymore
Hi @anuchandy - Thank you for releasing version 7.17.4 of Service Bus SDK - it appears to have fixed our issue!
Regarding the reactor-executor thread leak, what we observed is that in versions prior to 7.17.4, when the below error was logged for certain service bus namespaces, the reactor-executor thread associated to that connection would not be closed, and the number of reactor-executor threads would increase over time.
This was the error that always preceded the thread leak: reactor.core.Exceptions$ErrorCallbackNotImplemented: com.azure.core.amqp.exception.AmqpException: onSessionRemoteClose connectionId[MF_ff675e_1727378638933], entityName[sbt-my-topic-name] condition[Error{condition=amqp:connection:forced, description='The connection was closed by container ‘6b2b3bba82084087bfd7d760339cdade_G0’ because it did not have any active links in the past 300000 milliseconds.
We observed this initially as slowly increasing CPU usage of our application, which is a Spring Boot microservice deployed to Azure Spring Apps. We ran a JFR on the Spring Apps instance and observed the high number of zombie reactor-executor threads. Then during troubleshooting we found that only certain service bus namespaces exhibit this behavior. For us, the only namespace that exhibits this behavior is our production instance, and none of our non-prod service bus namespaces exhibit the behavior, despite the SB namespaces being configured identically (via Terraform) and the same exact code being deployed in prod and non-prod.
In any case, here are the answers you requested:
Then wait for 15 minutes. After 15 minutes (exactly), we get the error messages in the attached file. This issue only happens on our production service bus, but for that SB namespace, it happens every time the connection goes idle for 15m or more.
Again, the thread leak behavior appears to be remediated in version 7.17.4 of Service Bus SDK but I’m documenting this here for others.
Also - our use case is low-latency message transfer, so we open a connection to a database and another conenction to Service Bus and we keep these connections open for long periods of time. Then when any message is published from the database, it can be transferred to a Service Bus topic as quickly as possible (without the delay of opening the connection to Service Bus). There are periods of time when the message volume drops and there are no messages transferred in a 15-minute window, which is when we were seeing the behavior.