Skip to content

"Internal connection fatal error" from TdsParser.TryRun #4299

Description

@initram

Describe the bug

I am trying to upgrade an older ASP.NET app running on .Net Framework 4.8 to use Microsoft.Data.SqlClient. But when running the app after upgrading I run into errors early during startup. It is able to connect and make requests, but it fails on the forth connection that it makes.

It always fails on the same piece of code (a call to ExecuteScalar()), but if that code is run in isolation, then there are no problems.

Exception message: System.InvalidOperationException: Internal connection fatal error. Error state: 15, Token : 50
Stack trace:   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at Microsoft.Data.SqlClient.SqlDataReader.TryCloseInternal(Boolean closeReader)
   at Microsoft.Data.SqlClient.SqlDataReader.Close()
   at Microsoft.Data.SqlClient.SqlCommand.CompleteExecuteScalar(SqlDataReader reader, Boolean returnLastResult)
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteScalar()
   at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Object execObject, DataSet fillDataSet, Boolean catchException) in /_/src/Microsoft/SqlServer/Management/ConnectionInfo/ConnectionManager.cs:line 808
   at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteScalar(String sqlCommand) in /_/src/Microsoft/SqlServer/Management/ConnectionInfo/ServerConnection.cs:line 1516
   at Geomatic.Framework.Data.Impl.DbManagement.ExecuteScalar(String sql) in C:\dev\Development\src\Framework\app\Geomatic.Framework\Data\Impl\DbManagement.cs:line 111
   at Geomatic.Framework.Data.Impl.DbManagement.ExecuteScalar[T](String sql) in C:\dev\Development\src\Framework\app\Geomatic.Framework\Data\Impl\DbManagement.cs:line 101
   at Geomatic.Framework.Deployment.DbDeployment.GetVersionFromDatabase(ConnectionDef connectionDef) in C:\dev\Development\src\Framework\app\Geomatic.Framework\Deployment\DbDeployment.cs:line 316
   at Geomatic.Framework.Deployment.DbDeployment.IsVersionCompatible(TimestampedDatabase timestampedDatabase, Boolean forRemovingObsoleteDatabases) in C:\dev\Development\src\Framework\app\Geomatic.Framework\Deployment\DbDeployment.cs:line 268
   at Geomatic.Framework.Deployment.DbDeployment.<>c__DisplayClass19_0.<GetDatabaseSets>b__0(TimestampedDatabase x) in C:\dev\Development\src\Framework\app\Geomatic.Framework\Deployment\DbDeployment.cs:line 223
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Geomatic.Framework.Deployment.DbDeployment.GetDatabaseSets(IList`1 databaseSetNames, Boolean forRemovingObsoleteDatabases) in C:\dev\Development\src\Framework\app\Geomatic.Framework\Deployment\DbDeployment.cs:line 221
   at Geomatic.Framework.Deployment.DbDeployment.<GetLatestPrimaryDatabases>d__11.MoveNext() in C:\dev\Development\src\Framework\app\Geomatic.Framework\Deployment\DbDeployment.cs:line 77
   at Geomatic.Framework.Deployment.WebDbDeployment.UpdateCurrentWebAppConnectionStrings()
   at Geomatic.Framework.Deployment.WebDbDeployment.UpdateWebAppConnectionStrings()
   at Geomatic.MosaicProspect.GlobalAsax.Application_Start(Object sender, EventArgs e)]

To reproduce

I do not have a minimal way of reproducing this issue without sending over a lot of code and databases.

I am hoping to find some way of creating a smaller reproducible example, but I am not sure what causes the problem, so it is hard to construct it. Any input would be appreciated.

Expected behavior

I expected it the code to work in a similar fashion to System.Data.SqlClient, or at least get an exception with a helpful message that can be used for debugging. The error that I get leads nowhere and it is very hard for me to know where to look and what to change.

Further technical details

Microsoft.Data.SqlClient version: (found on the nuget or Microsoft.Data.SqlClient.dll)
.NET target: .NET Framework 4.8
SQL Server version: SQL Server 2016
Operating system: Windows 11 client calling the server

Additional context
Connection string is in this format: Data Source=SERVER_NAME;Initial Catalog=DB_NAME;Integrated Security=True;Pooling=False;Encrypt=False (I have also tried without the Pooling=False, it still fails the same way)

Here is the logged output of diagnostics tracing:

2026-05-20 12:44:07,533 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionOpenBefore]: [OperationId, 98d0b7aa-74c7-460e-beb1-c20b690501b4], [Operation, Open], [Timestamp, 1576111278735], [Connection, Microsoft.Data.SqlClient.SqlConnection], [ClientVersion, 7.0.1.26114]
2026-05-20 12:44:07,912 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionOpenAfter]: [OperationId, 98d0b7aa-74c7-460e-beb1-c20b690501b4], [Operation, Open], [Timestamp, 1576115231063], [ConnectionId, 20931833-8f54-420d-932e-e6a7c3eae471], [Connection, Microsoft.Data.SqlClient.SqlConnection], [ClientVersion, 7.0.1.26114], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:07,951 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandBefore]: [OperationId, 245ce2e6-f9f2-4024-9b55-0c04f07db71d], [Operation, ExecuteReader], [Timestamp, 1576115720932], [ConnectionId, 20931833-8f54-420d-932e-e6a7c3eae471], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand]
2026-05-20 12:44:08,083 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandAfter]: [OperationId, 245ce2e6-f9f2-4024-9b55-0c04f07db71d], [Operation, ExecuteReader], [Timestamp, 1576116743162], [ConnectionId, 20931833-8f54-420d-932e-e6a7c3eae471], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,121 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionCloseBefore]: [OperationId, 3b8ba6d0-1346-4f89-a156-267448c9940a], [Operation, Close], [Timestamp, 1576117337187], [ConnectionId, 20931833-8f54-420d-932e-e6a7c3eae471], [Connection, Microsoft.Data.SqlClient.SqlConnection], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,131 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionCloseAfter]: [OperationId, 3b8ba6d0-1346-4f89-a156-267448c9940a], [Operation, Close], [Timestamp, 1576117443033], [ConnectionId, 20931833-8f54-420d-932e-e6a7c3eae471], [Connection, Microsoft.Data.SqlClient.SqlConnection], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,139 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionOpenBefore]: [OperationId, 62f03a5b-47ee-454a-8727-9cd8509f6c49], [Operation, Open], [Timestamp, 1576117556101], [Connection, Microsoft.Data.SqlClient.SqlConnection], [ClientVersion, 7.0.1.26114]
2026-05-20 12:44:08,139 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionOpenAfter]: [OperationId, 62f03a5b-47ee-454a-8727-9cd8509f6c49], [Operation, Open], [Timestamp, 1576117593438], [ConnectionId, 20931833-8f54-420d-932e-e6a7c3eae471], [Connection, Microsoft.Data.SqlClient.SqlConnection], [ClientVersion, 7.0.1.26114], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,139 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandBefore]: [OperationId, f8f941ae-fc97-4b12-b765-c5ce24ecfeea], [Operation, ExecuteReader], [Timestamp, 1576117615849], [ConnectionId, 20931833-8f54-420d-932e-e6a7c3eae471], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand]
2026-05-20 12:44:08,202 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandAfter]: [OperationId, f8f941ae-fc97-4b12-b765-c5ce24ecfeea], [Operation, ExecuteReader], [Timestamp, 1576118158488], [ConnectionId, 20931833-8f54-420d-932e-e6a7c3eae471], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,202 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionCloseBefore]: [OperationId, 7358b529-bdd8-49ae-852f-46e3115acff0], [Operation, Close], [Timestamp, 1576118224736], [ConnectionId, 20931833-8f54-420d-932e-e6a7c3eae471], [Connection, Microsoft.Data.SqlClient.SqlConnection], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,202 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionCloseAfter]: [OperationId, 7358b529-bdd8-49ae-852f-46e3115acff0], [Operation, Close], [Timestamp, 1576118250841], [ConnectionId, 20931833-8f54-420d-932e-e6a7c3eae471], [Connection, Microsoft.Data.SqlClient.SqlConnection], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,218 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionOpenBefore]: [OperationId, 0d5abfe5-93bb-4de2-9c82-0dab5b438fd4], [Operation, Open], [Timestamp, 1576118411991], [Connection, Microsoft.Data.SqlClient.SqlConnection], [ClientVersion, 7.0.1.26114]
2026-05-20 12:44:08,279 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionOpenAfter]: [OperationId, 0d5abfe5-93bb-4de2-9c82-0dab5b438fd4], [Operation, Open], [Timestamp, 1576118999287], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [Connection, Microsoft.Data.SqlClient.SqlConnection], [ClientVersion, 7.0.1.26114], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,316 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandBefore]: [OperationId, 2ded8a06-8cf0-4f03-a38b-5712091785e9], [Operation, ExecuteReader], [Timestamp, 1576119336915], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand]
2026-05-20 12:44:08,324 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandAfter]: [OperationId, 2ded8a06-8cf0-4f03-a38b-5712091785e9], [Operation, ExecuteReader], [Timestamp, 1576119385308], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,453 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandBefore]: [OperationId, e48f4fa4-4dea-43a1-b0db-81409f5907c9], [Operation, ExecuteScalar], [Timestamp, 1576120772614], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand]
2026-05-20 12:44:08,479 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandAfter]: [OperationId, e48f4fa4-4dea-43a1-b0db-81409f5907c9], [Operation, ExecuteScalar], [Timestamp, 1576120906669], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,644 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandBefore]: [OperationId, 26654182-8023-4dbd-9183-5c77d9c1a962], [Operation, ExecuteReader], [Timestamp, 1576122582579], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand]
2026-05-20 12:44:08,685 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandAfter]: [OperationId, 26654182-8023-4dbd-9183-5c77d9c1a962], [Operation, ExecuteReader], [Timestamp, 1576122954784], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,880 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandBefore]: [OperationId, 6915fba9-3868-45f7-bc54-5ccf8ee1b215], [Operation, ExecuteScalar], [Timestamp, 1576124933736], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand]
2026-05-20 12:44:08,886 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandAfter]: [OperationId, 6915fba9-3868-45f7-bc54-5ccf8ee1b215], [Operation, ExecuteScalar], [Timestamp, 1576124985343], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,898 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandBefore]: [OperationId, 189eaa8a-610b-4c91-a4a3-fc16857e0e4e], [Operation, ExecuteReader], [Timestamp, 1576125124667], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand]
2026-05-20 12:44:08,907 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandAfter]: [OperationId, 189eaa8a-610b-4c91-a4a3-fc16857e0e4e], [Operation, ExecuteReader], [Timestamp, 1576125191594], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,951 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandBefore]: [OperationId, 8cd248f2-04cd-4cec-9710-88d592a6537c], [Operation, ExecuteReader], [Timestamp, 1576125785922], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand]
2026-05-20 12:44:08,966 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandAfter]: [OperationId, 8cd248f2-04cd-4cec-9710-88d592a6537c], [Operation, ExecuteReader], [Timestamp, 1576125897948], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:08,994 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandBefore]: [OperationId, 0d5f401f-d008-467c-ae8a-eed4016189b3], [Operation, ExecuteNonQuery], [Timestamp, 1576126086973], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand]
2026-05-20 12:44:09,003 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandAfter]: [OperationId, 0d5f401f-d008-467c-ae8a-eed4016189b3], [Operation, ExecuteNonQuery], [Timestamp, 1576126150329], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:09,004 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandBefore]: [OperationId, ea1b8540-9be6-40a9-ad3f-73a737597d3f], [Operation, ExecuteReader], [Timestamp, 1576126175309], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand]
2026-05-20 12:44:09,012 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandAfter]: [OperationId, ea1b8540-9be6-40a9-ad3f-73a737597d3f], [Operation, ExecuteReader], [Timestamp, 1576126288953], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:09,044 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionCloseBefore]: [OperationId, ff9f7fac-962e-4a55-b29c-21021479b611], [Operation, Close], [Timestamp, 1576126695944], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [Connection, Microsoft.Data.SqlClient.SqlConnection], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:09,044 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionCloseAfter]: [OperationId, ff9f7fac-962e-4a55-b29c-21021479b611], [Operation, Close], [Timestamp, 1576126725543], [ConnectionId, e2b83a6e-ea16-4b9e-96b1-1270806baf10], [Connection, Microsoft.Data.SqlClient.SqlConnection], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:09,060 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionOpenBefore]: [OperationId, 84116a96-a81a-4879-8d8f-5fa76140dcd7], [Operation, Open], [Timestamp, 1576126740560], [Connection, Microsoft.Data.SqlClient.SqlConnection], [ClientVersion, 7.0.1.26114]
2026-05-20 12:44:09,091 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionOpenAfter]: [OperationId, 84116a96-a81a-4879-8d8f-5fa76140dcd7], [Operation, Open], [Timestamp, 1576127076320], [ConnectionId, 321c1e95-0c4e-4379-938f-68063da2d0c0], [Connection, Microsoft.Data.SqlClient.SqlConnection], [ClientVersion, 7.0.1.26114], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:09,091 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandBefore]: [OperationId, e52e10f9-2ab7-434f-844e-9cabc8a5e034], [Operation, ExecuteScalar], [Timestamp, 1576127094955], [ConnectionId, 321c1e95-0c4e-4379-938f-68063da2d0c0], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand]
2026-05-20 12:44:09,107 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionCloseBefore]: [OperationId, 70252906-72dd-4e30-83c5-e6311aabb8af], [Operation, Close], [Timestamp, 1576127264744], [ConnectionId, 321c1e95-0c4e-4379-938f-68063da2d0c0], [Connection, Microsoft.Data.SqlClient.SqlConnection], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:09,107 [1] INFO  Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteConnectionCloseAfter]: [OperationId, 70252906-72dd-4e30-83c5-e6311aabb8af], [Operation, Close], [Timestamp, 1576127286640], [ConnectionId, 321c1e95-0c4e-4379-938f-68063da2d0c0], [Connection, Microsoft.Data.SqlClient.SqlConnection], [Statistics, Microsoft.Data.SqlClient.SqlStatistics+StatisticsDictionary]
2026-05-20 12:44:14,012 [1] ERROR Geomatic.Framework.Data.Logging.SqlClientDiagnosticTracing [(null)] - SqlClient [Microsoft.Data.SqlClient.WriteCommandError]: [OperationId, e52e10f9-2ab7-434f-844e-9cabc8a5e034], [Operation, ExecuteScalar], [Timestamp, 1576161247039], [ConnectionId, 321c1e95-0c4e-4379-938f-68063da2d0c0], [TransactionId, ], [Command, Microsoft.Data.SqlClient.SqlCommand], [Exception, System.InvalidOperationException: Internal connection fatal error. Error state: 15, Token : 50
   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at Microsoft.Data.SqlClient.SqlDataReader.TryCloseInternal(Boolean closeReader)
   at Microsoft.Data.SqlClient.SqlDataReader.Close()
   at Microsoft.Data.SqlClient.SqlCommand.CompleteExecuteScalar(SqlDataReader reader, Boolean returnLastResult)
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteScalar()
   at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Object execObject, DataSet fillDataSet, Boolean catchException) in /_/src/Microsoft/SqlServer/Management/ConnectionInfo/ConnectionManager.cs:line 808
   at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteScalar(String sqlCommand) in /_/src/Microsoft/SqlServer/Management/ConnectionInfo/ServerConnection.cs:line 1516
   at Geomatic.Framework.Data.Impl.DbManagement.ExecuteScalar(String sql) in C:\dev\Development\src\Framework\app\Geomatic.Framework\Data\Impl\DbManagement.cs:line 111
   at Geomatic.Framework.Data.Impl.DbManagement.ExecuteScalar[T](String sql) in C:\dev\Development\src\Framework\app\Geomatic.Framework\Data\Impl\DbManagement.cs:line 101
   at Geomatic.Framework.Deployment.DbDeployment.GetVersionFromDatabase(ConnectionDef connectionDef) in C:\dev\Development\src\Framework\app\Geomatic.Framework\Deployment\DbDeployment.cs:line 316
   at Geomatic.Framework.Deployment.DbDeployment.IsVersionCompatible(TimestampedDatabase timestampedDatabase, Boolean forRemovingObsoleteDatabases) in C:\dev\Development\src\Framework\app\Geomatic.Framework\Deployment\DbDeployment.cs:line 268
   at Geomatic.Framework.Deployment.DbDeployment.<>c__DisplayClass19_0.<GetDatabaseSets>b__0(TimestampedDatabase x) in C:\dev\Development\src\Framework\app\Geomatic.Framework\Deployment\DbDeployment.cs:line 223
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Geomatic.Framework.Deployment.DbDeployment.GetDatabaseSets(IList`1 databaseSetNames, Boolean forRemovingObsoleteDatabases) in C:\dev\Development\src\Framework\app\Geomatic.Framework\Deployment\DbDeployment.cs:line 221
   at Geomatic.Framework.Deployment.DbDeployment.<GetLatestPrimaryDatabases>d__11.MoveNext() in C:\dev\Development\src\Framework\app\Geomatic.Framework\Deployment\DbDeployment.cs:line 77
   at Geomatic.Framework.Deployment.WebDbDeployment.UpdateCurrentWebAppConnectionStrings()
   at Geomatic.Framework.Deployment.WebDbDeployment.UpdateWebAppConnectionStrings()
   at Geomatic.MosaicProspect.GlobalAsax.Application_Start(Object sender, EventArgs e)]

