azure-sdk-for-net
azure-sdk-for-net copied to clipboard
[BUG] Cannot access a disposed object. Object name: 'ClientRegistration'.
Library name and version
Azure.Messaging.ServiceBus, Version=7.17.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8
Describe the bug
Randomly we get an exception with the message "Cannot access a disposed object. Object name: 'ClientRegistration'." This occurs when we try to read messages from a topic.
Here is the stack trace of the error:
System.ObjectDisposedException:
at Microsoft.Extensions.Azure.ClientRegistration1.GetClient (Microsoft.Extensions.Azure, Version=1.7.4.0, Culture=neutral, PublicKeyToken=92742159e12e44c8) at Microsoft.Extensions.Azure.AzureClientFactory2.CreateClient (Microsoft.Extensions.Azure, Version=1.7.4.0, Culture=neutral, PublicKeyToken=92742159e12e44c8)
at Infrastructure.ServiceBus.CommandPublisher+<Schedule>d__8`1.MoveNext (ABCD.Infrastructure.ServiceBus, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
Expected behavior
We should be able to consume messages from Azure bus topic.
Actual behavior
Randomly getting the error. At times 20-30 messages fail in quick succession in a day. We normally get 1 million messages to process in a day.
Message consumer is hosted in AKS.
Reproduction Steps
It is a standard setup of message consumption.
Environment
No response
Thank you for your feedback. Tagging and routing to the team member best able to assist.
Hi @rajeevBlr. Thanks for reaching out and we regret that you’re experiencing difficulties. The message that you’re seeing indicates that either you’ve explicitly disposed the receiver or client that you are attempting to use or that DI has disposed it.
There is not enough to context to offer further thoughts. If you need further assistance, please share a stand-alone app that can reproduce the behavior that you’re seeing. We’d be happy to take a look.
Hi @rajeevBlr. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.
Hi @jsquire , thanks for your response.
As I mentioned in my previous message, this is not replicable consistenly. It occurs say 30 times out of 1 million times. So even if I create a stand-alone app, it will not be easily replicable.
We are using Microsoft.Extensions.Hosting.BackgroundService to host the Message Conumer client Here is the trimmed down code snippet.
` this.serviceBusClient = new ServiceBusClient("connectionString"));
this.sessionProcessorOptions = new ServiceBusSessionProcessorOptions { MaxConcurrentSessions = maxConcurrentSessions, PrefetchCount = prefetchCount, SessionIdleTimeout = TimeSpan.FromSeconds(1) };
this.processor = this.serviceBusClient.CreateSessionProcessor(topicName, subscriptionName, this.sessionProcessorOptions); this.processor.ProcessMessageAsync += this.ProcessMessagesAsync; this.processor.ProcessErrorAsync += this.ProcessErrorAsync; await this.processor.StartProcessingAsync(cancellationToken);`
Hi @rajeevBlr. Thanks for sharing the snippet. Unfortunately, there's nothing obvious that stands out in there. That also does not align with the error message that you've shared. In the snippet, you're directly creating Service Bus clients and your application explicitly owns the lifetime. Unless you explicitly closed/disposed them and then attempted to use them, you would not see a dispose exception.
The stack trace in your issue description indicates that something in your application is disposing a singleton instance in the DI container that was registered with the Microsoft.Azure.Extensions library. The snippet does not show any extensions use.
In both cases, there does not seem to be data that indicates an issue with the Azure SDK packages. We would advise reviewing your application and ensuring that you are not disposing clients registered for DI and that you are stopping any processor instances before your application terminates and Di cleanup happens.
Hi @rajeevBlr. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.
Hi @rajeevBlr, since you haven’t asked that we /unresolve the issue, we’ll close this out. If you believe further discussion is needed, please add a comment /unresolve to reopen the issue.