dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.63k stars 3.15k forks source link

[iOS / Sqlite] EntityMaterializerSource.TryReadValue[TValue] NullReferenceException #16727

Closed ChaseFlorell closed 1 year ago

ChaseFlorell commented 5 years ago

Describe what is not working as expected.

Unfortunately we cannot reproduce this issue locally, however we are seeing an increasing number of exceptions landing in our AppCenter insights.

System
String.memcpy_aligned_1 (System.Byte* dest, System.Byte* src, System.Int32 size)
Microsoft.EntityFrameworkCore.Metadata.Internal
EntityMaterializerSource.TryReadValue[TValue] (Microsoft.EntityFrameworkCore.Storage.ValueBuffer& valueBuffer, System.Int32 index, Microsoft.EntityFrameworkCore.Metadata.IPropertyBase property)
(wrapper unknown) System.Object.gsharedvt_in()
(wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
System.Reflection
MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
System.Linq.Expressions.Interpreter
MethodInfoCallInstruction.Run (System.Linq.Expressions.Interpreter.InterpretedFrame frame)
System.Linq.Expressions.Interpreter
Interpreter.Run (System.Linq.Expressions.Interpreter.InterpretedFrame frame)
System.Linq.Expressions.Interpreter
LightLambda.Run1[T0,TRet] (T0 arg0)
(wrapper unknown) System.Object.gsharedvt_in()
Microsoft.EntityFrameworkCore.Query.Internal
QueryBuffer.GetEntity (Microsoft.EntityFrameworkCore.Metadata.IKey key, Microsoft.EntityFrameworkCore.Query.EntityLoadInfo entityLoadInfo, System.Boolean queryStateManager, System.Boolean throwOnNullKey)
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal
BufferedEntityShaper`1[TEntity].Shape (Microsoft.EntityFrameworkCore.Query.QueryContext queryContext, Microsoft.EntityFrameworkCore.Storage.ValueBuffer& valueBuffer)
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal
BufferedOffsetEntityShaper`1[TEntity].Shape (Microsoft.EntityFrameworkCore.Query.QueryContext queryContext, Microsoft.EntityFrameworkCore.Storage.ValueBuffer& valueBuffer)
(wrapper unknown) System.Object.gsharedvt_out()
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal
CompositeShaper+TypedCompositeShaper`5[TOuterShaper,TOuter,TInnerShaper,TInner,TResult].Shape (Microsoft.EntityFrameworkCore.Query.QueryContext queryContext, Microsoft.EntityFrameworkCore.Storage.ValueBuffer& valueBuffer)
(wrapper unknown) System.Object.gsharedvt_in()
(wrapper unknown) System.Object.gsharedvt_out()
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.CompositeShaper+TypedCompositeShaper`5[TOuterShaper,TOuter,TInnerShaper,TInner,TResult].Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal
IShaper<TResult>.Shape (Microsoft.EntityFrameworkCore.Query.QueryContext queryContext, Microsoft.EntityFrameworkCore.Storage.ValueBuffer& valueBuffer)
(wrapper unknown) System.Object.gsharedvt_in()
(wrapper unknown) System.Object.gsharedvt_out()
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal
CompositeShaper+TypedCompositeShaper`5[TOuterShaper,TOuter,TInnerShaper,TInner,TResult].Shape (Microsoft.EntityFrameworkCore.Query.QueryContext queryContext, Microsoft.EntityFrameworkCore.Storage.ValueBuffer& valueBuffer)
(wrapper unknown) System.Object.gsharedvt_in()
(wrapper unknown) System.Object.gsharedvt_out()
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.CompositeShaper+TypedCompositeShaper`5[TOuterShaper,TOuter,TInnerShaper,TInner,TResult].Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal
IShaper<TResult>.Shape (Microsoft.EntityFrameworkCore.Query.QueryContext queryContext, Microsoft.EntityFrameworkCore.Storage.ValueBuffer& valueBuffer)
(wrapper unknown) System.Object.gsharedvt_in()
(wrapper unknown) System.Object.gsharedvt_out()
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal
ProjectionShaper+TypedProjectionShaper`3[TShaper,TIn,TOut].Shape (Microsoft.EntityFrameworkCore.Query.QueryContext queryContext, Microsoft.EntityFrameworkCore.Storage.ValueBuffer& valueBuffer)
(wrapper unknown) System.Object.gsharedvt_in()
(wrapper unknown) System.Object.gsharedvt_out()
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ProjectionShaper+TypedProjectionShaper`3[TShaper,TIn,TOut].Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal
IShaper<TOut>.Shape (Microsoft.EntityFrameworkCore.Query.QueryContext queryContext, Microsoft.EntityFrameworkCore.Storage.ValueBuffer& valueBuffer)
(wrapper unknown) System.Object.gsharedvt_in()
Microsoft.EntityFrameworkCore.Query.Internal
AsyncQueryingEnumerable`1+AsyncEnumerator+<BufferlessMoveNext>d__12[T].MoveNext ()
Microsoft.EntityFrameworkCore.Query.Internal
AsyncQueryingEnumerable`1+AsyncEnumerator+<MoveNext>d__11[T].MoveNext ()
Microsoft.EntityFrameworkCore.Query.Internal
QueryBuffer+<IncludeCollectionAsync>d__13`3[TEntity,TRelated,TElement].MoveNext ()
Microsoft.EntityFrameworkCore.Query.Internal
IncludeCompiler+IncludeLoadTreeNodeBase+<_AwaitMany>d__8.MoveNext ()
Microsoft.EntityFrameworkCore.Query.Internal
IncludeCompiler+<_IncludeAsync>d__22`1[TEntity].MoveNext ()
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal
TaskLiftingExpressionVisitor+<_ExecuteAsync>d__8`1[T].MoveNext ()
(wrapper unknown) System.Object.gsharedvt_out()
Microsoft.EntityFrameworkCore.Query
ResultOperatorHandler+AsyncGroupByAsyncEnumerable`3+GroupByAsyncEnumerator+<MoveNext>d__5[TSource,TKey,TElement].MoveNext ()
System.Linq
AsyncEnumerable+SkipAsyncIterator`1+<MoveNextCore>d__7[TSource].MoveNext ()
System.Linq
AsyncEnumerable+AsyncIterator`1+<MoveNext>d__10[TSource].MoveNext ()
System.Linq
AsyncEnumerable+TakeAsyncIterator`1+<MoveNextCore>d__7[TSource].MoveNext ()
System.Linq
AsyncEnumerable+AsyncIterator`1+<MoveNext>d__10[TSource].MoveNext ()
Microsoft.EntityFrameworkCore.Query.Internal
AsyncLinqOperatorProvider+AsyncSelectEnumerable`2+AsyncSelectEnumerator+<MoveNext>d__3[TSource,TResult].MoveNext ()
Microsoft.EntityFrameworkCore.Query.Internal
AsyncLinqOperatorProvider+ExceptionInterceptor`1+EnumeratorExceptionInterceptor+<MoveNext>d__5[T].MoveNext ()
System.Linq
AsyncEnumerable+<Aggregate_>d__6`3[TSource,TAccumulate,TResult].MoveNext ()
AgExpert.Mobile.Field.Core.Repositories
ActivityRepository+<List>d__2.MoveNext ()
AgExpert.Mobile.Field.Core.Services.Data
ActivityDataService+<ListActivities>d__6.MoveNext ()
AgExpert.Mobile.Field.ViewModels
ActivityListViewModel+<<LoadData>b__31_0>d.MoveNext ()
AgExpert.Mobile.Field.ViewModels
BaseViewModel+<RunSafeAsync>d__69.MoveNext ()

Steps to reproduce

we cannot reproduce this locally, this is happening in production.

Screen Shot 2019-07-24 at 11 47 31 AM
public async Task<IEnumerable<ActivityListModel>> List(int skip, int pageSize, Expression<Func<Activity, bool>> predicate, CancellationToken token)
{
    var activities = await Activities.AsNoTracking()
        .Include(x => x.FieldCropYear.Field)
        .Include(x => x.SeedCropYear.SeedInput.SeedVariety.Seed)
        .Include(x => x.Seeds)
        .ThenInclude(x => x.SeedCropYear.SeedInput.SeedVariety.Seed)
        .Include(x => x.Fertilizers)
        .ThenInclude(x => x.FertilizerCropYear.FertilizerInput)
        .Include(x => x.Products)
        .ThenInclude(x => x.ProductCropYear.ProductInput.Product)
        .Where(predicate)
        .OrderByDescending(x => x.StartDate)
        .ThenByDescending(x => x.CreatedAt)
        // we have to group instead of distinct because EF doesn't play nice with Distinct + Skip/Take
        // GroupBy comes after ordering because we can't order an IGrouping
        .GroupBy(x => x.Id) 
        .Skip(skip)
        .Take(pageSize)
        // now we grab the first item of each group, since the group is a collection of the same record
        .Select(activity => new ActivityListModel(activity.First())) 
        .ToListAsync(token)
        .ConfigureAwait(false);

    return activities;
}

Further technical details

EF Core version: 2.2.4 Database Provider: Microsoft.EntityFrameworkCore.Sqlite Operating system: iOS 12.* IDE: Not applicable, but we use Rider and VS Mac

ChaseFlorell commented 5 years ago

Possibly related to #11294

divega commented 5 years ago

@ChaseFlorell do you see any indications that this is specific to running on iOS?

ChaseFlorell commented 5 years ago

@divega well it is landing in our iOS insights, which is separate from our Android insights.

ChaseFlorell commented 5 years ago
Screen Shot 2019-07-25 at 12 46 42 PM Screen Shot 2019-07-25 at 12 46 57 PM
divega commented 5 years ago

@ChaseFlorell in this case I would recommend reporting the issue on https://github.com/xamarin/xamarin-macios/issues/new. I anticipate that they will have some of the same challenges to investigate the issue without a repro, but at least they will be familiar with any runtime differences between Xamarin on iOS and Android maybe see some clue in the stack traces or dumps.