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.)
SniTcpHandle.Dispose()sets_stream = nullunderlock(this).- The MARS demuxer (
SniMarsConnection.HandleReceiveComplete) runs underDemuxerSync(a different lock) and, after a successful receive withpacket.DataLeft == 0, re-arms the next read viaReceiveAsync(ref packet)->SniTcpHandle.ReceiveAsync->packet.ReadFromStreamAsync(_stream). - If the handle was disposed in between,
_streamis null andStream.ReadAsyncis invoked on a null reference. ThecatchinSniTcpHandle.ReceiveAsynconly filtersObjectDisposedException | SocketException | IOException, so theNullReferenceExceptionescapes. - The read was issued with a fire-and-forget
ContinueWithinSniPacket.ReadFromStreamAsync; the faulted continuation task is never observed, so it surfaces asTaskScheduler.UnobservedTaskExceptionon the finalizer thread.
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.
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.
| 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.