Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ports Fix deadlock in transaction (#1242) to .NET #2161

Merged
merged 6 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -348,31 +348,34 @@ public void SinglePhaseCommit(SinglePhaseEnlistment enlistment)
#endif
try
{
Exception commitException = null;

lock (connection)
// If the connection is doomed, we can be certain that the
// transaction will eventually be rolled back or has already been aborted externally, and we shouldn't
// attempt to commit it.
if (connection.IsConnectionDoomed)
{
// If the connection is doomed, we can be certain that the
// transaction will eventually be rolled back or has already been aborted externally, and we shouldn't
// attempt to commit it.
if (connection.IsConnectionDoomed)
lock (connection)
{
_active = false; // set to inactive first, doesn't matter how the rest completes, this transaction is done.
_connection = null;

enlistment.Aborted(SQL.ConnectionDoomed());
}
else

enlistment.Aborted(SQL.ConnectionDoomed());
}
else
{
Exception commitException;
lock (connection)
{
try
{
// Now that we've acquired the lock, make sure we still have valid state for this operation.
ValidateActiveOnConnection(connection);

_active = false; // set to inactive first, doesn't matter how the rest completes, this transaction is done.
_connection = null; // Set prior to ExecuteTransaction call in case this initiates a TransactionEnd event
_connection = null; // Set prior to ExecuteTransaction call in case this initiates a TransactionEnd event

connection.ExecuteTransaction(SqlInternalConnection.TransactionRequest.Commit, null, System.Data.IsolationLevel.Unspecified, _internalTransaction, true);
commitException = null;
}
catch (SqlException e)
{
Expand All @@ -391,42 +394,41 @@ public void SinglePhaseCommit(SinglePhaseEnlistment enlistment)
ADP.TraceExceptionWithoutRethrow(e);
connection.DoomThisConnection();
}
if (commitException != null)
}
if (commitException != null)
{
// connection.ExecuteTransaction failed with exception
if (_internalTransaction.IsCommitted)
{
// connection.ExecuteTransaction failed with exception
if (_internalTransaction.IsCommitted)
{
// Even though we got an exception, the transaction
// was committed by the server.
enlistment.Committed();
}
else if (_internalTransaction.IsAborted)
{
// The transaction was aborted, report that to
// SysTx.
enlistment.Aborted(commitException);
}
else
{
// The transaction is still active, we cannot
// know the state of the transaction.
enlistment.InDoubt(commitException);
}

// We eat the exception. This is called on the SysTx
// thread, not the applications thread. If we don't
// eat the exception an UnhandledException will occur,
// causing the process to FailFast.
// Even though we got an exception, the transaction
// was committed by the server.
enlistment.Committed();
}
else if (_internalTransaction.IsAborted)
{
// The transaction was aborted, report that to
// SysTx.
enlistment.Aborted(commitException);
}
else
{
// The transaction is still active, we cannot
// know the state of the transaction.
enlistment.InDoubt(commitException);
}

connection.CleanupConnectionOnTransactionCompletion(_atomicTransaction);
// We eat the exception. This is called on the SysTx
// thread, not the applications thread. If we don't
// eat the exception an UnhandledException will occur,
// causing the process to FailFast.
}
}

if (commitException == null)
{
// connection.ExecuteTransaction succeeded
enlistment.Committed();
connection.CleanupConnectionOnTransactionCompletion(_atomicTransaction);
if (commitException == null)
{
// connection.ExecuteTransaction succeeded
enlistment.Committed();
}
}
}
catch (System.OutOfMemoryException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ public void SinglePhaseCommit(SysTx.SinglePhaseEnlistment enlistment)
Debug.Assert(null != enlistment, "null enlistment?");
SqlInternalConnection connection = GetValidConnection();


if (null != connection)
{
SqlConnection usersConnection = connection.Connection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,17 @@ public static bool IsTargetReadyForAeWithKeyStore()
;
}

public static bool IsSupportingDistributedTransactions()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to use fully qualified names here to avoid having to do if/def in the using section

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to use the same Is pattern although I felt is is a bit of a clumsy name. I would have preferred SupportsDistributedTransactions but I wanted to make sure it fits the style of other such methods.

{
#if NET7_0_OR_GREATER
return OperatingSystem.IsWindows() && System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture != System.Runtime.InteropServices.Architecture.X86 && IsNotAzureServer();
#elif NETFRAMEWORK
return System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows) && IsNotAzureServer();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: .NET Framework is not built/tested on non-windows, so platform check can be avoided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it safer to leave it as is? Happy to change it if you wish so

danielmarbach marked this conversation as resolved.
Show resolved Hide resolved
#else
return false;
#endif
}

public static bool IsUsingManagedSNI() => UseManagedSNIOnWindows;

public static bool IsNotUsingManagedSNIOnWindows() => !UseManagedSNIOnWindows;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ public static void TestManualEnlistment_Enlist_TxScopeComplete()
RunTestSet(TestCase_ManualEnlistment_Enlist_TxScopeComplete);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.Netcoreapp)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsSupportingDistributedTransactions))]
public static void TestEnlistmentPrepare_TxScopeComplete()
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
{
try
Assert.Throws<TransactionAbortedException>( () =>
{
using TransactionScope txScope = new(TransactionScopeOption.RequiresNew, new TransactionOptions()
{
Expand All @@ -63,12 +62,7 @@ public static void TestEnlistmentPrepare_TxScopeComplete()
connection.Open();
System.Transactions.Transaction.Current.EnlistDurable(EnlistmentForPrepare.s_id, new EnlistmentForPrepare(), EnlistmentOptions.None);
txScope.Complete();
Assert.False(true, "Expected exception not thrown.");
}
catch (Exception e)
{
Assert.True(e is TransactionAbortedException);
}
});
}

private static void TestCase_AutoEnlistment_TxScopeComplete()
Expand Down