Bug #113841 .NET Connector hangs in multi-threaded environment when pooling is enabled
Submitted: 1 Feb 1:40 Modified: 2 Apr 17:22
Reporter: Boris G Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:8.3.0 OS:Any
Assigned to: CPU Architecture:Any

[1 Feb 1:40] Boris G
Description:
In a multi-threaded application MySqlConnection.Open() hangs randomly.
v8.0.32 was the last version with no connection pooling problems.

Stack trace:
MySqlConnection.Open() in MySql.Data.MySqlClient, MySql.Data.dll
TaskAwaiter.HandleNonSuccessAndDebuggerNotification() in System.Runtime.CompilerServices, System.Private.CoreLib.dll
Task.InternalWaitCore() in System.Threading.Tasks, System.Private.CoreLib.dll
Task.SpinThenBlockingWait() in System.Threading.Tasks, System.Private.CoreLib.dll
ManualResetEventSlim.Wait() in System.Threading, System.Private.CoreLib.dll
Monitor.Wait() in System.Threading, System.Private.CoreLib.dll

The thread hangs here indefinitely.

How to repeat:
Here is an example xunit test:

    [Theory]
    [InlineData(128)]
    public void Test(int numConnections)
    {
        string connectionString = "Pooling=True;Min Pool Size=100;Max Pool Size=200;Connection Reset=False;Cache Server Properties=True;Server=127.0.0.1;Database=mydb;Uid=root;Pwd=mypw;";

        Task[] tasks =
            Enumerable.Range(1, numConnections)
                .Select(_ =>
                    Task.Run(() =>
                    {
                        using var connection = new MySqlConnection(connectionString);
                        connection.Open();
                    }))
                .ToArray();
        Assert.True(Task.WaitAll(tasks, 10000));
    }
[6 Feb 13:02] MySQL Verification Team
Hello Boris Giftge,

Thank you for the bug report.
Could you please provide repeatable test case(sample project, etc. - please make it as private if you prefer) to reproduce this issue at our end? 
Provided example is not enough to reproduce the issue. Thanks.

Regards,
Ashwini Patil
[6 Feb 17:53] Boris G
Hello Ashwini, Thanks for taking a look.
I reproduced the issue with the attached test on two different laptops (windows 11, .NET 8) and MySql Community Server 8.3. 
You may need to increase the number of connections if you have trouble reproducing it. 
I also attached the source files of the project in case there are other differences.
Thanks!
[7 Feb 13:10] MySQL Verification Team
Hello Boris,

Thank you for the test project and details.
I tried to reproduce your issue on windows 11 with Connector/NET 8.3 and MySQL Server 8.3 using test project attached but I am not seeing any issues at my end.
Tried increasing number of connections, still no issues found.

Regards,
Ashwini Patil
[8 Feb 20:23] Boris G
Hi Ashwini,
That's really odd. Another team member just ran this test on a MacBook and reproduced it reliably at 200+ connections. 
Please let me know if there's anything else I can do to assist.
As is, we are stuck with the connector version 8.0.32.
Thanks, Boris
[21 Feb 14:11] Istvan Kurta
We have a ASP.NET Web application with .NET framework 4.6.2, we are using the MySqlData.dll version 8.3 installed from nuget packages, and we are experiencing the same issue. In development environment the operating system is Windows 10, and the application is running from IIS. The application hang appears randomly, but when it occurs, it simply gets stuck at the connection.open line, and doesn't throw any exceptions or anything, it just stays there indefinitely. We have to kill the w3wp process from task manager in order to continue using the app.
In debug mode what we saw was by freezing the execution was, that around 10 operations were stuck at the connection.open line.
[20 Mar 21:41] Boris G
Is there any update on this?
[2 Apr 17:22] Boris G
I suspect https://bugs.mysql.com/bug.php?id=114272 fixes this issue.
pull request here: https://github.com/mysql/mysql-connector-net/pull/61