Activity

  1. changed the title [-]"Internal connection fatal error" from[/-] [+]"Internal connection fatal error" from TdsParser.TryRun[/+] on May 20, 2026
  2. github-actions commented on May 20, 2026

    @github-actions

    🔍 Triage Summary

    Check Result
    Issue type Bug
    Environment ⚠️ Missing: Exact Microsoft.Data.SqlClient NuGet version (template field left as placeholder — version 7.0.1.26114 is visible in the diagnostics trace log)
    Area Area\Netfx
    Duplicates None found (issue #919 has a similar error message but refers to System.Data.SqlClient with a different token and a different root cause)
    Regression Likely regression from System.Data.SqlClient — worked previously, fails after migration to Microsoft.Data.SqlClient

    Analysis

    The reporter is migrating an ASP.NET app from System.Data.SqlClient to Microsoft.Data.SqlClient 7.0.1 on .NET Framework 4.8. The error System.InvalidOperationException: Internal connection fatal error. Error state: 15, Token : 50 is thrown inside TdsParser.TryRun during SqlDataReader.Close() / CompleteExecuteScalar, but only when multiple connections are made in sequence during application startup (specifically the 4th connection). Pooling is disabled, the connection uses Integrated Security against SQL Server 2016, and the issue does not reproduce in isolation. This points to a TDS state machine issue specific to the .NET Framework code path, possibly related to how TdsParser handles token 50 (an unknown/unexpected TDS token during reader cleanup). Severity: P2 — application startup is blocked after migration.

    Next Steps

    • @initram, please confirm the exact NuGet version used (e.g. 7.0.1 or 7.0.0) so we can check changelogs for any related fixes.
    • Assign this issue to the Copilot coding agent to investigate TdsParser.TryRun on the .NET Framework code path — specifically how TDS token 0x32 (decimal 50) is handled during SqlDataReader.TryCloseInternal. The fact that it only occurs on the 4th connection in a sequence (even with Pooling=False) may suggest shared parser/session state or a login-token negotiation side-effect in the .netfx path that differs from System.Data.SqlClient. The agent should also check whether SQL Server 2016 sends any login-response tokens that the newer driver doesn't expect or handle correctly.
    • If possible, try capturing a network trace (e.g. with Wireshark or netsh trace) or enable SqlClientDiagnosticTracing at TRACE level to capture what token is received just before the error.

    Note: This triage summary is auto-generated by an AI agent. The analysis and suggestions above have not been verified by a human maintainer. Please treat as preliminary guidance only.

    Generated by SqlClient Issue Auto-Triage for issue #4299 · ● 3.7M · ◷

  3. initram commented on May 20, 2026

    @initram
    Author

    I am using Microsoft.Data.SqlClient version 7.0.1 from nuget.

  4. dallmair commented on May 20, 2026

    @dallmair

    Same here, both 7.0.0 and 7.0.1 on .NET 4.8 fail this way, haven't tried older versions. Tried to connect to both SQL Server 2019 as well as SQL Server 2025.

  5. cheenamalhotra commented on May 26, 2026

    @cheenamalhotra
    Member

    @initram @dallmair

    From the traces, it seems like your application is sharing physical connection across multiple commands. The trace reveals activity on the connection 321c1e95-0c4e-4379-938f-68063da2d0c0:

    1. WriteConnectionOpenAfter at timestamp 1576127076320
    2. WriteCommandBefore (ExecuteScalar) at 1576127094955
    3. WriteConnectionCloseBefore at 1576127264744 — the connection is being closed while the command is still in progress
    4. WriteCommandError at 1576161247039 — the error fires ~5 seconds later

    The connection is being closed underneath the active command. This is the classic symptom of Microsoft.SqlServer.Management.Common.ServerConnection sharing a single physical SqlConnection across multiple logical operations, where one code path closes the connection while another is still reading results.

    Here's what I would recommend:

    • Review your code is using latest versions of SMO library and are not sharing connections for multiple commands.
    • Avoid sharing connections across operations — Ensure each ExecuteScalar/ExecuteReader call uses its own SMO connection (or at minimum, fully consumes/closes the previous result before issuing the next command on the same connection).
    • Try enabling MARS to see it that helps with the issue.
    • Try an earlier version of Microsoft.Data.SqlClient (e.g., 6.1.x) to see if the behavior differs, which would help narrow whether this is a regression or an existing incompatibility with SMO.
  6. moved this from To triage to Waiting for customer in SqlClient Boardon May 26, 2026
  7. initram commented on May 27, 2026

    @initram
    Author
    • I was using the latest version of SMO library (Microsoft.SqlServer.SqlManagementObjects Version="181.19.0")
    • I had a using on all the connections that wrapped the usage of the connections as far as I can tell
    • I tried to enable MARS. That did not help.

    What worked was downgrading to Microsoft.Data.SqlClient 6.1.5. I can not provide a new diagnostics trace for this version as it does not support it for .Net Framework (as far as I can tell)

    So my code works with System.Data.SqlClient and it works with Microsoft.Data.SqlClient 6.1.5, but it does not work with Microsoft.Data.SqlClient 7.0.1.

    Thanks for helping with this. I will gladly try a new version of 7.x.x, if any fixes are released related to this in the future.

  8. initram commented on May 27, 2026

    @initram
    Author

    What I think is happening with the trace with connection id 321c1e95-0c4e-4379-938f-68063da2d0c0 is that it fails during the command (after WriteCommandBefore, but before WriteCommandAfter) but the tracing does not log the error event before the connection has been closed. I think the connection is being closed as it failed communication and then logged error afterwards.

    I have not looked at the code, but that is what I assume has happened.

  9. added
    Regression 💥
    on May 27, 2026
  10. moved this from Waiting for customer to Backlog in SqlClient Boardon Jun 5, 2026
  11. dallmair commented on Jun 9, 2026

    @dallmair

    Hi @cheenamalhotra and also @initram, thanks for following up. Sorry for the late response from my side, I was oof for a few days.

    Just like @initram, I can also confirm it's a regression in 7.0.0 as 6.1.5 works fine. Digging through the code base of the internal tool I'm encountering this with, I checked every connection instantiation and usage, but everything is fine and disposed of correctly with using blocks and statements, every connection, transaction, command, reader, etc.

    So I've tried to create a minimal reproduction today, and it isn't as easy as it seems. Interestingly, I can run everything just fine in a .NET 4.8 Console App (with 7.0.1), but when I use the exact same code in an ASP.NET 4.8 app hosted in IIS it fails. As there is at Geomatic.MosaicProspect.GlobalAsax.Application_Start in the stack trace posted by @initram in the initial report, it looks like that's the case for both of us.

    Maybe that helps anyone in the team to root cause this and come up with a fix?

  12. initram commented on Jun 9, 2026

    @initram
    Author

    I am also in an asp.net app as you noticed.

  13. 1 remaining item

  14. assigned and unassigned on Sep 22, 2026
  15. modified the milestones: 7.0.3, 8.0.0-preview1 on Oct 1, 2026
  16. mdaigle commented on Oct 1, 2026

    @mdaigle
    Contributor

    @initram @dallmair our theory at the moment is that your application is triggering a ThreadAbortException mid-command. In our testing, manually triggering a ThreadAbortException during ExecuteScalar results in the same stack trace and error message. We suspect that a change introduced in #3535 removed some cleanup we used to do when encountering ThreadAbort, OOM, etc. Now, a close run in a finally statement encounters partial state and fails, obscuring the underlying error. I'll work on a fix for the cleanup so that the proper exception is exposed.

    In the meantime, if you can temporarily register an AppDomain.CurrentDomain.FirstChanceException handler it will help pinpoint the cause of the underlying exception. You can add it at the very beginning of Application_Start, before the first database operation.

    For example:

    AppDomain.CurrentDomain.FirstChanceException += (sender, args) =>
    {
        Log(
            $"{DateTime.UtcNow:O} Thread={Thread.CurrentThread.ManagedThreadId}\n" +
            $"{args.Exception}\n" +
            $"{Environment.StackTrace}");
    };
  17. initram commented on Oct 5, 2026

    @initram
    Author

    Thanks @mdaigle, and sorry for the slow reply.

    I added the FirstChanceException handler at the very beginning of Application_Start, before the first database operation, and retried with Microsoft.Data.SqlClient 7.0.1. It's the same build as in my original trace (7.0.1.26114), again on .NET Framework 4.8 under IIS (Express).

    I can no longer reproduce the error. Startup now runs the same code path as the original stack trace (DbDeployment.GetVersionFromDatabase → SMO ServerConnection.ExecuteScalar). It also rewrites a config file mid-startup, which recycles the AppDomain. That all completes, and the app restarts, lets me sign in and serves pages normally. The handler caught no ThreadAbortException and no SqlClient exceptions, only the usual assembly-probing noise.

    Several things have changed since May, so I can't say which one made the difference:

    • The SQL Server instance has since been patched with the latest security update.
    • We moved to Central Package Management, which changed a lot of our transitive dependency versions.
    • Microsoft.IdentityModel.* went to 8.16.0, as 7.0.1 requires.
    • We corrected a number of stale binding redirects. Among them, SMO 181.19.0 is compiled against Microsoft.Data.SqlClient 6.0.0.0, so it needs a redirect to 7.0.0.0.
    • We now connect without Pooling=False.

    If the cause was on our side, one possibility is that an exception was thrown during the command that wasn't a ThreadAbortException, for example a FileLoadException from a mismatched binding redirect, and that the cleanup issue you describe hid it the same way. I can't confirm this, because I no longer have that exact dependency graph or server state.

    I think your fix for surfacing the underlying exception is still well worth doing. "Internal connection fatal error. Error state: 15, Token : 50" gave us nothing to go on, and @dallmair may still be running into it. If it comes back for us, I'll post the handler output here.

  18. MaceWindu commented on Oct 12, 2026

    @MaceWindu
    Contributor

    We see the same failure on Microsoft.Data.SqlClient 7.0.2, on .NET 10, against SQL Server 2019, in a Windows service. Our case differs from the original report in two ways: the token is 49, and the path is async (an IAsyncEnumerable query through linq2db 6.5.0). The exception comes out of the reader's dispose, not out of a read:

    System.InvalidOperationException: Internal connection fatal error. Error state: 15, Token : 49
       at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
       at Microsoft.Data.SqlClient.SqlDataReader.TryCloseInternal(Boolean closeReader)
       at Microsoft.Data.SqlClient.SqlDataReader.Close()
       at Microsoft.Data.SqlClient.SqlDataReader.Dispose(Boolean disposing)
       at LinqToDB.Data.DataReaderWrapper.DisposeAsync()
       at LinqToDB.Internal.Linq.QueryRunner.BasicResultEnumerable`1.GetAsyncEnumerable(CancellationToken cancellationToken)+MoveNext()
       at LinqToDB.Internal.Async.AsyncEnumeratorAsyncWrapper`1.System.Collections.Generic.IAsyncEnumerator<T>.MoveNextAsync()
       at <application code: await foreach over the query, inside a transaction>
    

    The calling code is roughly this:

    await foreach (var row in query.AsAsyncEnumerable().WithCancellation(cancellationToken))
    {
        if (!Known(row))
            return null; // early exit: the enumerator is disposed with rows possibly unread
        ...
    }

    So the reader is disposed in one of three ways: after reading every row, after an early return from the loop, or after an exception or cancellation. Token 49 (0x31) is not a valid TDS token, so the parser state was already broken when Close() tried to drain the rest of the response. That fits the theory that cleanup no longer happens after an aborted operation (#3535), with dispose hiding the original exception. Here the aborting event would be async cancellation or a connection fault rather than a ThreadAbortException, which doesn't exist on .NET Core.

    In our logs the early-exit branch did not run, and nothing else failed first: no timeout, no cancellation, no earlier exception on the same operation. The whole thing took about 30 ms, from opening the transaction (a fresh connection from the pool) to the error. So either the dispose error hides an exception thrown inside the loop body, or the connection was already in a bad state when it came out of the pool.

    We have not reproduced it on demand; it is a single occurrence in production logs.

  19. moved this from Backlog to In progress in SqlClient Boardon Oct 19, 2026
  20. modified the milestones: 8.0.0, 8.1.0-preview1 on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Regression 💥

Type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions