FindLogEntries exception 'Destination array was not long enough' #671

Closed
opened 2025-12-29 15:30:29 +01:00 by adam · 2 comments
Owner

Originally created by @rinkeb on GitHub (Feb 25, 2025).

Originally assigned to: @StefH on GitHub.

Describe the bug

Intermittent exception when calling WireMockServer.FindLogEntries:

Message:

System.ArgumentException : Destination array was not long enough. Check the destination index, length, and the array's lower bounds. (Parameter 'destinationArray')

Stack Trace: 

Array.CopyImpl(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
List`1.CopyTo(T[] array, Int32 arrayIndex)
Enumerable.ToArray[TSource](IEnumerable`1 source)
WireMockServer.get_LogEntries()
WireMockServer.FindLogEntries(IRequestMatcher[] matchers)

This only seems to happen when log entries are being added to WireMockMiddlewareOptions.LogEntries during the call to FindLogEntries.

Expected behavior:

No exception should be thrown.

Test to reproduce

  • Call FindLogEntries while entries are added to source collection _options.LogEntries.

It looks like the generic Enumerable.ToArray implementation called in public IReadOnlyList LogEntries => _options.LogEntries.ToArray(); for getting a snapshot of the log entries does not like it when the source collection grows while copying the items. A dedicated threadsafe ToArray implementation on ConcurrentObservableCollection might be the solution.

Related: https://github.com/WireMock-Net/WireMock.Net/issues/1234
Using version 1.7.2

Originally created by @rinkeb on GitHub (Feb 25, 2025). Originally assigned to: @StefH on GitHub. ### Describe the bug Intermittent exception when calling `WireMockServer.FindLogEntries`: Message: ``` System.ArgumentException : Destination array was not long enough. Check the destination index, length, and the array's lower bounds. (Parameter 'destinationArray') ``` Stack Trace:  ``` Array.CopyImpl(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable) Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length) List`1.CopyTo(T[] array, Int32 arrayIndex) Enumerable.ToArray[TSource](IEnumerable`1 source) WireMockServer.get_LogEntries() WireMockServer.FindLogEntries(IRequestMatcher[] matchers) ``` This only seems to happen when log entries are being added to [WireMockMiddlewareOptions.LogEntries](https://github.com/WireMock-Net/WireMock.Net/blob/e7d442e5aca8408ce73d12cb88136400d4bf5eb7/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs#L33 ) during the call to `FindLogEntries`. ### Expected behavior: No exception should be thrown. ### Test to reproduce - Call `FindLogEntries` while entries are added to source collection `_options.LogEntries`. ### Other related info It looks like the generic `Enumerable.ToArray` implementation called in [public IReadOnlyList<ILogEntry> LogEntries => _options.LogEntries.ToArray();](https://github.com/WireMock-Net/WireMock.Net/blob/e7d442e5aca8408ce73d12cb88136400d4bf5eb7/src/WireMock.Net/Server/WireMockServer.LogEntries.cs#L27) for getting a snapshot of the log entries does not like it when the source collection grows while copying the items. A dedicated threadsafe `ToArray` implementation on `ConcurrentObservableCollection` might be the solution. Related: https://github.com/WireMock-Net/WireMock.Net/issues/1234 Using version 1.7.2
adam added the bug label 2025-12-29 15:30:29 +01:00
adam closed this issue 2025-12-29 15:30:29 +01:00
Author
Owner

@StefH commented on GitHub (Feb 26, 2025):

"A dedicated threadsafe ToArray implementation on ConcurrentObservableCollection might be the solution"
--> Good suggestion !

@StefH commented on GitHub (Feb 26, 2025): "A dedicated threadsafe ToArray implementation on ConcurrentObservableCollection might be the solution" --> Good suggestion !
Author
Owner

@StefH commented on GitHub (Feb 26, 2025):

https://github.com/WireMock-Net/WireMock.Net/pull/1256

@StefH commented on GitHub (Feb 26, 2025): https://github.com/WireMock-Net/WireMock.Net/pull/1256
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#671