Repository: dotnet/runtime
File: src/libraries/System.Security.Cryptography.OpenSsl/tests/SafeEvpPKeyHandleTests.cs (C#)

Code before:
using Xunit;

namespace System.Security.Cryptography.OpenSsl.Tests
{
    public static class SafeEvpPKeyHandleTests
    {
        [Fact]
        public static void DuplicateHandle_ConcurrentWithDispose_DoesNotProduceInvalidHandle()
        {
            using ECDsaOpenSsl ecdsa = new ECDsaOpenSsl();

            for (int i = 0; i < 1000; i++)
            {
                SafeEvpPKeyHandle keyHandle = ecdsa.DuplicateKeyHandle();
                SafeEvpPKeyHandle? duplicated = null;

                Thread disposeThread = new Thread(() =>
                {
                    Thread.Sleep(Random.Shared.Next(0, 3));
                    keyHandle.Dispose();
                });

                Thread duplicateThread = new Thread(() =>
                {
                    Thread.Sleep(Random.Shared.Next(0, 3));
                    try { duplicated = keyHandle.DuplicateHandle(); } catch { }
                });

                disposeThread.Start();
                duplicateThread.Start();
                disposeThread.Join();
                duplicateThread.Join();

                if (duplicated is not null)
                {
                    bool refAdded = false;

                    try
                    {
                        duplicated.DangerousAddRef(ref refAdded);
                        Assert.NotEqual(IntPtr.Zero, duplicated.DangerousGetHandle());
                    }
                    finally
                    {
                        if (refAdded)
                        {
                            duplicated.DangerousRelease();
                        }
                    }

                    duplicated.Dispose();

Review comment (bug):
@copilot Please format this try/finally according to our coding guidelines. Each brace should be on a line of their own.

Add a comment inside the `catch` block that we explicitly are ignoring managed exceptions, and we are only interested in crashes.

Code after:
using Xunit;

namespace System.Security.Cryptography.OpenSsl.Tests
{
    public static class SafeEvpPKeyHandleTests
    {
        [Fact]
        public static void DuplicateHandle_ConcurrentWithDispose_DoesNotProduceInvalidHandle()
        {
            using ECDsaOpenSsl ecdsa = new ECDsaOpenSsl();

            for (int i = 0; i < 1000; i++)
            {
                SafeEvpPKeyHandle keyHandle = ecdsa.DuplicateKeyHandle();
                SafeEvpPKeyHandle? duplicated = null;

                Thread disposeThread = new Thread(() =>
                {
                    Thread.Sleep(Random.Shared.Next(0, 3));
                    keyHandle.Dispose();
                });

                Thread duplicateThread = new Thread(() =>
                {
                    Thread.Sleep(Random.Shared.Next(0, 3));
                    try
                    {
                        duplicated = keyHandle.DuplicateHandle();
                    }
                    catch
                    {
                        // We are only interested in crashes, not managed exceptions.
                    }
                });

                disposeThread.Start();
                duplicateThread.Start();
                disposeThread.Join();
                duplicateThread.Join();

                if (duplicated is not null)
                {
                    bool refAdded = false;

                    try
                    {
                        duplicated.DangerousAddRef(ref refAdded);
                        Assert.NotEqual(IntPtr.Zero, duplicated.DangerousGetHandle());
                    }
                    finally
                    {