Skip to content

System.Data.SqlClient 4.8.2 throws TransactionAbortedException (the connection has been broken) #729

Closed

Description

@MichaelLogutov

Description

After updating System.Data.SqlClient version from old 4.6.1 to latest 4.8.2 we started to see period errors like this:

System.Transactions.TransactionAbortedException: The transaction has aborted. ---> System.InvalidOperationException: The requested operation cannot be completed because the connection has been broken.

   --- End of inner exception stack trace ---
   at System.Transactions.TransactionStateAborted.EndCommit(InternalTransaction tx)
   at System.Transactions.CommittableTransaction.Commit()
   at System.Transactions.TransactionScope.InternalDispose()
   at System.Transactions.TransactionScope.Dispose()
   ...

Those exceptions happening in pure ADO.NET code with TransactionScope created with ReadCommitted isolation level, TransactionScopeOption.Required, 1 minute timeout and TransactionScopeAsyncFlowOption.Enabled.

After rolling back to System.Data.SqlClient 4.8.1 those exceptions dissapeared but we saw another new type of exceptions we've never seen before from cassandra driver:

Exception while trying borrow a connection from a pool

System.Net.Sockets.SocketException: Transport endpoint is not connected
   at Cassandra.Connections.HostConnectionPool.GetExistingConnections()
   at Cassandra.Connections.HostConnectionPool.BorrowExistingConnection()
   at async Cassandra.Connections.HostConnectionPool.GetConnectionFromHostAsync(?)

Downgrading back to 4.6.1 seems to fixed all issues.

Configuration

It happened on aspnet core 3.1 web applications and console netcore 3.1 applications with latest 3.1 images both running in container on Linux.

Regression?

It works on System.Data.SqlClient 4.6.1

Other information

This issue happening on heavy load application and only on production. I can't reproduce this issue locally.
I wish I could find source for System.Data.SqlClient library to see the diff for 4.8.2 version.

Activity

  1. Wraith2 commented on Sep 12, 2020

    @Wraith2
    Contributor

    The System version the library has been frozen and moved out of this repository in favour of Microsoft.Data.SqlClient https://github.com/dotnet/SqlClient which can be versioned separately. It contains fixes and improvements beyond those present in the framework provided versions. If possible give it a try and see if the behaviour you're seeing has already been resolved.

  2. MichaelLogutov commented on Sep 13, 2020

    @MichaelLogutov
    Author

    Does it requires rewriting all namespaces Microsoft.*? Because we can't afford such rework in our libraries and all microservices.

  3. Wraith2 commented on Sep 13, 2020

    @Wraith2
    Contributor

    It does. You should probably consider introducing abstractions in your codebase to avoid such things causing agility limitations.

  4. MichaelLogutov commented on Sep 13, 2020

    @MichaelLogutov
    Author

    Well, it's not that simple when you have large code base and 20 product team developers.

    Can I see the source code or at least changelog of what been changed in 4.8.2?

  5. MichaelLogutov commented on Sep 14, 2020

    @MichaelLogutov
    Author

    Thanks. I can see the source now but I can't seems to see the changes in 4.8.2
    https://github.com/microsoft/dotnet/tree/master/releases do not seems to have release notes for individual nuget packages like System.Data.SqlClient. Or I'm missing something?

  6. Wraith2 commented on Sep 14, 2020

    @Wraith2
    Contributor

    It's part of the framework so it doesn't get it's own update schedule. You probably want to review all the pull requests which targeted files in that project.

  7. MichaelLogutov commented on Sep 14, 2020

    @MichaelLogutov
    Author

    I couldn't find the source code in github for System.Data.SqlClient. I saw only System.Data.Common

  8. devsko commented on Sep 14, 2020

    @devsko

    AFAIK System.Data.SqlClient is not open sourced but source open at https://referencesource.microsoft.com/#System.Data as mentioned above.

  9. Wraith2 commented on Sep 14, 2020

    @Wraith2
    Contributor

    AFAIK System.Data.SqlClient is not open sourced but source open at

    It absolutely was and was part of this repository. It was removed in favour of the Microsoft version as I said earlier. To see the code you'll have to look into the history prior to it's removal.

  10. devsko commented on Sep 14, 2020

    @devsko

    @Wraith2 Just curious. It was the same code for core / desktop? Where lives the System.Data.SqlClient desktop code now?

  11. Wraith2 commented on Sep 14, 2020

    @Wraith2
    Contributor

    The desktop version was distinct from the core version. This repo only contained the core version.
    The code is still here but not current which I why I say you'll need to look through the repo history to look at it. You can look back at any file that has ever existed by reviewing the git history but because it isn't currently being build and shipped it's not visible in the current master.

    The code was forked into Microsoft.Data.SqlClient which also contains the desktop version. The Microsoft version supercedes the System version and is versioned independently so it can be updated without a framework update. It contains performance improvements new features and bug fixes that may not be ported back to core or desktop verisons.

  12. devsko commented on Sep 14, 2020

    @devsko

    Thanks.
    I totally misunderstood this issue. After reading 4.6.1 and 4.8.2 I thought it is all about .NET Framework - but it isn't. Sorry for the confusion.

  13. cheenamalhotra commented on Sep 14, 2020

    @cheenamalhotra
    Member

    Tagging @saurabh500 @corivera for System.Data.SqlClient (.Net Framework)

  14. 14 remaining items

  15. MichaelLogutov commented on Oct 9, 2020

    @MichaelLogutov
    Author

    I would gladly, but I'm afraid I can't - the code is simple ADO.NET update but something could be with the all other stuff microservice doing (heavy CPU usage due to image resizing on Skia + cassandra and rabbitmq libraries connected to servers).

  16. cheenamalhotra commented on Oct 9, 2020

    @cheenamalhotra
    Member

    Ok, I'll try to form a repro based on provided info.

    Could you also confirm if your connection is a pooled connection or pooling is disabled?
    Does changing that make any difference?

    And also do you also know why the transaction aborts at the first place? Are there failing commands or it's something else? Have you been able to gather it from logs and is it possible to prevent the transaction abort?

  17. MichaelLogutov commented on Oct 12, 2020

    @MichaelLogutov
    Author

    Could you also confirm if your connection is a pooled connection or pooling is disabled?

    It's default connection string so it's pooled by default (if I'm not mistaken).
    The only thing that changed - max pool size. So connection string looks like this:
    server=my.server.name; database=MyDatabase; User ID=MyUser; Password=mypassword; Max Pool Size=500

    Does changing that make any difference?

    Before I tested it, just to clarify - should I just add "Pooling=false" or also change min/max pool size in connection string?

    And also do you also know why the transaction aborts at the first place? Are there failing commands or it's something else? Have you been able to gather it from logs and is it possible to prevent the transaction abort?

    I think it doesn't aborts at all. Because otherwise there should be warning about transaction in not active state before commit.

  18. cheenamalhotra commented on Oct 13, 2020

    @cheenamalhotra
    Member

    should I just add "Pooling=false" or also change min/max pool size in connection string

    Just changing "Pooling = false" should disable it.

  19. cheenamalhotra commented on Oct 13, 2020

    @cheenamalhotra
    Member

    I think it doesn't aborts at all. Because otherwise there should be warning about transaction in not active state before commit.

    There's certainly an abort triggered somewhere, otherwise you'd never run into this issue, since the change in last release was made specifically on "Transaction Abort" callback API.

    You should be able to capture SQL Profiler traces for "Transaction" > "DTC Transaction" events to see which "Application" triggers "Transaction Aborted" events. You should notice something like below in SQL profiler traces:

    EventClass TextData TransactionId SPID EventSubClass DatabaseName
    DTCTransaction {D3296B44-F55C-4EA2-9B26-3A6011464D6C} 4502378 18 16 - Transaction is aborting master
  20. wolgasster commented on Jan 14, 2021

    @wolgasster

    We started getting the same error in .Net Framework 4.8 application under high load (lots of parallel user sessions execute database queries) after upgrade of Microsoft.Data.SqlClient from 1.1.2 to 1.1.3.
    Input conditions look the same as in the issue's description. All commands are executed wrapped by a TransactionScope and using connections from ConnectionPool, smth like:

    using (var transactionScope = DataAccessFactory.Current.CreateTransactionScope())
    {
    	using (var connection = DataAccessFactory.Current.CreateConnection())
    	{
    		// create and execute command ...
    	}
    	transactionScope.Complete();
    }

    Update:
    Looks like disabling of pooling fixes the issue, but it is not a good solution from performance perspective.

    Meanwhile, adding the next code in the SqlDelegatedTransaction.TransactionEnded method looks strange considering if a transaction is successfully committed: (commit 0b20c3b)

    // Safest approach is to doom this connection, whose transaction has been aborted externally.
    // If we want to avoid dooming the connection for performance, state needs to be properly restored. (future TODO)
    connection.DoomThisConnection();

    +@cheenamalhotra @David-Engel

  21. dotWils commented on Feb 1, 2021

    @dotWils

    Having the same problem here with .NET Framework 4.8 and Microsoft.Data.SqlClient 2.1.1

    transaction = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions()
    {
    IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted,
    }, TransactionScopeAsyncFlowOption.Enabled);

    MSDTC is used, connection pooling is used, SqlConnection is open and closed for every command

    at System.Transactions.TransactionStateAborted.EndCommit(InternalTransaction tx)
    at System.Transactions.CommittableTransaction.Commit()
    at System.Transactions.TransactionScope.InternalDispose()
    at System.Transactions.TransactionScope.Dispose()
    at Robin.Bll.RunManager.CompleteTransaction()

  22. cheenamalhotra commented on Feb 1, 2021

    @cheenamalhotra
    Member

    Hi @wolgasster @dotWils

    Is it possible to create a repro application to mimic your scenarios where exception occurs? That would help us in understanding more about your usecase.

    The closing of Connection happens only on Transaction Abort event, and that's important to do because the driver does not lose reference to old transaction and continues to reattach it for next connection. If there's a server side delay to abort old transaction, server accepts that request. This in turn causes a security issue since whenever the transaction does end on the server side, the remaining queries on the new connection execute with implicit transactions.

    We may have to look at why an aborted transaction is being committed by your application.

  23. dotWils commented on Feb 2, 2021

    @dotWils

    Hi @wolgasster @dotWils

    Is it possible to create a repro application to mimic your scenarios where exception occurs? That would help us in understanding more about your usecase.

    The closing of Connection happens only on Transaction Abort event, and that's important to do because the driver does not lose reference to old transaction and continues to reattach it for next connection. If there's a server side delay to abort old transaction, server accepts that request. This in turn causes a security issue since whenever the transaction does end on the server side, the remaining queries on the new connection execute with implicit transactions.

    We may have to look at why an aborted transaction is being committed by your application.

    Thx for being willing to look at it.
    It will be difficult to send a repro as it's a complete engine running procedures on a database.
    Our .NET 4.8 application runs for days on .NET 4.8 System.Data.SqlClient. (No transactions aborted)
    When only upgrading client to Microsoft.Data.SqlClient (of course in the whole application) we get this rather fast.
    Will talk with client if it's worth the effort...

  24. csnemes commented on Apr 7, 2021

    @csnemes

    We have the same problem with Microsoft.Data.SqlClient under Net 5.0. With heavy parallel load the connection pooling/transaction handling seems to break down.
    With the attached solution the problem can be reproduced. It runs heavy load in cycles. Sometimes you have to wait till cycle 50 or so for the problem to occur, but it will appear consistently.
    SQLProblem.zip
    I tried to dig deeper, but unfortunately the System.Transactions.TransactionEventSource is not working at the moment.

  25. cheenamalhotra commented on Apr 20, 2021

    @cheenamalhotra
    Member

    @csnemes @dotWils @wolgasster @MichaelLogutov

    We have a potential fix for this issue in PR #1042.
    I've tested the PR with the repro "SQLproblem" as well from #729 (comment) and this issue seems to be solved. Would appreciate your feedback if you could test and confirm the fix from this package: Build Artifacts

    Thank you

  26. csnemes commented on Apr 26, 2021

    @csnemes

    Hi, I've managed to try the patch both with the reproduction and within our application (where we originally encountered with this problem), and it works perfectly. Thanks a lot for the fix.

  27. cheenamalhotra commented on Apr 28, 2021

    @cheenamalhotra
    Member

    Closing this issue as fixed has been merged for main(3.0), 2.1 and 1.1 servicing.
    Next releases will contain fix for this issue.

  28. dotWils commented on May 7, 2021

    @dotWils

    Closing this issue as fixed has been merged for main(3.0), 2.1 and 1.1 servicing.
    Next releases will contain fix for this issue.

    Thx for finding a solution.
    Not so clear how this will be released.
    The title reads System.Data.SqlClient 4.8.2 will this be version 4.8.3 or will this not be serviced?
    Because the merging/servicing you mention here is for Microsoft.Data.SqlClient?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions