Repository navigation
Description
Describe the bug
About once or twice a week on production (using Azure SQL) we get an unobserved null reference exception.
I've run some analyzers on our code and there's not a single task that is not awaited.
Today we had a caught exception right before the unobserved one (Microsoft.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired.).
The stack trace doesn't indicate any code from us and it doesn't seem related to above (I traced all code paths to ensure its error indeed caught the exceptions).
System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (Object reference not set to an instance of an object.)
---> System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Data.SqlClient.TdsParserStateObject.ReadAsyncCallback(IntPtr key, PacketHandle packet, UInt32 error)
at Microsoft.Data.SqlClient.TdsParserStateObject.ReadAsyncCallback(PacketHandle packet, UInt32 error)
at Microsoft.Data.SqlClient.ManagedSni.SniMarsHandle.HandleReceiveError(SniPacket packet)
at Microsoft.Data.SqlClient.ManagedSni.SniMarsConnection.HandleReceiveError(SniPacket packet)
at Microsoft.Data.SqlClient.ManagedSni.SniMarsConnection.HandleReceiveComplete(SniPacket packet, UInt32 sniErrorCode)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of inner exception stack trace ---
To reproduce
Not really any reproduction since it feels like there's nothing we're doing specifically, though maybe worth noting it relates to a relatively intense and regular SQL activity (we're using a sp_getapplock regulary inside this method).
Perhaps worth noting we're wrapping the SQL client with Dapper.
Expected behavior
No unobserved task exceptions
Further technical details
Microsoft.Data.SqlClient version: 6.1.2
.NET target: net9
SQL Server version: Azure SQL
Operating system: Docker container running Ubuntu
Additional context
None, but happy to expose anything useful.
Similar to #746
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIn validation
Activity
apoorvdeshmukh commentedon Oct 27, 2025
Thank you @janpieterz for reporting this.
Do you see this issue only with SqlClient 6.1.2 or any of the supported earlier versions as well?
janpieterz commentedon Oct 27, 2025
@apoorvdeshmukh We've seen an increase moving to 6.1.2 from 6.1.1, but can't speak for sure if that's a red herring or not.
This weekend we have a good couple more cases, with no previous exception (so the initial comment with the timeout exception is unrelated, though perhaps it does indicate a relation to some kind of underlying database instability causing it).
It's not reliable, as in, it happens now and then, some hours nothing this weekend, then a few hours regular ones in a row. See screenshot below.
apoorvdeshmukh commentedon Oct 28, 2025
@janpieterz Is it possible for you to try with the earlier versions of the driver such as 5.1 or 5.2?
janpieterz commentedon Oct 29, 2025
@apoorvdeshmukh yea, downgraded to 5.2.3 and will let you know in a week or two if we've had recurrences.
janpieterz commentedon Nov 12, 2025
@apoorvdeshmukh we've had no issues while it was downgraded to 5.2.3. Will upgrade to latest version today to see if it comes back.
janpieterz commentedon Nov 13, 2025
@apoorvdeshmukh having it back on our live environment for a few hours we're experiencing it again. Happy to give any info to help isolate, also see that there's been some pull request activity mentioning this.
Repro Test for #3720 (Unobserved Exception) (#4460)
billpeet commentedon Sep 11, 2026
A reliable reproduction of the same finalizer-thread NRE, this time on the receive re-arm path (SniTcpHandle.ReceiveAsync after Dispose nulls _stream) rather than HandleReceiveError, is in #4679 with a small standalone project: https://github.com/billpeet/SqlClientMarsReceiveNreRepro. Hits within ~50-200 iterations on Ubuntu 24.04 / .NET 10 / MDS 7.0.2 with MARS on, never with MARS off.
priyankatiwari08 commentedon Sep 17, 2026
/triage
github-actions commentedon Sep 17, 2026
🔍 Triage Summary (on-demand re-triage)
Area\Managed SNIAnalysis
This is an intermittent
TaskScheduler.UnobservedTaskExceptionwrapping aNullReferenceExceptionthat surfaces on the finalizer thread fromTdsParserStateObject.ReadAsyncCallback→ managed SNI (SniMarsHandle/SniMarsConnection) receive-completion path, occurring under regular/intense async SQL activity (MARS-adjacent) against Azure SQL. The reporter's own bisection (5.2.3 clean, later versions reproduce) plus the closely related, actively-repro'd issue #4679 point to a race in the managed SNI receive/teardown path where a physical connection handle can be disposed (nulling internal state) while the demuxer is still re-arming a receive for another session, causing an unobserved NRE in the completion callback instead of a handledSNI_ERROR. Severity: P2 — not user-crashing but noisy in production and a real reliability/observability regression; #4679 already provides a reliable repro and a candidate fix (PR #4680) for what looks like the same underlying defect class.Next Steps
SniTcpHandle/_streamnulled concurrently with a demuxer-initiated re-arm) is consistent with this issue's stack trace (ReadAsyncCallback→SniMarsHandle.HandleReceiveError→SniMarsConnection.HandleReceiveComplete) and may be the same root cause.TdsParserStateObjectManaged/ManagedSnireceive-callback paths for missing disposed/null checks analogous to the fix applied for Managed SNI + MARS: unobserved NullReferenceException in SniTcpHandle.ReceiveAsync when the physical connection is disposed while the demuxer is still receiving (reliable repro) #4679, specifically aroundReadAsyncCallbackandSniMarsHandle.HandleReceiveError.cheenamalhotra commentedon Nov 11, 2026
@janpieterz
Could you please try v8.0.0-preview1 and let us know if you still observe this issue? #4680 will likely address this issue as well, but needs to be validated in customer space.