Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread safety for Hashtable in LogicalCallContext

While using LogicalCallContext to share some data across threads we came across an exception with the below stack trace

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.Collections.Hashtable.HashtableEnumerator.MoveNext()
   at System.Runtime.Remoting.Messaging.LogicalCallContext.Merge(LogicalCallContext lc)
   at System.Runtime.Remoting.Proxies.RealProxy.EndInvokeHelper(Message reqMsg, Boolean bProxyCase)
   at System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(Object NotUsed, MessageData& msgData)

I read from MSDN when EndInvoke is called CallContext is merged across threads. My question is doesn't the Hashtable inside CallContext support concurrency. If it doesn't support should we be using CallContext at the first point? If yes are there any guidelines for this.

Can anyone explain when can i see this stack trace?

an example for such scenario is more helpful if u can share.

like image 936
sri Avatar asked Oct 04 '13 07:10

sri


1 Answers

The CallContext works by making a copy of the current collection on the remote machine, and then merging back changes made to that copy during the call. That's what it's trying to do when it throws this exception. It seems like you passed the context to another thread that's modifying the collection while the framework is copying changes back. The exception you are seeing is because the particular collection isn't thread-safe, but even if it used a thread-safe collection there would be a race condition here - changes made to the collection after the changes were copied would be lost. You need to make all of your context changes before the call completes.

like image 64
bmm6o Avatar answered Oct 02 '22 12:10

bmm6o