Skip to content

Always Encrypted introduces sporadic errors #4199

Open

Description

@Suchiman

Describe the bug

We have recently introduced our first table using always encrypted on SQL Azure Hyperscale + Azure KeyVault as keystore. After deploying it to production we've started experiencing sporadic failures that we've traced back to connections enabled for always encrypted. After narrowing the scope of connections featuring Column Encryption Setting=Enabled to the currently rarely exercised code path where its needed, the problem disappeared.

The error would show itself somewhere between minutes to hours apart, resulting in about 1-4 connections failing within the same second in parallel on seperate SqlConnection's opened by independent http requests, with the following exception

Microsoft.Data.SqlClient.SqlException (0x80131904): A severe error occurred on the current command.  The results, if any, should be discarded.
   at void Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, SqlCommand command, bool callerHasConnectionLock, bool asyncClose)
   at TdsOperationStatus Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, out bool dataReady)
   at object Microsoft.Data.SqlClient.SqlCommand.InternalEndExecuteNonQuery(IAsyncResult asyncResult, bool isInternal, string endMethod)
   at int Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult asyncResult)
   at int Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryAsync(IAsyncResult asyncResult)
   at void System.Threading.Tasks.TaskFactory<TResult>.FromAsyncCoreLogic(IAsyncResult iar, Func<IAsyncResult, TResult> endFunction, Action<IAsyncResult> endAction, Task<TResult> promise, bool requiresSynchronization)
   at async Task<int> Dapper.SqlMapper.ExecuteImplAsync(IDbConnection cnn, CommandDefinition command, object param) in /_/Dapper/SqlMapper.Async.cs:line 663
ClientConnectionId:3c4124b6-2604-4e7b-ab2e-11aa3da9030c
ClientConnectionId before routing:4f7c97b0-0fbb-434d-adfd-54f87ef5841f
Routing Destination:<redacted>.<redacted>.germanywestcentral1-a.worker.database.windows.net,11050

immediately followed by exactly one exception that is different

Microsoft.Data.SqlClient.SqlException (0x80131904): A transport-level error has occurred when receiving results from the server. (provider: Session Provider, error: 19 - Physical connection is not usable)
   at int Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryAsync(IAsyncResult asyncResult)
   at void System.Threading.Tasks.TaskFactory<TResult>.FromAsyncCoreLogic(IAsyncResult iar, Func<IAsyncResult, TResult> endFunction, Action<IAsyncResult> endAction, Task<TResult> promise, bool requiresSynchronization)
   at async Task<int> Dapper.SqlMapper.ExecuteImplAsync(IDbConnection cnn, CommandDefinition command, object param) in /_/Dapper/SqlMapper.Async.cs:line 663
ClientConnectionId:3c4124b6-2604-4e7b-ab2e-11aa3da9030c
Error Number:-1,State:0,Class:20
ClientConnectionId before routing:4f7c97b0-0fbb-434d-adfd-54f87ef5841f
Routing Destination:<redacted>.<redacted>.germanywestcentral1-a.worker.database.windows.net,11050

One notable observation is that all failing connections in a burst of failures all share the exact same ClientConnectionId which is suspicious to me. We do have MARS enabled, however MARS should not allow independently constructed SqlConnection's to reuse the same physical connection.

Also note that none of these are actually accessing tables with encrypted columns, it's merely that adding Column Encryption Setting=Enabled to the connection string causes these errors as a side effect.

To reproduce

There's no trivial way to reproduce, presumably it only happens under load as there was only one occurence on our testsystems over two weeks vs. dozens of times a day on production.
The code experiencing the exception roughly looks like this:

// Data Source=tcp:<servername>.database.windows.net,1433;Initial Catalog=<dbname>;Persist Security Info=False;User ID=<user>;Password=<pw>;Multiple Active Result Sets=True;Connect Timeout=30;Encrypt=True;Trust Server Certificate=False;Application Name=<appName>;Column Encryption Setting=Enabled

