Skip to content

AddRemoveFromDifferentThreads.ConcurrentStack hangs with NativeAOT #66987

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

Closed
adamsitnik opened this issue Mar 22, 2022 · 3 comments
Closed

AddRemoveFromDifferentThreads.ConcurrentStack hangs with NativeAOT #66987

adamsitnik opened this issue Mar 22, 2022 · 3 comments

Comments

@adamsitnik
Copy link
Member

I am currently working on getting all dotnet/performance microbenchmarks work with NativeAOT. This particular benchmark just hangs. It's not the first time it has caused trouble for our runtime (#64980).

Environment: Windows 11 x64, AMD hardware. I have not tested it with other configs.

cc @jkotas @MichalStrehovsky

Repro:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="7.0.0-*" />
  </ItemGroup>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nativeAOTfeed" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet7/nuget/v3/index.json" />
  </packageSources>
</configuration>
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

namespace aotHang
{
    class Program
    {
        static void Main(string[] args)
        {
            AddRemoveFromDifferentThreads<int> sut = new ();
            Console.WriteLine(nameof(sut.SetupConcurrentStackIteration));
            sut.SetupConcurrentStackIteration();
            Console.WriteLine(nameof(sut.ConcurrentStack));
            sut.ConcurrentStack();
            Console.WriteLine(nameof(sut.IterationCleanup));
            sut.IterationCleanup();
        }
    }

    public class AddRemoveFromDifferentThreads<T>
    {
        const int NumThreads = 2;

        public int Size = 2_000_000;

        private Barrier _barrier;
        private Task _producer, _consumer;

        public void IterationCleanup() => _barrier.Dispose();

        public void SetupConcurrentStackIteration()
        {
            var stack = new ConcurrentStack<T>();

            _barrier = new Barrier(NumThreads + 1);

            _producer = Task.Factory.StartNew(() =>
            {
                _barrier.SignalAndWait();
                _barrier.SignalAndWait();

                for (int i = 0; i < Size; i++)
                {
                    stack.Push(default);
                }
            }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);

            _consumer = Task.Factory.StartNew(() =>
            {
                _barrier.SignalAndWait();
                _barrier.SignalAndWait();

                int count = 0;
                while (count < Size)
                {
                    if (stack.TryPop(out T _))
                    {
                        count++;
                    }
                }
            }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);

            _barrier.SignalAndWait();
        }

        public void ConcurrentStack() => SignalAndWaitForAllTasks();

        private void SignalAndWaitForAllTasks()
        {
            _barrier.SignalAndWait();

            Task.WaitAll(_producer, _consumer);
        }
    }
}
dotnet  publish -c Release -r win-x64
.\bin\Release\net6.0\win-x64\native\aotHang.exe
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Mar 22, 2022
@hez2010
Copy link
Contributor

hez2010 commented Mar 22, 2022

May be related: #67805

@danmoseley
Copy link
Member

danmoseley commented Apr 4, 2022

Note I got a hang without Native AOT. #67559

@jkotas
Copy link
Member

jkotas commented Apr 9, 2022

I have transferred #67805 to dotnet/runtime and resolving this as duplicate.

@jkotas jkotas closed this as completed Apr 9, 2022
@jeffhandley jeffhandley removed the untriaged New issue has not been triaged by the area owner label May 6, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jun 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants