Reputation: 16959
I am using the Azure Search Client Library and I want to call a query multiple times in parallel with different query parameters. When I try this I get the exception:
"Collection was modified; enumeration operation may not execute."
I handled the problem by adding a SemaphoreSlim object before the call which prevents multiple threads to execute the query at the same time. However this solution doubles the execution time.
private static readonly SemaphoreSlim syncLock = new SemaphoreSlim(1);
....
await syncLock.WaitAsync();
result = await SearchClient.Indexes[IndexName].QueryAsync<MyIndex>(queryParams);
syncLock.Release();
Since each query is an independent call I assume the threads should not be affected by each other?
Upvotes: 0
Views: 273
Reputation: 26
Behind the doors, there's a common enumerable object which lists the indexes created within a service. If there isn't a reference in the memory of the index you're trying to retrieve, one will be created after getting the index's statistics, schema and some other properties on your behalf, totally transparent to the user. However, this operation, if done on another thread in parallel multiple times will throw this exception. Thanks a lot for this feedback, I try to update the library asap and have this situation treated correspondigly. Until then (I suspect this might take a couple of days), please keep using your Semaphore solution which works great. Thanks again! Alex
Upvotes: 1