Bug #112321 Pools thread-unsafe, causes: An item with the same key has already been added.
Submitted: 12 Sep 2023 11:58 Modified: 13 Sep 2023 13:00
Reporter: Wei Shi Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:8.0.33 OS:Any
Assigned to: CPU Architecture:Any

[12 Sep 2023 11:58] Wei Shi
Description:
See https://github.com/mysql/mysql-connector-net/commit/291c2f31fa9d7bf1b3c02bd7da19db1b66f89a...

In this commit (in 8.0.33), the lock in the GetPool method is modified to SemaphoreSlim.

But this semaphore instance does not work as expected. Because every time this method is called, a new SemaphoreSlim instance is created.

That is, there are no locks here. When concurrent, Pools.Add may throw an exception.

System.ArgumentException: An item with the same key has already been added. Key: {my connection string}
         at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
         at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
         at MySql.Data.MySqlClient.MySqlPoolManager.GetPoolAsync(MySqlConnectionStringBuilder settings, Boolean execAsync, CancellationToken cancellationToken)
         at MySql.Data.MySqlClient.MySqlConnection.OpenAsync(Boolean execAsync, CancellationToken cancellationToken)

How to repeat:
1. 8.0.33+
2. Use pools
3. Open MySqlConnections in multiple threads.

Suggested fix:
a. Still using lock(Pools) { ... }

b. Define semaphoreSlim in the class (static property), not in the method.
[13 Sep 2023 13:00] MySQL Verification Team
Hello!

Thank you for the bug report.
Imho this is duplicate of Bug #110717, please see Bug #110717.

Regards,
Ashwini Patil