Skip to content

About

Minimal repro: unobserved NullReferenceException in Microsoft.Data.SqlClient managed SNI (MARS) after physical connection teardown

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Unobserved NullReferenceException in managed SNI (MARS) after physical connection teardown

Reproduces the finalizer-thread AggregateException / NullReferenceException reported in dotnet/SqlClient #1171, #2026 and #3720:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Data.SqlClient.ManagedSni.SniPacket.ReadFromStreamAsync(Stream stream)
   at Microsoft.Data.SqlClient.ManagedSni.SniTcpHandle.ReceiveAsync(SniPacket& packet)
   at Microsoft.Data.SqlClient.ManagedSni.SniMarsConnection.ReceiveAsync(SniPacket& packet)
   at Microsoft.Data.SqlClient.ManagedSni.SniMarsConnection.HandleReceiveComplete(SniPacket packet, UInt32 sniErrorCode)
   at Microsoft.Data.SqlClient.ManagedSni.SniPacket.InvokeAsyncIOCompletionCallback(UInt32 sniErrorCode)
   at Microsoft.Data.SqlClient.ManagedSni.SniPacket.ReadFromStreamAsyncContinuation(Task`1 task, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)

(In production builds the JIT inlines `ReadFromStreamAsync` and `InvokeAsyncIOCompletionCallback`, so
the reported trace is usually the shorter SniTcpHandle.ReceiveAsync / SniMarsConnection.ReceiveAsync /
HandleReceiveComplete / ReadFromStreamAsyncContinuation chain.)

Mechanism (Microsoft.Data.SqlClient 7.0.2, ManagedSni/*.netcore.cs)

  1. SniTcpHandle.Dispose() sets _stream = null under lock(this).
  2. The MARS demuxer (SniMarsConnection.HandleReceiveComplete) runs under DemuxerSync (a different lock) and, after a successful receive with packet.DataLeft == 0, re-arms the next read via ReceiveAsync(ref packet) -> SniTcpHandle.ReceiveAsync -> packet.ReadFromStreamAsync(_stream).
  3. If the handle was disposed in between, _stream is null and Stream.ReadAsync is invoked on a null reference. The catch in SniTcpHandle.ReceiveAsync only filters ObjectDisposedException | SocketException | IOException, so the NullReferenceException escapes.
  4. The read was issued with a fire-and-forget ContinueWith in SniPacket.ReadFromStreamAsync; the faulted continuation task is never observed, so it surfaces as TaskScheduler.UnobservedTaskException on the finalizer thread.

Trigger used here

One SqlConnection with MultipleActiveResultSets=true. Session 1 streams a large result set; session 2 executes RAISERROR(..., 20, 1) WITH LOG, which makes the server terminate the connection, so the client dooms the connection and disposes the physical SniTcpHandle while session 1's packets are still being received and demuxed. Any teardown of the physical connection while data is arriving (broken connection, server-side KILL, disposal of a doomed pooled connection) has the same shape.

Run

export SQLCLIENT_REPRO_CONNECTION="Server=localhost,1433;User Id=sa;Password=...;Encrypt=False;TrustServerCertificate=True"
dotnet run -c Release -- 300               # MARS on, pooling off  -> exit code 1 when reproduced
dotnet run -c Release -- 300 mars pooled
dotnet run -c Release -- 300 no-mars       # control: never reproduces

Use a throwaway SQL Server (e.g. mcr.microsoft.com/mssql/server): the severity-20 error is written to the SQL Server log and requires a login permitted to raise it.

Observed

Platform MARS Pooling Result
.NET 10.0.11, Windows 11 (managed SNI switch on) on off 6, 16 and 8 hits per 200 iterations (three runs)
.NET 10.0.11, Windows 11 (managed SNI switch on) on on 11 hits per 200 iterations
.NET 10.0.11, Windows 11 (managed SNI switch on) off off 0 hits per 200 iterations
.NET 10.0.12, Ubuntu 24.04 (mcr.microsoft.com/dotnet/sdk:10.0) on off first hit within 125 iterations
.NET 10.0.12, Ubuntu 24.04 (mcr.microsoft.com/dotnet/sdk:10.0) on on first hit within 225 iterations

This exact program: Windows/managed SNI, MARS on, pooled — 2 hits in the first 25 iterations; Ubuntu 24.04 container, MARS on, pooled — first hit within 50 iterations (both exit code 1).

SQL Server 2025 (RTM-CU5) 17.0.4045 in mcr.microsoft.com/mssql/server:latest.

About

Minimal repro: unobserved NullReferenceException in Microsoft.Data.SqlClient managed SNI (MARS) after physical connection teardown

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages