Skip to content

SequenceEqual broken for concurrent use? #47016

@stephentoub

Description

@stephentoub

Repro:

using System;
using System.Security.Cryptography;
using System.Threading;

class Program
{
    static void Main()
    {
        static void Work(object _)
        {
            while (true)
            {
                Span<byte> buffer1 = RandomNumberGenerator.GetBytes(1024 * 1024);
                Span<byte> buffer2 = new byte[buffer1.Length];
                buffer1.CopyTo(buffer2);

                if (!buffer1.SequenceEqual(buffer2)) // should never be false...
                {
                    while (!buffer1.SequenceEqual(buffer2)) ; // but if it is, it certainly shouldn't go back to true...
                    throw new Exception("What in the world just happened?!");
                }
            }
        }

        ThreadPool.QueueUserWorkItem(Work);
        ThreadPool.QueueUserWorkItem(Work);
        Console.ReadLine();
    }
}

This runs without error on .NET 5.

On master on my Windows machine (I've not tried other OSes), within a few seconds I get a failure like this:

Unhandled exception. System.Exception: SequenceEquals changed from false to true?!
   at Program.Work() in C:\Users\stoub\source\repos\ConsoleApp17\ConsoleApp17\Program.cs:line 25
   at Program.<>c.<Main>b__0_0(Object _) in C:\Users\stoub\source\repos\ConsoleApp17\ConsoleApp17\Program.cs:line 10
   at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute() in D:\repos\runtime\src\libraries\System.Private.CoreLib\src\System\Threading\ThreadPool.cs:line 977
   at System.Threading.ThreadPoolWorkQueue.Dispatch() in D:\repos\runtime\src\libraries\System.Private.CoreLib\src\System\Threading\ThreadPool.cs:line 705
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart() in D:\repos\runtime\src\libraries\System.Private.CoreLib\src\System\Threading\PortableThreadPool.WorkerThread.cs:line 56
   at System.Threading.Thread.StartCallback() in D:\repos\runtime\src\coreclr\System.Private.CoreLib\src\System\Threading\Thread.CoreCLR.cs:line 105

Note that if I remove one of the ThreadPool calls so that there's no concurrency, it's never failed for me; only once I introduce the parallelism does it start to fail.

JIT bug? GC? Some kind of latent issue in SequenceEquals?

cc: @geoffkizer, @jkotas

Metadata

Metadata

Assignees

Labels

area-VM-coreclrtenet-reliabilityReliability/stability related issue (stress, load problems, etc.)

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions