Uploaded image for project: 'Log4net'
  1. Log4net
  2. LOG4NET-646

RendererMap not threadsafe (IndexOutOfRangeException)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 2.0.8
    • None
    • Core
    • None

    Description

      Concurrent calls to code such as

      loggerRepository.RendererMap.FindAndRender(new { Foo = "bar" });
      

      occasionally fail with

      System.IndexOutOfRangeException: Index was outside the bounds of the array.
          at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
          at log4net.ObjectRenderer.RendererMap.Get(Type type)
          at log4net.ObjectRenderer.RendererMap.FindAndRender(Object obj, TextWriter writer)
      

      Once this has occured, further calls to RendererMap throw. This causes total failure of the logging system.

      This appears to be because log4net.ObjectRenderer.RendererMap is not thread-safe. Internally it holds two Hashtables, m_map and m_cache. The first uses a synchronized wrapper and the second does not.

      From https://docs.microsoft.com/en-us/dotnet/api/system.collections.hashtable?view=netframework-4.8:

      Hashtable is thread safe for use by multiple reader threads and a single writing thread. It is thread safe for multi-thread use when only one of the threads perform write (update) operations, which allows for lock-free reads provided that the writers are serialized to the Hashtable. To support multiple writers all operations on the Hashtable must be done through the wrapper returned by the Synchronized(Hashtable) method, provided that there are no threads reading the Hashtable object.

      So when two threads attempt to mutate the cache concurrently its internal state is corrupted.

      A solution (at least for .NET Framework 4.0 onwards) might be to replace both with a ConcurrentDictionary<Type, IObjectRenderer> accessed via its GetOrAdd method.

      Attachments

        Activity

          People

            davydm Davyd McColl
            pmg23 Matt Grimwade
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: