Sporadic InvalidCastException, possible hash collision in ObjectCreator.cache
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 15/100
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
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di JoshClose/CsvHelper
-
feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
-
documentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
-
Inefficient parsing of some invalid filesForse già presa Una pull request collegata a questa issue è aperta o già unita. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
When using ExceptionMessagesContainRawData = false the RawRecord data still appears in the exceptionApertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
-
Custom Type Converter page should include an example of using type converter constructor argumentsApertadocumentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
Tutte le issue di JoshClose/CsvHelper
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
BEagle1984/silverback#280 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 63/100
-
Playground selection bar: comes back after Apply or scrolling while focus is in the Markdown paneAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
sillsdev/SIL.TextEditing#175 ·
I maintainer di solito rispondono entro 1 giorno
-
needs attention untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet#3666 ·
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 73/100
MicrosoftLearning/eShopOnWeb#466 ·