Skip to content

Unobserved null reference exception #3720

Open

Description

@janpieterz

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

Activity

  1. added
    Intermittent 😨
    on Oct 27, 2025
  2. apoorvdeshmukh commented on Oct 27, 2025

    @apoorvdeshmukh
    Contributor

    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?

  3. janpieterz commented on Oct 27, 2025

    @janpieterz
    Author

    @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.

    Image
  4. apoorvdeshmukh commented on Oct 28, 2025

    @apoorvdeshmukh
    Contributor

    @janpieterz Is it possible for you to try with the earlier versions of the driver such as 5.1 or 5.2?

  5. janpieterz commented on Oct 29, 2025

    @janpieterz
    Author

    @apoorvdeshmukh yea, downgraded to 5.2.3 and will let you know in a week or two if we've had recurrences.

  6. janpieterz commented on Nov 12, 2025

    @janpieterz
    Author

    @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.

  7. janpieterz commented on Nov 13, 2025

    @janpieterz
    Author

    @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.

  8. added
    Regression 💥
    on Nov 14, 2025
  9. moved this from To triage to Investigating in SqlClient Boardon Nov 20, 2025
  10. moved this from Investigating to Backlog in SqlClient Boardon Dec 9, 2025
  11. added a commit that references this issue on Jul 28, 2026
  12. billpeet commented on Sep 11, 2026

    @billpeet

    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.

  13. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  14. github-actions commented on Sep 17, 2026

    @github-actions

    🔍 Triage Summary (on-demand re-triage)

    Check Result
    Issue type Bug
    Environment All required environment details provided for investigation
    Area Area\Managed SNI
    Duplicates Potentially related: #746 (closed, older similar signature), #1171, #2026, #4679 (open, has a reliable reproduction and an active fix, PR #4680)
    Regression Likely regression from v5.2.3 — reporter confirmed no recurrences on 5.2.3 and reproduction returned after upgrading back to a later version

    Analysis

    This is an intermittent TaskScheduler.UnobservedTaskException wrapping a NullReferenceException that surfaces on the finalizer thread from TdsParserStateObject.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 handled SNI_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

    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 #3720 · copilot · auto · 73 AIC · ⌖ 13.1 AIC · ⊞ 11.9K · ◷

  15. cheenamalhotra commented on Nov 11, 2026

    @cheenamalhotra
    Member

    @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.

  16. moved this from In progress to In validation in SqlClient Boardon Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Area\AsyncIntermittent 😨Regression 💥

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions