Skip to content

Cancellation: "Multiple simultaneous connections ... not supported when UseXaTransactions=False" #1001

Closed

Description

@Timovzl

With UseXaTransactions=False and TransactionScope, triggering the cancellation token during ExecuteReaderAsync leads to the following exception:

System.NotSupportedException: Multiple simultaneous connections or connections with different connection strings
inside the same transaction are not supported when UseXaTransactions=False.
at MySqlConnector.MySqlConnection.FindExistingEnlistedSession(Transaction transaction) in /_/src/MySqlConnector/MySqlConnection.cs:line 289
at MySqlConnector.MySqlConnection.OpenAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlConnection.cs:line 435
at MySqlConnector.MySqlConnection.Open() in /_/src/MySqlConnector/MySqlConnection.cs:line 380
at MySqlConnector.MySqlConnection.Cancel(ICancellableCommand command, Int32 commandId, Boolean isCancel) in /_/src/MySqlConnector/MySqlConnection.cs:line 719
at MySqlConnector.MySqlCommand.Cancel() in /_/src/MySqlConnector/MySqlCommand.cs:line 98
at System.Threading.CancellationToken.<>c.<.cctor>b__26_0(Object obj)
at System.Threading.CancellationTokenSource.InternalRegister(Action`1 callback, Object stateForCallback, SynchronizationContext syncContext, ExecutionContext executionContext)
at System.Threading.CancellationToken.Register(Action`1 callback, Object state, Boolean useSynchronizationContext, Boolean useExecutionContext)
at MySqlConnector.MySqlCommand.MySqlConnector.Core.ICancellableCommand.RegisterCancel(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 364
at MySqlConnector.MySqlDataReader.ReadAsync(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 43

The cancellation token was triggered due to application shutdown (SIGTERM).

The flow was as follows:

  • Construct TransactionsScope in using, with async flows enabled.
  • ExecuteReaderAsync, with CancellationToken.
  • CancellationToken requests cancellation.

I have a theory. Is it possible that the logic that handles cancellation fails to return the connection to the pool? Perhaps a rollback is then attempted, but the connection that was bound to the transaction is not available, so a new connection is attempted to be used. That would explain the exception.

I also wonder if this could be related to #745.

Activity

  1. bgrainger commented on Jun 14, 2021

    @bgrainger
    Member

    It looks like the background connection being opened to cancel the command is automatically enlisting in the current TransactionScope.

    This is not desirable, and should be trivial to bypass with AutoEnlist=false.

  2. added a commit that references this issue on Jun 14, 2021
    a258166
  3. Timovzl commented on Jun 15, 2021

    @Timovzl
    Author

    Nice! As always, your fast response is greatly appreciated.

  4. bgrainger commented on Jun 16, 2021

    @bgrainger
    Member

    Fixed in 1.3.10.

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