Timing class is not threadsafe

Timing class is not threadsafe.

I get System.IndexOutOfRangeException error from the AddChild method in the Timing class.

I have added a lock to avoid the error:

    private object _childrenLock = new object();

    /// <summary>
    /// Add the parameter 'timing' to this Timing's Children collection.
    /// </summary>
    /// <param name="timing">The timing.</param>
    /// <remarks>Used outside this assembly for custom deserialization when creating an <see cref="Storage.IStorage"/> implementation.</remarks>
    public void AddChild(Timing timing)
    {
        lock (_childrenLock)
        {
            if (Children == null)
                Children = new List<Timing>();

            Children.Add(timing);
        }
        timing.ParentTiming = this;
    }

Exception detail:

Exception: System.AggregateException: One or more errors occurred. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
 at System.Collections.Generic.List`1.Add(T item)
 at StackExchange.Profiling.Timing.AddChild(Timing timing) in c:\Code\github\SamSaffron\MiniProfiler\StackExchange.Profiling\Timing.cs:line 350
 at StackExchange.Profiling.Timing..ctor(MiniProfiler profiler, Timing parent, String name) in c:\Code\github\SamSaffron\MiniProfiler\StackExchange.Profiling\Timing.cs:line 61
 at StackExchange.Profiling.MiniProfiler.StepImpl(String name, ProfileLevel level) in c:\Code\github\SamSaffron\MiniProfiler\StackExchange.Profiling\MiniProfiler.cs:line 415
 at StackExchange.Profiling.MiniProfilerExtensions.Step(MiniProfiler profiler, String name, ProfileLevel level) in c:\Code\github\SamSaffron\MiniProfiler\StackExchange.Profiling\MiniProfilerExtensions.cs:line 40
 at SkemaBll.Skema.HoldBeregninger.Beregn(SkemaSkoleaarsPlan skoleaarsPlan, DriftsDagplan dagPlan, StandardSkema skema, Boolean medBeregningsListe, List`1 beregninger) in c:\SVN\UVSkoleV2\newtrunk\UVSkole\Server\SkemaBll\Skema\HoldBeregninger.cs:line 30
 at UVSkoleSkemaKlient.Readers.CalculationHoldReader.<>c__DisplayClass9.<StartCalculationWhenCurrentIsFinished>b__5() in c:\SVN\UVSkoleV2\newtrunk\UVSkole\Klient\Win\UVSkoleSkemaKlient\Readers\CalculationHoldReader.cs:line 40
 at System.Threading.Tasks.Task`1.InnerInvoke()
 at System.Threading.Tasks.Task.Execute()
 --- End of inner exception stack trace ---
---> (Inner Exception #0) System.IndexOutOfRangeException: Index was outside the bounds of the array.
 at System.Collections.Generic.List`1.Add(T item)
 at StackExchange.Profiling.Timing.AddChild(Timing timing) in c:\Code\github\SamSaffron\MiniProfiler\StackExchange.Profiling\Timing.cs:line 350
 at StackExchange.Profiling.Timing..ctor(MiniProfiler profiler, Timing parent, String name) in c:\Code\github\SamSaffron\MiniProfiler\StackExchange.Profiling\Timing.cs:line 61
 at StackExchange.Profiling.MiniProfiler.StepImpl(String name, ProfileLevel level) in c:\Code\github\SamSaffron\MiniProfiler\StackExchange.Profiling\MiniProfiler.cs:line 415
 at StackExchange.Profiling.MiniProfilerExtensions.Step(MiniProfiler profiler, String name, ProfileLevel level) in c:\Code\github\SamSaffron\MiniProfiler\StackExchange.Profiling\MiniProfilerExtensions.cs:line 40
 at SkemaBll.Skema.HoldBeregninger.Beregn(SkemaSkoleaarsPlan skoleaarsPlan, DriftsDagplan dagPlan, StandardSkema skema, Boolean medBeregningsListe, List`1 beregninger) in c:\SVN\UVSkoleV2\newtrunk\UVSkole\Server\SkemaBll\Skema\HoldBeregninger.cs:line 30
 at UVSkoleSkemaKlient.Readers.CalculationHoldReader.<>c__DisplayClass9.<StartCalculationWhenCurrentIsFinished>b__5() in c:\SVN\UVSkoleV2\newtrunk\UVSkole\Klient\Win\UVSkoleSkemaKlient\Readers\CalculationHoldReader.cs:line 40
 at System.Threading.Tasks.Task`1.InnerInvoke()
 at System.Threading.Tasks.Task.Execute()<---

@jdixon any idea what is going on here?

It’s well-known the implementation isn’t thread-safe. Something to be addressed soon, I hope.

1 Like

Has this been addressed yet?

This has not been addressed yet. You can track progress or contribute on issue 35 in the Github site