var context = new MyContext();
...
context.EnableEncryptionAccess(true);

var con = context.Database.GetDbConnection();
await using var close = await con.EnsureOpenAsync();

// Create a temp table as a destination for our SqlBulkCopy
await con.ExecuteAsync("SELECT TOP 0 * INTO #tmp FROM UnencryptedTable");  <-- exception **might** occur here at random

// ready a Bulk Copy
var copy = new SqlBulkCopy(con as SqlConnection);
copy.DestinationTableName = "#tmp";

try
{
    await copy.WriteToServerAsync(reader, cancellationToken);

This is part of our batch data import API that is the single most stressed component on production. It's not actually exercised by the always encrypted table since the ETL pattern used here seemed to be incompatible by design with the always encrypted requirements, so we've built an alternative path but kept the entire batch import API Always Encrypted enabled, leading to the issues. We're unsure if the new code is affected by that issue as well but currently its not yet exercised on prod.

supporting code:

Details
public class MyContext : DbContext
{
    public MyContext(bool readOnly = false)
    {
        Database.SetConnectionString(GetConnectionString(readOnly));
    }

    public static string? ConnectionString { get; set; }

    private string GetConnectionString(bool readOnly, bool readEncrypted = false)
    {
        string conString = ConnectionString!;
        var builder = new Microsoft.Data.SqlClient.SqlConnectionStringBuilder(conString)
        {
            ColumnEncryptionSetting = readEncrypted
                ? Microsoft.Data.SqlClient.SqlConnectionColumnEncryptionSetting.Enabled
                : Microsoft.Data.SqlClient.SqlConnectionColumnEncryptionSetting.Disabled,
            ApplicationIntent = readOnly
                ? Microsoft.Data.SqlClient.ApplicationIntent.ReadOnly
                : Microsoft.Data.SqlClient.ApplicationIntent.ReadWrite
        };
        conString = builder.ToString();

        return conString;
    }

    public void EnableEncryptionAccess(bool enabled)
    {
        if (Database.GetDbConnection().State != System.Data.ConnectionState.Closed)
        {
            throw new InvalidOperationException("Cannot switch Always Encrypted setting because the connection is not closed");
        }

        Database.SetConnectionString(GetConnectionString(ReadOnly, readEncrypted: enabled));
    }
}

public static class SqlExtensions
{
    public static async Task<ClosingWrapper> EnsureOpenAsync(this DbConnection con)
    {
        if (con.State == System.Data.ConnectionState.Closed)
        {
            await con.OpenAsync();
            return new ClosingWrapper(con);
        }
        return new ClosingWrapper(null);
    }
}

public struct ClosingWrapper(DbConnection? con) : IDisposable, IAsyncDisposable
{
    private readonly DbConnection? Connection = con;

    public void Dispose() => Connection?.Close();

    public async ValueTask DisposeAsync()
    {
        if (Connection is not null)
            await Connection.CloseAsync();
    }
}

Expected behavior

Always Encrypted should not affect the connection reliability

Further technical details

Microsoft.Data.SqlClient version: 7.0
.NET target: .NET 10
SQL Server version: SQL Azure Hyperscale
Operating system: Azure Web Apps Windows Code (non container)

Additional Info

We do not have Switch.Microsoft.Data.SqlClient.UseCompatibilityAsyncBehaviour, Switch.Microsoft.Data.SqlClient.UseCompatibilityProcessSni enabled on prod; they were however enabled on the testsystem during that one time where the error occured on the testsystem, so it seems unrelated.

Activity

  1. Wraith2 commented on Apr 17, 2026

    @Wraith2
    Contributor

    We do have MARS enabled, however MARS should not allow independently constructed SqlConnection's to reuse the same physical connection.

    MARS works by muxing multiple logical connections over a single socket so if there's an error in the packet stream then all logical connections will be at risk, closing all logical connections in this state seems like a safe thing to do from the library perspective.

    My non-official, just-a-contributor-here, recommendation is always to avoid using MARS. With that said, it doesn't usually cause protocol errors just performance issues so my immediate thought is that MARS is unlikely to be the cause but that it will make diagnosis more difficult.

  2. cheenamalhotra commented on May 8, 2026

    @cheenamalhotra
    Member

    @Suchiman

    Have you tried disabling MARS - did that help?

  3. moved this from To triage to Waiting for customer in SqlClient Boardon May 8, 2026
  4. Suchiman commented on May 8, 2026

    @Suchiman
    ContributorAuthor

    @Suchiman

    Have you tried disabling MARS - did that help?

    Its not possible to trivially test this since our code assumes MARS to be available.
    Disabling it will require auditing the entire codebase for patterns where we implicitly, through EFC, relied on MARS being available.

    It's on my to do list, as this isn't the first time MARS has been criticized as a troublemaker but i haven't gotten around to it yet

  5. self-assigned this
    on Jul 16, 2026
  6. moved this from Waiting for customer to Investigating in SqlClient Boardon Jul 16, 2026
  7. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  8. 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\AKV Provider
    Duplicates None found (searched for MARS/transport-level/Always Encrypted error patterns; no exact match)
    Regression Not indicated (author reports issue only appears when Column Encryption Setting=Enabled is set, even on tables without encrypted columns; no prior-working-version boundary mentioned)

    Analysis

    This report describes sporadic "A severe error occurred on the current command" followed immediately by "A transport-level error has occurred ... Physical connection is not usable" on connections that merely have Column Encryption Setting=Enabled (Always Encrypted), even though the queries involved do not touch encrypted columns. The reporter observed that multiple independently-opened SqlConnection instances failing in the same burst all share the same ClientConnectionId, which is suspicious given MARS is also enabled — this suggests a possible interaction between Always Encrypted connection setup/enclave negotiation and MARS session/physical-connection multiplexing, or a state-corruption bug triggered specifically by the AE code path (e.g., TdsParser/TdsParserStateObject handling during SqlCommand.EndExecuteNonQueryAsync). Given production impact (repeated connection failures under load, though currently mitigated by disabling AE on the affected path) and difficulty reproducing outside of load, this looks like a P2 severity issue — real but currently worked around, and not trivially reproducible.

    Next Steps

    • Recommend assigning this issue to Copilot coding agent to investigate the interaction between Always Encrypted (Column Encryption Setting=Enabled) and MARS in TdsParser.TryRun/ThrowExceptionAndWarning and the async execution path (SqlCommand.InternalEndExecuteNonQuery → EndExecuteNonQueryAsync), focusing on whether AE-specific code paths (e.g., enclave session setup, parameter encryption metadata lookups) can corrupt or cross-contaminate TdsParserStateObject/session state shared across MARS logical connections that share a ClientConnectionId.
    • Ask the reporter (if further diagnosis is needed) whether they can capture a network trace or enable Microsoft.Data.SqlClient.EventSource tracing around a failure burst, since the same ClientConnectionId failing across independently-opened connections strongly suggests connection pool or MARS session reuse under the hood.
    • As already discussed in the thread, disabling MARS as an experiment (even temporarily on a subset of traffic) would help isolate whether MARS is a necessary condition for the failure, though the reporter has noted this requires broader auditing of their EF Core-based codebase.
    • Consider cross-referencing with historical MARS + Always Encrypted issues in this repo (e.g., The MARS TDS header contained errors connecting to Azure SQL Server #2886 "MARS TDS header contained errors connecting to Azure SQL Server") for any related root cause, even though no exact duplicate was found.

    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 #4199 · copilot · auto · 50.9 AIC · ⌖ 13.2 AIC · ⊞ 11.9K · ◷

  9. moved this from Investigating to Backlog in SqlClient Boardon Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions