algolia / algolia/algoliasearch-client-csharp

[bug]: InvalidOperationException: Collection was modified; enumeration operation may not execute.

Open
#865 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
C#
Stars
128
Forks
61
PR merge metrics
No merged PRs in 30d

Description

### Description

When running an indexing job for Algolia, we experience this an InvalidOperationException error intermittently.
The code is the following:

```
var batchResponses = await _client.SaveObjectsAsync(_indexName, dtos, cancellationToken: _token);
var taskResponses = new List();

foreach (var batchResponse in batchResponses)
{
var taskResponse = await _client.WaitForTaskAsync(_indexName, batchResponse.TaskID, ct: _token);
taskResponses.Add(taskResponse);
}

return taskResponses.Any(x => x.Status == TaskStatus.NotPublished);
```

From the stacktrace (that is added in the log output), it is an issue in the HttpRequestHeadersExtensions.Fill method probably comes from AlgoliaHttpRequester.SendRequestAsync line 70 ( httpRequestMessage.Headers.Fill(request.Headers); )

### Client

Search

### Version

7.4.1

### Relevant log output

```shell
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
at Algolia.Search.Http.HttpRequestHeadersExtensions.Fill(HttpRequestHeaders headers, IDictionary`2 dictionary)
at Algolia.Search.Http.AlgoliaHttpRequester.d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Algolia.Search.Transport.HttpTransport.d__10`2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Algolia.Search.Transport.HttpTransport.d__8`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Algolia.Search.Clients.SearchClient.d__72.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Algolia.Search.Clients.SearchClient.<>c__DisplayClass131_0.<b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Algolia.Search.Clients.SearchClient.d__149`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Algolia.Search.Clients.SearchClient.d__131.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at <>.Algolia.AlgoliaIndexingJob.d__55.MoveNext() in <>
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at <>.Algolia.AlgoliaIndexingJob.d__42.MoveNext() in <>
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at HttpRequestHeadersExtensions.Fill and AlgoliaHttpRequester.SendRequestAsync line 70, using the reported indexing flow and stack trace as the reproduction context. Confirm the source of the collection modification and verify that repeated or concurrent requests no longer produce the intermittent InvalidOperationException.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.