NServiceBus icon indicating copy to clipboard operation
NServiceBus copied to clipboard

Collection was modified in SetExceptionMetadata

Open SimonCropp opened this issue 2 years ago • 8 comments

Not sure if this one should be in the sql transport or core

we got this in prod. dont know how to replicate yet

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.Collections.ListDictionaryInternal.NodeEnumerator.MoveNext()
   at NServiceBus.FaultMetadataExtractor.SetExceptionMetadata(Dictionary`2 headers, Exception e) in /_/src/NServiceBus.Core/Recoverability/FaultMetadataExtractor.cs:line 50
   at NServiceBus.FaultMetadataExtractor.Extract(ErrorContext errorContext) in /_/src/NServiceBus.Core/Recoverability/FaultMetadataExtractor.cs:line 24
   at NServiceBus.RecoverabilityPipelineExecutor`1.Invoke(ErrorContext errorContext, CancellationToken cancellationToken) in /_/src/NServiceBus.Core/Recoverability/RecoverabilityPipelineExecutor.cs:line 41
   at NServiceBus.Transport.SqlServer.ProcessStrategy.HandleError(Exception exception, Message message, TransportTransaction transportTransaction, Int32 processingAttempts, ContextBag context, CancellationToken cancellationToken) in /_/src/NServiceBus.Transport.SqlServer/Receiving/ProcessStrategy.cs:line 63

the code in question is

 static void SetExceptionMetadata(Dictionary<string, string> headers, Exception e)
{
    headers["NServiceBus.ExceptionInfo.ExceptionType"] = e.GetType().FullName;

    if (e.InnerException != null)
    {
        headers["NServiceBus.ExceptionInfo.InnerExceptionType"] = e.InnerException.GetType().FullName;
    }

    headers["NServiceBus.ExceptionInfo.HelpLink"] = e.HelpLink;
    headers["NServiceBus.ExceptionInfo.Message"] = Truncate(e.GetMessage(), 16384);
    headers["NServiceBus.ExceptionInfo.Source"] = e.Source;
    headers["NServiceBus.ExceptionInfo.StackTrace"] = e.ToString();
    headers["NServiceBus.TimeOfFailure"] = DateTimeOffsetHelper.ToWireFormattedString(DateTimeOffset.UtcNow);

    if (e.Data == null)
    {
        return;
    }
    foreach (DictionaryEntry entry in e.Data)
    {
        if (entry.Value == null)
        {
            continue;
        }
        headers["NServiceBus.ExceptionInfo.Data." + entry.Key] = entry.Value.ToString();
    }
}

with the exception on foreach (DictionaryEntry entry in e.Data).

SimonCropp avatar Jan 09 '23 22:01 SimonCropp