Sporadic InvalidCastException, possible hash collision in ObjectCreator.cache

Aperta
#2,279 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
15/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
csharp
Ambito
backend

Direzione di ricerca

The suspect code is ObjectCreator and its cache field, reached from ObjectResolver.Resolve and CsvContext.RegisterClassMap. Start by reading how the cache key is built and how ClassMap.Map is reached during map construction. There is no reproduction, so done means either a failing test that shows the wrong cached creator being returned, or a reasoned explanation with a key change that the existing tests still pass with.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

Describe the bug
Sometimes the test suite I have throws a very strange InvalidCastException:

System.InvalidCastException : Unable to cast object of type 'CsvHelper.Configuration.MemberMap`2[OtherRecordClass,SomeEnumType]' to type 'CsvHelper.Configuration.MemberMap`2[MyRecordClass,System.Nullable`1[System.DateTime]]'.
Stack Trace:
   at CsvHelper.Configuration.ClassMap`1.Map[TMember](Expression`1 expression, Boolean useExistingMap)
   at MyRecordClassDtoMap..ctor()
   at lambda_method5587(Closure, Object[])
   at CsvHelper.ObjectCreator.CreateInstance(Type type, Object[] args)
   at CsvHelper.ObjectResolver.<>c__DisplayClass17_0.<.cctor>b__1(Type type, Object[] args)
   at CsvHelper.ObjectResolver.Resolve(Type type, Object[] constructorArgs)
   at CsvHelper.ObjectResolver.Resolve[T](Object[] constructorArgs)
   at CsvHelper.CsvContext.RegisterClassMap[TMap]()

My code involved in this is the following:

using var csv = new CsvHelper.CsvReader(reader, config);
csv.Context.RegisterClassMap<MyRecordClassDtoMap>(); // <-- exception here

And MyRecordClassDtoMap is this:

public class MyRecordClassDtoMap : ClassMap<MyRecordClass>
{
    public MyRecordClassDtoMap()
    {
        AutoMap(CultureInfo.CurrentCulture);
        Map(m => m.OptionalDateTime1).Convert(CellConverter.Instance.DateTimeNullableDbase); // <-- this line is the `at MyRecordClassDtoMap..ctor()` in the stack trace, the exception is in the Map call
        Map(m => m.OptionalDateTime2).Convert(CellConverter.Instance.DateTimeNullableDbase);
    }
}

Inspecting the code in the CsvHelper the only way I could imagine such a problem occurring is for there to be a rare hash collision in ObjectCreator's cache causing it to wrongly use a cached instance creation function for a different type than then one needed.

My code does not use any custom IObjectResolver or other ObjectResolver instances.

To Reproduce
Sadly, I was unable to locally reproduce the problem. But it occurs somewhat often on my CI runner.

Additional context

I tried replacing the ObjectResolver.Current with a new instance done analogous to how static ObjectResolver() assigns it, but using a customised copy of ObjectCreator, which uses a string[] as its cache key. The strings are the AssemblyQualifiedNames of the types, and the Dictionary is given a custom equality comparer to handle hashing and comparing string arrays.

Since then I have not encountered the error. However, at the moment each test class that needs it, does that initialization separately (I'm still not sure where is a good central place to replace ObjectResolver.Current for the tests), so it's not certain that the improvement is not just due to refreshing the ObjectResolver.Current value for each test class.

Still, I think it may be a good idea to change ObjectCreator.cache to avoid possible hash collisions.

Lingua principale
C#
Stelle
5.2k
Fork
1.1k
Merge medio
6m
PR unite (30g)
1

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di JoshClose/CsvHelper

Tutte le issue di JoshClose/CsvHelper

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.