dotnet / dotnet/SqlClient

Null Reference Exception during command timeout

Open
#4,374 4 comments 0 reactions 1 assignee Claimed by @paulmedynski View on GitHub
Area\Async
Dominant language
C#
Stars
989
Forks
340
Avg merge
4d 19h
Merged PRs (30d)
72

Description

### Describe the bug
NullReferenceException on timeout instead of SqlException.
I see only couple of them in production for the last month.

```
ExceptionDetail_HResult -2147467261
ExceptionDetail_Message Object reference not set to an instance of an object.
ExceptionDetail_Source Microsoft.Data.SqlClient
ExceptionDetail_StackTrace
at Microsoft.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
at Microsoft.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at Microsoft.Data.SqlClient.TdsParser.ProcessAttention(TdsParserStateObject stateObj)
at Microsoft.Data.SqlClient.TdsParser.ProcessPendingAck(TdsParserStateObject stateObj)
at Microsoft.Data.SqlClient.TdsParserStateObject.ResetCancelAndProcessAttention()
at Microsoft.Data.SqlClient.TdsParserStateObject.CloseSession()
at Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryAsync(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlRawAsync(DatabaseFacade databaseFacade, String sql, IEnumerable`1 parameters, CancellationToken cancellationToken)
at ...

ExceptionDetail_TargetSite Void ReadSniSyncOverAsync()
ExceptionDetail_Type System.NullReferenceException
observed_timestamp 1781769665345251900

CommandTimeout '60'
CommandType 'Text',
EventId_Id 20102
EventId_Name Microsoft.EntityFrameworkCore.Database.Command.CommandError
commandText DELETE dbo.SYSException WHERE CreatedOn < @CutoffTime
elapsed 65,002
message_template_text Failed executing DbCommand ({elapsed}ms) [Parameters=[{parameters}], CommandType='{commandType}', CommandTimeout='{commandTimeout}']{newLine}{commandText}
newLine
observed_timestamp 1781769665329176300
parameters @CutoffTime='?' (DbType = DateTime)
scope_name Microsoft.EntityFrameworkCore.Database.Command
```

### Further technical details
Microsoft.Data.SqlClient version: 6.1.2
EF Core: Microsoft.EntityFrameworkCore.SqlServer 9.0.7
.NET target: .Net 9
SQL Server version: SQL Server 2022
Operating system: Windows Server 2022
multipleactiveresultsets=True

**Additional context**
Similar to https://github.com/dotnet/SqlClient/issues/3720

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.