runtime: GC doesn't run when using TransactionScope + async

Description

I have an application that uses the TransactionScope class to control SQL transactions. We also use the same library to do mathematical simulations, but in this case it injects a dummy repository to stop the persistence to SQL. When doing this I noticed that performance became progressively worse, and eventually the app crashed with OOM errors.

I had previously created a bug report here: https://github.com/dotnet/runtime/issues/1419 but it was closed because it was determined that this is a GC issue and not a TransactionScope issue.

Take this code:

using System;
using System.Diagnostics;
using System.Threading;
using System.Transactions;

namespace TestTrxScope
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            double lastElapsed = 0;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            while(true)
            {
                using(var scope = CreateTransactionScope())
                {
                    scope.Complete();
                    i++;
                    if(i % 1_000_000 == 0)
                    {
                        Console.WriteLine($"1m took {sw.Elapsed.TotalSeconds - lastElapsed}");
                        lastElapsed = sw.Elapsed.TotalSeconds;
                        //GC.Collect(2);
                    }
                }
            }
        }

        public static TransactionScope CreateTransactionScope()
        {
            //Memory grows constantly
            return new TransactionScope(TransactionScopeOption.Suppress, new TransactionOptions() { }, TransactionScopeAsyncFlowOption.Enabled);

            //This version doesn't constantly grow in memory usage
            //return new TransactionScope();
        }
    }
}

This program’s memory usage will grow forever, and the time it takes to complete 1 million TransactionScopes will also grow forever.

image

image

image

Uncommenting the GC.Collect(2) fixes the ever increasing memory issue, but for some reason the GC does not run on its own in this program. You might think that this is a contrived example but I found this issue in production code, where we have console apps that do extremely heavy calculations.

Note that even if GC.Collect(2) is called manually, the performance per 1 million rounds still gets worse by 75% or so until it evens out. This suggests that our data access code is impacted by this even in situations when the GC might run level 2 collections on its own due to other allocations causing it.

Configuration

.net core 3.1, Windows 10 x64

Regression?

This does not seem to happen on .net framework.

Other information

The situation improves somewhat with .net 5 as I haven’t managed to get OOM errors but it still gets massively progressively worse.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 20 (13 by maintainers)

Commits related to this issue

Most upvoted comments

I believe that there are many pieces that fit together to cause this problem. Unfortunately, each part makes sense on its own. I’ll attempt to describe it all here.

  • TransactionScope is only relevant in that it uses ConditionalWeakTable (CWT). A similar repro that does
    object obj = new();
    cwt.Add(obj, value);
    cwt.Remove(obj);
    
    exhibits similar behavior.
  • CWT has a design point that it takes no locks on read access, so all updates are constrained in what they can do. One specific impact is that the Remove can’t fully deallocate the entry internally because a concurrent read might be happening.
  • CWT has an internal table (Container). Entries aren’t reused (see the previous point), so it is periodically replaced, though (as before) we can’t immediately deallocate the internal entries. Also, the Containers are linked in order to manage ownership of entries that end up shared across Containers.
  • The final cleanup work for a Container is left to a finalizer because at that point we know that those concurrent read operations must be done (since they would have references to the Container). However, the possibility of object resurrection (of an object with a reference to a CWT) means that new read operations could have been started. I’ll avoid the details for now, but the end result is that a second finalization pass is good enough.
  • Two stage finalization means that that Containers must get garbage collected twice and go through the finalization queue twice, so these objects end up in gen2. Because of the links, they end up being roots for future Containers. This means that eventually the Containers can’t even start the cleanup process until a gen2, so they pile up.
  • On top of this, in the microbenchmark scenario, there is a risk of creating finalization work faster than it can be completed. The order of finalization isn’t guaranteed, so it’s possible that a “bad” order could make things worse. I haven’t looked for this or seen evidence either way. However, once a Container is in the finalization queue, no more Containers from the same CWT will be added because they will be rooted by the original one. Therefore, there should a bit of self-correction is that we’ll eventually get to the oldest one, and so on.

