PowerPlatform-DataverseServiceClient icon indicating copy to clipboard operation
PowerPlatform-DataverseServiceClient copied to clipboard

Collection was modified; enumeration operation may not execute.

Open 1-Accelerator-1 opened this issue 3 years ago • 2 comments

https://github.com/microsoft/PowerPlatform-DataverseServiceClient/blob/dc278e33f6ad4e1b13335d70bb1ee53c6f8d9235/src/GeneralTools/DataverseClient/Client/Utils/Utils.cs#L1097-L1111

My application uses multithreading and sometimes such an error occurs "Collection was modified; enumeration operation may not execute.", most likely since I use multithreading, then at the same time in the execution of this method, "cookieCollection" changes in another thread, which is why this error occurs.

StackTrace: at System.Collections.Generic.Dictionary2.Enumerator.MoveNext() at Microsoft.PowerPlatform.Dataverse.Client.Utilities.GetCookiesFromCollectionAsString(Dictionary2 cookieCollection) at Microsoft.PowerPlatform.Dataverse.Client.DataverseTelemetryBehaviors.BeforeSendRequest(Message& request, IClientChannel channel) at System.ServiceModel.Dispatcher.ImmutableClientRuntime.BeforeSendRequest(ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.PrepareCall(ProxyOperationRuntime operation, Boolean oneway, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(MethodCall methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(MethodInfo targetMethod, Object[] args) --- End of stack trace from previous location where exception was thrown --- at System.Reflection.DispatchProxyGenerator.Invoke(Object[] args) at generatedProxy_1.Execute(OrganizationRequest ) at Microsoft.PowerPlatform.Dataverse.Client.Connector.WebProxyClientAsync1.ExecuteAction[TResult](Func1 action) at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.Command_Execute(OrganizationRequest req, String errorStringCheck, Boolean bypassPluginExecution)

1-Accelerator-1 avatar Aug 10 '22 08:08 1-Accelerator-1

We have several multithreading test cases as part of our test suit and have not seen this.

Could you give us some information on how your using the client here? Are you using the tpl extensions or are you using your own thread management system?

Thanks

MattB-msft avatar Aug 11 '22 04:08 MattB-msft

Hello, I'm very sorry, I went on vacation and forgot about your question.

So about my service, I don't use the tpl extensions.

Concerning operation with threads.

The main task of the service is to read the data of topics from Kafka to Dataverse. At the moment I have 6 topics with which the service works. Each of these topics is read by a separate thread, that is, when the service starts, 6 threads are created to work in each topic. Also, in each thread, records from other topics can be created. For example, there is a topic A that is read in thread 1 and there is a topic B that is read in thread 2, and so sometimes thread 1 can create posts for topic B and vice versa. This is all the work with threads in my service.

Again, sorry for the delay in replying.

1-Accelerator-1 avatar Sep 02 '22 12:09 1-Accelerator-1

We have a speculative fix queued for this issue that will be in the next release.

MattB-msft avatar Oct 21 '22 21:10 MattB-msft

We replaced ADAL with MSAL using Microsoft.PowerPlatform.Dataverse.Client, for every 1 minutes, we process 200 records. In connection string, we use RequiredNewInstance = false to reuse the collection and retry the connection when it failed. However, we got this intermittent error

Server stack trace: at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.Dictionary2.Enumerator.MoveNext() at Microsoft.PowerPlatform.Dataverse.Client.Utilities.GetCookiesFromCollectionAsString(Dictionary2 cookieCollection) at Microsoft.PowerPlatform.Dataverse.Client.DataverseTelemetryBehaviors.BeforeSendRequest(Message& request, IClientChannel channel) at System.ServiceModel.Dispatcher.ImmutableClientRuntime.BeforeSendRequest(ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.PrepareCall(ProxyOperationRuntime operation, Boolean oneway, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.Update(Entity entity)

this error is similar to an earlier post. How this issue be fixed? When using the connecition, should I use RequiredNewInstance= True or False? Why do we get this error after switching to use MSAL ?

rsaclee avatar May 31 '23 20:05 rsaclee