Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception "Destination array is not long enough to copy all the items in the collection. Check array index and length." happens in when running under debugger #200

Closed
SergeyKanzhelev opened this issue Feb 25, 2016 · 3 comments
Assignees
Labels
Milestone

Comments

@SergeyKanzhelev
Copy link
Contributor

Exception "Destination array is not long enough to copy all the items in the collection. Check array index and length." happens in when running under debugger:

System.ArgumentException occurred
  HResult=-2147024809
  Message=Destination array is not long enough to copy all the items in the collection. Check array index and length.
  Source=mscorlib
  StackTrace:
       at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
       at System.Collections.Generic.Dictionary`2.CopyTo(KeyValuePair`2[] array, Int32 index)
       at System.Collections.Generic.Dictionary`2.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair`2[] array, Int32 index)
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at Microsoft.ApplicationInsights.Extensibility.Implementation.Property.SanitizeProperties(IDictionary`2 dictionary)
       at Microsoft.ApplicationInsights.DataContracts.EventTelemetry.Microsoft.ApplicationInsights.Channel.ITelemetry.Sanitize()
       at Microsoft.ApplicationInsights.Extensibility.Implementation.JsonSerializer.SeializeToStream(IEnumerable`1 telemetryItems, TextWriter streamWriter)
       at Microsoft.ApplicationInsights.Extensibility.Implementation.JsonSerializer.SerializeAsString(IEnumerable`1 telemetryItems)
       at Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryDebugWriter.WriteTelemetry(ITelemetry telemetry, String filteredBy)
       at Microsoft.ApplicationInsights.Extensibility.Implementation.TransmissionProcessor.Process(ITelemetry item)
       at Microsoft.ApplicationInsights.WindowsServer.Channel.Implementation.SamplingPercentageEstimatorTelemetryProcessor.Process(ITelemetry item)
       at Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.SamplingTelemetryProcessor.Process(ITelemetry item)
       at Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor.Process(ITelemetry item)
       at Microsoft.ApplicationInsights.TelemetryClient.Track(ITelemetry telemetry)
       at Microsoft.ApplicationInsights.TelemetryClient.TrackEvent(EventTelemetry telemetry)
       at Microsoft.ApplicationInsights.TelemetryClient.TrackEvent(String eventName, IDictionary`2 properties, IDictionary`2 metrics)

The probable root cause is that Debug writer executes in parallel with the channel. Both of them doing sanitization by modifying the same collection in parallel.

@SergeyKanzhelev
Copy link
Contributor Author

Another exception you can get is:

System.InvalidOperationException occurred
  HResult=-2146233079
  Message=Collection was modified; enumeration operation may not execute.
  Source=mscorlib
  StackTrace:
       at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
  InnerException: 

    mscorlib.dll!System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource resource)   Unknown
    mscorlib.dll!System.Collections.Generic.Dictionary<string, string>.Enumerator.MoveNext()    Unknown
    Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.Extensibility.Implementation.JsonWriter.WriteProperty(string name, System.Collections.Generic.IDictionary<string, string> values)   Unknown
    Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.Extensibility.Implementation.JsonSerializer.SerializeEventTelemetry(Microsoft.ApplicationInsights.DataContracts.EventTelemetry eventTelemetry, Microsoft.ApplicationInsights.Extensibility.Implementation.JsonWriter writer)    Unknown
    Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.Extensibility.Implementation.JsonSerializer.SerializeTelemetryItem(Microsoft.ApplicationInsights.Channel.ITelemetry telemetryItem, Microsoft.ApplicationInsights.Extensibility.Implementation.JsonWriter jsonWriter)    Unknown
    Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.Extensibility.Implementation.JsonSerializer.SeializeToStream(System.Collections.Generic.IEnumerable<Microsoft.ApplicationInsights.Channel.ITelemetry> telemetryItems, System.IO.TextWriter streamWriter)    Unknown
    Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.Extensibility.Implementation.JsonSerializer.SerializeAsString(System.Collections.Generic.IEnumerable<Microsoft.ApplicationInsights.Channel.ITelemetry> telemetryItems)  Unknown
    Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryDebugWriter.WriteTelemetry(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry, string filteredBy) Unknown
    Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.Extensibility.Implementation.TransmissionProcessor.Process(Microsoft.ApplicationInsights.Channel.ITelemetry item)   Unknown
    Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.TelemetryClient.Track(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)   Unknown
>   ConsoleApplication5.exe!ConsoleApplication5.Program.Main(string[] args) Line 55 C#

Repro code:

    class Program
    {
        static Random random = new Random();

        public static string RandomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#-*\"#-*\"#-*\"#-*\"#-*\"#-*\"#-*\"#-*\"#-*\"#-*\"";

            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }


        static void Main(string[] args)
        {
            TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true;

            TelemetryClient client = new TelemetryClient();
            client.InstrumentationKey = "9d3ebb4f-7a11-4fb1-91ac-7ca8a17627eb";

            Random r = new Random();

            var counter = 0;

            while (true)
            {
                EventTelemetry telemetry = new EventTelemetry();

                int len = r.Next(50);

                for (int i = 0; i < len; i++)
                {
                    string key = RandomString(10);
                    if (!telemetry.Properties.ContainsKey(key))
                        telemetry.Properties.Add(key, RandomString(50));

                    Debug.WriteLine(key +" " + telemetry.Properties[key]);
                }

                Thread.Sleep(1000);
                client.Track(telemetry);
                //((ITelemetry)telemetry).Sanitize();

                Debug.WriteLine(counter++.ToString() + " len: " + len);
            }

        }
    }

@abaranch
Copy link
Contributor

@SergeyKanzhelev Please merge the fix to master. We want to have it in the next stable version.

@SergeyKanzhelev
Copy link
Contributor Author

Workaround is to set this flag:

TelemetryDebugWriter.IsTracingDisabled = true;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants