question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF read operation kills app consistently after 535 accesses.

See original GitHub issue

EF Core kills app after 535 reads from a DBContext.

Replication:

Replication Steps:

  • Open DBContext to Sqlite file.
  • Perform scalar query (like context.XXX.Count() ) at least 535 times.

Expected:

  • Can run query endlessly…

Actual:

  • After 535 reads, the process is quit, without throwing a FirstChange, or UnhandledException, with exit code 3.
  • App always ends exactly on this number every single time.
  • When I try accessing a different DBContext before, the app crashes at a smaller number (implying the number is a sum…).

Checked the Following:

Tried the following, in a loop:

  • Creating fresh context from the same database, without any operations -> no problem
  • Creating fresh context from the same database, with one operation per context -> problem exists
  • Creating fresh context from a different database, with one operation per context -> problem exists
  • Using an empty, non-spatial table -> problem exists

Tried the following:

  • Ensured all on one thread, no async.
  • Wait between calls, GC.Collect() & GC.WaitForPendingFinalizers().
  • Nothing on Trace.
  • Turned off antivirus
  • Replaced IQuerable’s Count() to IEnumerable’s ToArray().Count().

Code:

In the console app, the Main()

static void Main(string[] args)
{
    // logging, exception handling

    using (var context = new XXXDbContext (...))
    {
        var n = 0;
        while (true)
        {
            n++;
            Thread.Sleep(100);

            Console.WriteLine($"Reading {n}");
            Console.WriteLine($"Got {context.XXX.Count()}");
        }
    }
}

Trimmed XXXDbContext down to:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating (modelBuilder);

    modelBuilder.Entity<XXX>()
        .Property(_ => _.ID)
        .Metadata.SetBeforeSaveBehavior(PropertySaveBehavior.Ignore);

    ...
}

and:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    base.OnConfiguring(optionsBuilder);

    // ...

    optionsBuilder.UseSqlite(@"Data Source=C:\XXX\a.sqlite", x => x.UseNetTopologySuite());

    // ...
}

Output:

Nothing on Trace, output from Visual Studio console window:

Got 0
Reading 534
Got 0
Reading 535

C:\trunk3\source\XXX\ConsoleAppTester\bin\Debug\netcoreapp3.1\ConsoleAppTester.exe (process 44764) exited with code 3.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Attaching last few lines from ProcMon (hope this helps):

5:50:50.2998972 PM	ConsoleAppTester.exe	58300	IRP_MJ_CLOSE	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.14\System.Text.RegularExpressions.dll	SUCCESS	
5:50:50.2999041 PM	ConsoleAppTester.exe	58300	CloseFile	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.14\System.Runtime.InteropServices.RuntimeInformation.dll	SUCCESS	
5:50:50.2999112 PM	ConsoleAppTester.exe	58300	IRP_MJ_CLOSE	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.14\System.Runtime.InteropServices.RuntimeInformation.dll	SUCCESS	
5:50:50.3000064 PM	ConsoleAppTester.exe	58300	CloseFile	C:\trunk3\source\XXX\ConsoleAppTester\bin\Debug\netcoreapp3.1\Microsoft.Extensions.DependencyModel.dll	SUCCESS	
5:50:50.3000139 PM	ConsoleAppTester.exe	58300	IRP_MJ_CLOSE	C:\trunk3\source\XXX\ConsoleAppTester\bin\Debug\netcoreapp3.1\Microsoft.Extensions.DependencyModel.dll	SUCCESS	
5:50:50.3000212 PM	ConsoleAppTester.exe	58300	CloseFile	C:\trunk3\source\XXX\ConsoleAppTester\bin\Debug\netcoreapp3.1\Microsoft.DotNet.PlatformAbstractions.dll	SUCCESS	
5:50:50.3000281 PM	ConsoleAppTester.exe	58300	IRP_MJ_CLOSE	C:\trunk3\source\XXX\ConsoleAppTester\bin\Debug\netcoreapp3.1\Microsoft.DotNet.PlatformAbstractions.dll	SUCCESS	
5:50:50.3000357 PM	ConsoleAppTester.exe	58300	CloseFile	C:\trunk3\source\XXX\ConsoleAppTester\bin\Debug\netcoreapp3.1\System.Text.Json.dll	SUCCESS	
5:50:50.3000426 PM	ConsoleAppTester.exe	58300	IRP_MJ_CLOSE	C:\trunk3\source\XXX\ConsoleAppTester\bin\Debug\netcoreapp3.1\System.Text.Json.dll	SUCCESS	
5:50:50.3000491 PM	ConsoleAppTester.exe	58300	CloseFile	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.14\System.Buffers.dll	SUCCESS	
5:50:50.3000562 PM	ConsoleAppTester.exe	58300	IRP_MJ_CLOSE	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.14\System.Buffers.dll	SUCCESS	
5:50:50.3000629 PM	ConsoleAppTester.exe	58300	CloseFile	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.14\System.Runtime.CompilerServices.Unsafe.dll	SUCCESS	
5:50:50.3000697 PM	ConsoleAppTester.exe	58300	IRP_MJ_CLOSE	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.14\System.Runtime.CompilerServices.Unsafe.dll	SUCCESS	
5:50:50.3000775 PM	ConsoleAppTester.exe	58300	CloseFile	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.14\System.Numerics.Vectors.dll	SUCCESS	
5:50:50.3000856 PM	ConsoleAppTester.exe	58300	IRP_MJ_CLOSE	C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.14\System.Numerics.Vectors.dll	SUCCESS	
5:50:50.3000952 PM	ConsoleAppTester.exe	58300	RegCloseKey	HKLM\System\CurrentControlSet\Control\Nls\CodePage	SUCCESS	
5:50:50.3005309 PM	ConsoleAppTester.exe	58300	RegCloseKey	HKCU\Software\Classes	SUCCESS	
5:50:50.3007069 PM	ConsoleAppTester.exe	58300	CloseFile	C:\XXX\a.sqlite	SUCCESS	
5:50:50.3089956 PM	ConsoleAppTester.exe	58300	IRP_MJ_CLOSE	C:\...\ConsoleAppTester\bin\Debug\netcoreapp3.1\ConsoleAppTester.exe	SUCCESS	

