Skip to content

Commit

Permalink
Fix 10 minutes time out of TransactionScope
Browse files Browse the repository at this point in the history
  • Loading branch information
William Li committed Sep 17, 2021
1 parent e89f3d7 commit 0493702
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions src/Cli/dotnet/TransactionalAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Reflection;
using System.Transactions;
using Microsoft.DotNet.Cli.Utils;

namespace Microsoft.DotNet.Cli
{
Expand Down Expand Up @@ -65,19 +67,44 @@ public static T Run<T>(
// This automatically inherits any ambient transaction
// If a transaction is inherited, completing this scope will be a no-op
T result = default(T);
using (var scope = new TransactionScope(
TransactionScopeOption.Required,
TimeSpan.Zero))
try
{
Transaction.Current.EnlistVolatile(
new EnlistmentNotification(commit, rollback),
EnlistmentOptions.None);
using (var scope = CreateTransactionScope(
TimeSpan.FromDays(10)))
{
Transaction.Current.EnlistVolatile(
new EnlistmentNotification(commit, rollback),
EnlistmentOptions.None);

result = action();

result = action();
scope.Complete();
}

scope.Complete();
return result;
}
return result;
catch (TransactionAbortedException ex)
{
Reporter.Verbose.WriteLine(string.Format("TransactionAbortedException Message: {0}", ex.Message));
Reporter.Verbose.WriteLine(
$"Inner Exception Message: {ex?.InnerException?.Message + "---" + ex?.InnerException}");
throw;
}
}

private static void SetTransactionManagerField(string fieldName, object value)
{
typeof(TransactionManager).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, value);
}

// HACK: https://github.com/dotnet/sdk/issues/21101
// we should replace TransactionScope later.
private static TransactionScope CreateTransactionScope(TimeSpan timeout)
{
SetTransactionManagerField("s_cachedMaxTimeout", true);
SetTransactionManagerField("s_maximumTimeout", timeout);
return new TransactionScope(TransactionScopeOption.Required, timeout);
}

public static void Run(
Expand Down

0 comments on commit 0493702

Please sign in to comment.