Thread-safe Cached Enumerator - lock with yield

前端 未结 2 1114
轮回少年
轮回少年 2021-01-15 03:55

I have a custom \"CachedEnumerable\" class (inspired by Caching IEnumerable) that I need to make thread safe for my asp.net core web app.

Is the following implementa

2条回答
  •  爱一瞬间的悲伤
    2021-01-15 04:14

    Your class is not thread safe, because shared state is mutated in unprotected regions inside your class. The unprotected regions are:

    1. The constructor
    2. The Dispose method

    The shared state is:

    1. The _enumerator private field
    2. The _cache private field
    3. The CachingComplete public property

    Some other issues regarding your class:

    1. Implementing IDisposable creates the responsibility to the caller to dispose your class. There is no need for IEnumerables to be disposable. In the contrary IEnumerators are disposable, but there is language support for their automatic disposal (feature of foreach statement).
    2. Your class offers extended functionality not expected from an IEnumerable (ElementAt, Count etc). Maybe you intended to implement a CachedList instead? Without implementing the IList interface, LINQ methods like Count() and ToArray() cannot take advantage of your extended functionality, and will use the slow path like they do with plain vanilla IEnumerables.

    Update: I just noticed another thread-safety issue. This one is related to the public IEnumerator GetEnumerator() method. The enumerator is compiler-generated, since the method is an iterator (utilizes yield return). Compiler-generated enumerators are not thread safe. Consider this code for example:

    var enumerable = Enumerable.Range(0, 1_000_000);
    var cachedEnumerable = new CachedEnumerable(enumerable);
    var enumerator = cachedEnumerable.GetEnumerator();
    var tasks = Enumerable.Range(1, 4).Select(id => Task.Run(() =>
    {
        int count = 0;
        while (enumerator.MoveNext())
        {
            count++;
        }
        Console.WriteLine($"Task #{id} count: {count}");
    })).ToArray();
    Task.WaitAll(tasks);
    

    Four threads are using concurrently the same IEnumerator. The enumerable has 1,000,000 items. You may expect that each thread would enumerate ~250,000 items, but that's not what happens.

    Output:

    Task #1 count: 0
    Task #4 count: 0
    Task #3 count: 0
    Task #2 count: 1000000

    The MoveNext in the line while (enumerator.MoveNext()) is not your safe MoveNext. It is the compiler-generated unsafe MoveNext. Although unsafe, it includes a mechanism intended probably for dealing with exceptions, that marks temporarily the enumerator as finished before calling the externally provided code. So when multiple threads are calling the MoveNext concurrently, all but the first will get a return value of false, and will terminate instantly the enumeration, having completed zero loops. To solve this you must probably code your own IEnumerator class.


    Update: Actually my last point about thread-safe enumeration is a bit unfair, because enumerating with the IEnumerator interface is an inherently unsafe operation, which is impossible to fix without the cooperation of the calling code. This is because obtaining the next element is not an atomic operation, since it involves two steps (call MoveNext() + read Current). So your thread-safety concerns are limited to the protection of the internal state of your class (fields _enumerator, _cache and CachingComplete). These are left unprotected only in the constructor and in the Dispose method, but I suppose that the normal use of your class may not follow code paths that create the race conditions that would result to internal state corruption.

    Personally I would prefer to take care of these code paths too, and I wouldn't let it to the whims of chance.


    Update: I wrote a cache for IAsyncEnumerables, to demonstrate an alternative technique. The enumeration of the source IAsyncEnumerable is not driven by the callers, using locks or semaphores to obtain exclusive access, but by a separate worker-task. The first caller starts the worker-task. Each caller at first yields all items that are already cached, and then awaits for more items, or for a notification that there are no more items. As notification mechanism I used a TaskCompletionSource. A lock is still used to ensure that all access to shared resources is synchronized.

    public class CachedAsyncEnumerable : IAsyncEnumerable
    {
        private readonly object _locker = new object();
        private IAsyncEnumerable _source;
        private Task _sourceEnumerationTask;
        private List _buffer;
        private TaskCompletionSource _moveNextTCS;
        private Exception _sourceEnumerationException;
        private int _sourceEnumerationVersion; // Incremented on exception
    
        public CachedAsyncEnumerable(IAsyncEnumerable source)
        {
            _source = source ?? throw new ArgumentNullException(nameof(source));
        }
    
        public async IAsyncEnumerator GetAsyncEnumerator(
            CancellationToken cancellationToken = default)
        {
            lock (_locker)
            {
                if (_sourceEnumerationTask == null)
                {
                    _buffer = new List();
                    _moveNextTCS = new TaskCompletionSource();
                    _sourceEnumerationTask = Task.Run(
                        () => EnumerateSourceAsync(cancellationToken));
                }
            }
            int index = 0;
            int localVersion = -1;
            while (true)
            {
                T current = default;
                Task moveNextTask = null;
                lock (_locker)
                {
                    if (localVersion == -1)
                    {
                        localVersion = _sourceEnumerationVersion;
                    }
                    else if (_sourceEnumerationVersion != localVersion)
                    {
                        ExceptionDispatchInfo
                            .Capture(_sourceEnumerationException).Throw();
                    }
                    if (index < _buffer.Count)
                    {
                        current = _buffer[index];
                        index++;
                    }
                    else
                    {
                        moveNextTask = _moveNextTCS.Task;
                    }
                }
                if (moveNextTask == null)
                {
                    yield return current;
                    continue;
                }
                var moved = await moveNextTask;
                if (!moved) yield break;
                lock (_locker)
                {
                    current = _buffer[index];
                    index++;
                }
                yield return current;
            }
        }
    
        private async Task EnumerateSourceAsync(CancellationToken cancellationToken)
        {
            TaskCompletionSource localMoveNextTCS;
            try
            {
                await foreach (var item in _source.WithCancellation(cancellationToken))
                {
                    lock (_locker)
                    {
                        _buffer.Add(item);
                        localMoveNextTCS = _moveNextTCS;
                        _moveNextTCS = new TaskCompletionSource();
                    }
                    localMoveNextTCS.SetResult(true);
                }
                lock (_locker)
                {
                    localMoveNextTCS = _moveNextTCS;
                    _buffer.TrimExcess();
                    _source = null;
                }
                localMoveNextTCS.SetResult(false);
            }
            catch (Exception ex)
            {
                lock (_locker)
                {
                    localMoveNextTCS = _moveNextTCS;
                    _sourceEnumerationException = ex;
                    _sourceEnumerationVersion++;
                    _sourceEnumerationTask = null;
                }
                localMoveNextTCS.SetException(ex);
            }
        }
    }
    

    This implementation follows a specific strategy for dealing with exceptions. If an exception occurs while enumerating the source IAsyncEnumerable, the exception will be propagated to all current callers, the currently used IAsyncEnumerator will be discarded, and the incomplete cached data will be discarded too. A new worker-task may start again later, when the next enumeration request is received.

提交回复
热议问题