and the the last line’s Stack Trace:

0	FLTMGR.SYS	FltDecodeParameters + 0x1e3c	0xfffff8008232608c	C:\WINDOWS\System32\drivers\FLTMGR.SYS
1	FLTMGR.SYS	FltDecodeParameters + 0x18e7	0xfffff80082325b37	C:\WINDOWS\System32\drivers\FLTMGR.SYS
2	FLTMGR.SYS	FltDecodeParameters + 0x8f6	0xfffff80082324b46	C:\WINDOWS\System32\drivers\FLTMGR.SYS
3	FLTMGR.SYS	FltDecodeParameters + 0x66b	0xfffff800823248bb	C:\WINDOWS\System32\drivers\FLTMGR.SYS
4	ntoskrnl.exe	IofCallDriver + 0x55	0xfffff80080052f55	C:\WINDOWS\system32\ntoskrnl.exe
5	ntoskrnl.exe	NtQueryInformationFile + 0xf9a	0xfffff80080400f9a	C:\WINDOWS\system32\ntoskrnl.exe
6	ntoskrnl.exe	NtSetEvent + 0x190	0xfffff800803fb300	C:\WINDOWS\system32\ntoskrnl.exe
7	ntoskrnl.exe	ObfDereferenceObjectWithTag + 0xc7	0xfffff80080061277	C:\WINDOWS\system32\ntoskrnl.exe
8	ntoskrnl.exe	PcwAddInstance + 0x164e	0xfffff8008045dd1e	C:\WINDOWS\system32\ntoskrnl.exe
9	ntoskrnl.exe	KeUserModeCallback + 0x1786	0xfffff80080506a96	C:\WINDOWS\system32\ntoskrnl.exe
10	ntoskrnl.exe	LpcRequestPort + 0x2a2e	0xfffff80080509f8e	C:\WINDOWS\system32\ntoskrnl.exe
11	ntoskrnl.exe	setjmpex + 0x7c25	0xfffff800802077b5	C:\WINDOWS\system32\ntoskrnl.exe

System Information:

EF Core version: 5.0.6 (previously, tried also on 5.0.1) Database provider: SQLite Target framework: netcoreapp3.1 (console; previously, tried also in WPF) Operating system: Microsoft Windows 10 Pro / 10.0.19042 N/A Build 19042 IDE: Microsoft Visual Studio Professional 2019 / Version 16.8.5

.NET SDK (reflecting any global.json):
 Version:   5.0.104
 Commit:    ca6b6acadb

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.104\

Host (useful for support):
  Version: 5.0.5
  Commit:  2f740adc14

.NET SDKs installed:
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.523 [C:\Program Files\dotnet\sdk]
  3.1.408 [C:\Program Files\dotnet\sdk]
  5.0.103 [C:\Program Files\dotnet\sdk]
  5.0.104 [C:\Program Files\dotnet\sdk]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
AndroidEFCoreLovercommented, Aug 10, 2021

Got it going - downgraded apps to be 32 bit, and the 32bit works.

2reactions
vonzshikcommented, May 14, 2021

Also reproducible with SQLitePCLRaw, which suggests the problem is with either sqlite or mod_spatialite. The error message is ‘The specified module could not be found’.

using System;
using static SQLitePCL.raw;

static void Main(string[] args)
{
	var counter = 1;

	SQLitePCL.Batteries.Init();

	while (true)
	{
		Console.WriteLine(counter);
		counter++;
		var rc = sqlite3_open("a.db", out var db);
		HandleRc(rc);

		using var _ = db;

		rc = sqlite3_enable_load_extension(db, 1);
		HandleRc(rc);

		rc = sqlite3_exec(db, "SELECT load_extension('mod_spatialite.dll');", out var errorMessage);
		HandleRc(rc, errorMessage);
	}
}

static void HandleRc(int rc, string errorMessage = null)
{
	if (rc != 0)
	{
		throw string.IsNullOrEmpty(errorMessage) 
			? new Exception($"{sqlite3_errstr(rc).utf8_to_string()}")
			: new Exception(errorMessage);
	}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to force Entity Framework to always get updated data ...
I found that using AsNoTracking() method while querying disables the tracking and gets fresh data from the database. However, since EF does not ......
Read more >
DbContext Lifetime, Configuration, and Initialization
Asynchronous operation pitfalls​​ Always await EF Core asynchronous methods immediately.
Read more >
ThinkSystem RAID 930-x xGB Flash PCIe 12Gb Adapters2Gb ...
ThinkSystem RAID 930-x xGB Flash PCIe 12Gb Adapters2Gb Adapters update BIOS and Firmware (For Windows)
Read more >
IBM Tivoli Storage Manager for Linux: Administrator's Reference
In the usage and descriptions for administrative commands, the term characters corresponds to the number of bytes available to store an item.
Read more >
The Global Warming Issue - Sep 2007 - Page 20 - Google Books Result
In our backyard, 60 percent of Sierra Nevada glaciers and 40 percent of the perennial ice in North Cascades National Park has melted...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found