SqlClient: Microsoft.Data.SqlClient.SNI.x64.dll crashes when using multiple AppDomains in IIS Express and IIS Server
Describe the bug
In my .NetFramework 4.8 web app, I “Start Wihout Debugging” and everything works fine, bu when I rebuild the project, I get the errror below. I have to “Start Without Debugging” again, to make the website work again (and then it crashes againg when rebuilding etc).
From event viewer (sorry it is in Danish):
Program. Application Error Event ID 1000
Navn på program med fejl: iisexpress.exe, version: 10.0.22377.1000, tidsstempel: 0x8db0c0a2 Navn på modul med fejl: Microsoft.Data.SqlClient.SNI.x64.dll, version: 4.0.0.0, tidsstempel: 0x61953095 Undtagelseskode: 0xc0000409 Forskydning med fejl 0x000000000001c80c Proces-id 0xff14 Programmets starttidspunkt 0x01d7e767da588b02 Programsti: C:\Program Files\IIS Express\iisexpress.exe Modulsti: C:\Users\mora\AppData\Local\Temp\Temporary ASP.NET Files\vs\1453ae1f\938c099f\assembly\dl3\7084a998\0061005c_e1dcd701\Microsoft.Data.SqlClient.SNI.x64.dll Rapport-id: a6347776-fe45-45b4-a202-d971b9ac10a9 Fuldt navn på program med fejl: Relativt program-id for program med fejl:
Exception message:
None
Stack trace:
None
To reproduce
Include a complete code listing (or project/solution) that we can run to reproduce the issue.
Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.
I can't reproduce with a new project.
Expected behavior
Not crashing iisexpress.exe and let me build and refresh the website.
Further technical details
Microsoft.Data.SqlClient version: 4.0.0.0 .NET target: Framework 4.8 SQL Server version: Sql Server 2019 Operating system: Windows 11 Pro x64
Additional context Add any other context about the problem here.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 5
- Comments: 65 (27 by maintainers)
I did some research and found the difference between my local environment and the environment on the pipeline that generates the SNI GA build. I found out that I was using an older version of the Windows 10 SDK
10.0.19041.0
and the pipeline vm has all the versions of the Window SDK installed including the latest i.e.10.0.22000
and the SNI project referencesWindows 10
SDK without a specific version, so it uses the latest one installed. When I installed the latest Windows 10 SDK, I was able to reproduce the crash locally on my sample project and found out that the crash occurred inside of the registration ofTraceLoggerProvider
and compared the difference between the 2 versions of the Windows 10 SDK. It turns out a fatal error was introduced in the latest Windows 10 SDK version to prevent multiple registrations of the sameTraceLoggingProvider
(i.e. when using multiple AppDomain) due to a memory leak and memory corruption, so I’ve opened a PR to fix the multiple registrations issue. Hopefully, the fix will be included in the next 5.0 hotfix release and it might need to be back ported to the previous versions as well.The PR has been merged and will be in the next release.
@JRahnama I was able to create a repro of this problem, though not involving IISExpress it sounds similar to the scenario described by @leonmeijer, which is also the problem we we’re seeing, and I suspect it has the same underlying cause.
Here is a small standalone repro that exhibits this problem when using AppDomains in .NET Framework 4.8:
Program.cs:
ConsoleApp1.csproj:
And below is the output when running it, first with version 3.0.1 of Microsoft.Data.SqlClient, and then with version 4.1.0 (run on Windows 10 with latest updates in this scenario, but problem also observed on Windows Server 2019 and 2022)
The event log contains the following error entry after the second execution:
If running from Visual Studio with Native Debugging enabled and break on exceptions, we can see the following exception text at the call to
connection.Open()
from the other AppDomain:Two recent hotfixes 4.0.2 and 4.1.1 ship this fix. And, the next version 5.1 will be released till end of this year.
Release Notes
We are having the same issue as described by @alphaleonis. We are loading multiple appdomains in a Windows Service and executing EF queries and after we updated our dependencies our app is now crashing if 2 app domains are simultaniously making queries to the database.
With the code that can reproduce the issue provided by @alphaleonis is there an update yet on this issue? This should be regarded as a high priority bug IMHO.
Edit
I reverted my package version of
Microsoft.Data.SqlClient.SNI
back to2.1.1
and the problem does not occur. I have not tried3.0.0
.Sorry, I realized that I was testing a 5.0 dev SNI build, which worked. I just re-tried all 3 repro samples again using
v5.0.0
from nuget, and it does continue to fail when multiple AppDomain instances are used. We’ll have to further investigate and get back to you soon.@lcheunglci We’re still seeing the crash in v5.0.0 with the sample projects provided. With #1418 (comment) as an example, it’s still crashing on both x32 and x64 as shown below. The reproduction of the crash when using multiple appdomains is pretty consistent, but since most of the repros are console apps, you may not be seeing the crash unless you’re looking in the event log or output, since it blows up the whole process. This is an issue not just for console apps but also for any use in IIS which makes regular use of appdomains with a .NET Framework app.
The MDS v5.0.0 just went live, and I tried the 3 sample projects provided here: https://github.com/dotnet/SqlClient/issues/1418#issuecomment-1048797682 , https://github.com/dotnet/SqlClient/issues/1418#issuecomment-1121276135 , and https://github.com/dotnet/SqlClient/issues/1418#issuecomment-1121359393 and it no longer crashed in the Microsoft.Data.SqlClient.SNI.dll.
Yes, the fix will resolve this issue. As the issue lies in the
registerProvider
SNI method when registering the same provider more than once.Please backport to 4.0 as well, as we cannot uptake a non-LTS release.
My repro above exhibits the same issue with 5.0-preview3 as well.
I think the MDS Team is currently in the middle of a release, and the samples provided should be sufficient to reproduce the issue since I was able to run into the same crash. From debugging the code, I believe I ran into a invalid parameter exception in the native side of MDS SNI.dll, but wasn’t able to dive much deeper since I would need the pdb files to the SNI.dll included, so I wasn’t able to extract much info out of it. I don’t think the SNI code is open sourced, so only the MDS team is able to further investigate, so it’s currently on hold on again.
Thanks @HamsterExAstris for providing the repro sample. I was able able to reproduce what you mentioned with it and I’ll debug and diff the changes between v
3.1
and v4.1
and see if I could figure out what changes caused it to break in4.1
.I’m glad to announce that Microsoft.Data.SqlClient v5.0.1 has been released.
A patch for 5.0 is needed asap as well.
The thing is that I get this crash with MDS 4.1.0 w/ MDS.SNI 4.0.0, but it seems to work when using MDS 4.1.0 w/ MDS.SNI 3.0.0 😅
This issue is still tagged as waiting for customer and needs more info. It looks like repro samples have been provided. Does the MDS team still need more information to fix this?
Here is a repo with ASP.NET MVC basic project template reproducing the crash of IIS (https://github.com/antymon4o/SqlClient_issue_1418). The Connection is opened in ValuesController, action Get(). You should set (hardcode) the connection string in ValuesController.cs file. The configuration of the IIS site is detailed in the README file of the repo. One application pool is used for the site and virtual application resulting in loading Microsoft.Data.SqlClient.SNI.x64.dll in one process but in different application domains. After the crash in Event Viewer there is this Error
Here’s a console-based repro. (Sorry it’s not quite as minimal as the ones above.) No MARS or MSF in use. It works with MDS v2.1.1 and v3.1, and in v4.1 with only one AppDomain, but bombs on v4.1 with two AppDomains.
SqlClientCrashTest.zip
@lcheunglci Thank you for investigating this.
FWIW, we aren’t using MARS or MSF with MDS and we’re experiencing this crash with MDS 4.1. We’re currently running MDS 3.1 in production.
We recently upgraded our ASP.NET / .NET Framework 4.8 web app to use Microsoft.Data.SqlClient Version 4.1 and Microsoft.Data.SqlClient.SNI Version 4.0.
The new package worked fine locally and in our development environment, but when we deployed our app to our QA server the IIS application pool immediately crashes when starting up:
We upgraded primarily because we lost SQL statement logging using Application Insights with Microsoft.Data.SqlClient 3.0 and upgrading to 4.0 seemed to fix it.
@JRahnama
[Serializable]
doesn’t make a difference if theDbTest
class derives fromMarshalByRefObject
. Of course, this sample works if we add[Serializable]
and remove the inheritance fromMarshalByRefObject
, but then we’re testing something else entirely. (The problem occurrs when we have SqlConnections opened in multiple AppDomains)