I’ve experimented locally with several changes to CWT. Some of them improve the performance of the microbenchmark, but I believe that all of them are unsound. The problematic case that often breaks things is the following:

  • Object X has a CWT and both are collected.
  • X has a finalizer that starts using the CWT again. (Probably this happens by storing the reference to X and/or the CWT to some location and having another thread pick up the work so that the finalizer thread can continue to other objects.)
  • CWT may have an internal resize operation and has read operations happening.
  • A Container finalizer starts executing while the above is happening.

Note also that the CWT can’t tell the difference between an operation happening “normally” or under an object resurrection scenario. One thing that I tried was some cleanup during the resize operation so that only one finalization pass would be needed. This doesn’t work if the resize happens during an object resurrection.

Note: “during an object resurrection” is sloppy here - the point is that the Container strategy depends on learning that no references exist, which the GC determines during a collection. Some of these cleanup strategies depend on doing something before that point and something else after that point. When finalization leads to more operations, references “reappear” after that GC.

@aromaa thanks for looking into it… That may be true, I’m not sure - there may still be scenarios where the CWT usage is needed etc. This would require deeper investigation, and as I said, the bar on doing changes in System.Transactions is relatively high.

In any case, I’m still not convinced we should be necessarily working in the direction of dropping CWT from Sys.Tx; the issues analyzed by @markples above are general to CWT, so we should be thinking about them regardless of Sys.Tx (unless the decision is made that CWT is now discouraged etc.).

Thanks @roji. Given some of the details in #1419, it looks like the performance problem has been around for a very long time as well. It looks like the CWT implementation was changed between framework and .net core. The previous version locked on reads, which slows them, but then cleanup is much simpler (can be immediate). I don’t know the history of that change (though it’s certainly understandable to not want a lock for reads).

It’s possible that one or both of the above ideas will handle real scenarios. It would certainly be possible to build a microbenchmark that defeats them, so I worry a bit that a real scenario could still hit trouble. It would be interesting to try these are @karlra’s workload.

Cloning the entries into a new Container would allow use to break the link in general but with definite cost for other scenarios.

@markples thanks for doing the deep dive here, very interesting stuff…!

I assume that this is due to additional bookkeeping needed to make the async scenario work, but I don’t know any details here. @roji is this something that you can answer?

Unfortunately not, not without some investigation…

I will note that the code has been this way for a very long time, basically since TransactionScopeAsyncFlowOption.Enabled was introduced (I’m almost certain the same logic exists on .NET Framework). I’ll also note that in general, the bar for changing things in System.Transactions is quite high - this isn’t a part of .NET that’s being actively evolved (and in any case, unless we say that using CWT is discouraged and should be removed, I’m not sure there’s an actionable Sys.Tx-side thing to do…).

Why does this behavior trigger for TransactionScopes in async mode, but not for non-async ones? Does the non-async TransactionScope not use a CWT (I’m assuming that must be the case here)? If so, why is that the case? Could the implementation for the async flow be changed in a way that it stopped depending on CWT perhaps, thus avoiding this issue at its root?

The source code does seem to show that the behavior is async-vs-not.

            if (AsyncFlowEnabled)
            {
                Debug.Assert(ContextKey != null);
                // Async Flow is enabled and CallContext will be used for ambient transaction.
                _threadContextData = CallContextCurrentData.CreateOrGetCurrentData(ContextKey);
...
        public static ContextData CreateOrGetCurrentData(ContextKey contextKey)
        {
            s_currentTransaction.Value = contextKey;
            return s_contextDataTable.GetValue(contextKey, (env) => new ContextData(true));
        }
...
        private static readonly ConditionalWeakTable<ContextKey, ContextData> s_contextDataTable = new ConditionalWeakTable<ContextKey, ContextData>();

I assume that this is due to additional bookkeeping needed to make the async scenario work, but I don’t know any details here. @roji is this something that you can answer?

I have a partial explanation for this - will try to post later today.

Yes, that’s the whole point, omitting TransactionScopeAsyncFlowOption.Enabled fixes the issue (but then TransactionScope is unusable with async code).

I just tested with net7 and there is no appreciable difference vs net6. TieredPGO also makes no difference.

This is with TransactionScopeAsyncFlowOption.Enabled. The GC does run, but it doesn’t collect whatever is being leaked here:

image

This is without TransactionScopeAsyncFlowOption.Enabled. GC runs often, but RAM usage is stable.

image