Repository: dotnet/efcore
File: test/EFCore.Relational.Tests/RelationalConnectionTest.cs (C#)

Code before:
            // Start the reset task first, which will yield and then try to reset
            var resetTask = Task.Run(async () =>
            {
                try
                {
                    // Small delay to increase chance of race condition with HandleTransactionCompleted
                    await Task.Yield();

                    // ResetState calls ClearTransactions which might race with HandleTransactionCompleted
                    ((IResettableService)connection).ResetState();
                }
                catch (Exception ex)
                {
                    lock (exceptions)
                    {
                        exceptions.Add(ex);
                    }
                }
            });

            // Dispose the scope on the main thread, which will trigger the TransactionCompleted event
            // The event handler (HandleTransactionCompleted) may execute on a different thread and race
            // with the ClearTransactions call in resetTask
            scope.Dispose();

            Task.WaitAll(new[] { resetTask }, TimeSpan.FromSeconds(10));
        }

        Assert.Empty(exceptions);
    }

    private static IDbContextOptions CreateOptions(params RelationalOptionsExtension[] optionsExtensions)
    {
        var optionsBuilder = new DbContextOptionsBuilder();

        foreach (var optionsExtension in optionsExtensions)
        {
            ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(optionsExtension);
        }

        return optionsBuilder.Options;
    }

    private class FakeLoggingOptions(bool sensitiveDataLoggingEnabled, bool detailedErrorsEnabled = false) : ILoggingOptions
    {
        public void Initialize(IDbContextOptions options)
        {
        }

        public void Validate(IDbContextOptions options)
        {

Review comment (refactor):
@copilot Simplify this call. Don't store thrown exceptions, let them be unhandled. Also add another loop where `ResetState` is called before `scope.Complete()`

Code after:

            // Start the reset task first, which will yield and then try to reset
            var resetTask = Task.Run(() =>
            {
                // ResetState calls ClearTransactions which might race with HandleTransactionCompleted
                ((IResettableService)connection).ResetState();
            });

            // Dispose the scope on the main thread, which will trigger the TransactionCompleted event
            // The event handler (HandleTransactionCompleted) may execute on a different thread and race
            // with the ClearTransactions call in resetTask
            scope.Dispose();

            await resetTask;
        }

        // Test with ResetState() before scope.Complete()
        for (var i = 0; i < Environment.ProcessorCount; i++)
        {
            var connection = new FakeRelationalConnection(
                CreateOptions(new FakeRelationalOptionsExtension().WithConnectionString("Database=ConcurrencyTest")));

            var scope = new TransactionScope();
            connection.Open();

            // Start the reset task first
            var resetTask = Task.Run(async () =>
            {
                // Small delay to increase chance of race condition
                await Task.Yield();

                // ResetState calls ClearTransactions which might race with HandleTransactionCompleted
                ((IResettableService)connection).ResetState();
            });

            // Complete and dispose the scope, which will trigger the TransactionCompleted event
            scope.Complete();
            scope.Dispose();

            await resetTask;
        }
    }

    private static IDbContextOptions CreateOptions(params RelationalOptionsExtension[] optionsExtensions)
    {
        var optionsBuilder = new DbContextOptionsBuilder();

        foreach (var optionsExtension in optionsExtensions)
        {
            ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(optionsExtension);
        }