Bug #4027 ByteFX.Data.dll: Item has already been added
Submitted: 6 Jun 2004 21:26 Modified: 5 Sep 2004 21:58
Reporter: Casper Hornstrup Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version: OS:Windows (Windows Server 2003)
Assigned to: Assigned Account CPU Architecture:Any

[6 Jun 2004 21:26] Casper Hornstrup
Description:
With a multithreaded application and ByteFX.Data.dll with file version 0.7.2.13867 on an dual Xeon machine, I get random exceptions. The little test application gives me:

Unhandled Exception: System.ArgumentException: Item has already been added.  Key in dictionary: "latin5"  Key being added: "latin5"
   at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
   at System.Collections.Hashtable.Add(Object key, Object value)
   at ByteFX.Data.MySqlClient.CharSetMap.InitializeMapping()
   at ByteFX.Data.MySqlClient.CharSetMap.GetEncoding(String MySqlCharSetName)
   at ByteFX.Data.MySqlClient.MySqlConnection.Open()
   at bytefxtest.Worker.Start() in c:\cvshome\bytefxtest\bytefxtest\class1.cs:line 19 

I suspect the ByteFX ADO.NET provider is not thread safe. The test passes on an non-SMP machine.

How to repeat:
Run the test on a SMP machine.

using System;
using System.Data;
using System.Threading;
using ByteFX.Data.MySqlClient;

namespace bytefxtest
{
	public class Worker
	{
		private static readonly string connectionString = "Server=localhost;Database=mysql;User ID=root;Password=";

		public void Start()
		{
			while (true)
			{
				using (IDbConnection con = new MySqlConnection(connectionString))
				{
					Console.WriteLine("Open");
					con.Open();
					Thread.Sleep(10);
					Console.WriteLine("Close");
				}
			}
		}
	}

	class Class1
	{
		[STAThread]
		static void Main(string[] args)
		{
			for (int i = 0; i < 50; i++)
			{
				Worker worker = new Worker();
				Thread workerThread = new Thread(new ThreadStart(worker.Start));
				workerThread.Start();
			}
			Console.ReadLine();
		}
	}
}

Suggested fix:
Synchronize access to shared structures.
[6 Jun 2004 22:35] Casper Hornstrup
Upgraded to ByteFX.MySqlClient.dll, file version 0.7.6.15073. The other problem seems gone, but now the test case can output (still only on the SMP machine):

Unhandled Exception: ByteFX.Data.MySqlClient.MySqlException: Failure to initialize connection
   at ByteFX.Data.MySqlClient.MySqlInternalConnection.SetServerVariables(MySqlConnection connection)
   at ByteFX.Data.MySqlClient.MySqlConnection.Open()
   at bytefxtest.Worker.Start() in c:\cvshome\bytefxtest\bytefxtest\class1.cs:line 19
[17 Jul 2004 4:05] Matthew Lord
Hi Casper,

I'm extremely new to .NET.  When you say "run this app" what exactly should I do?  I'm reading 
the documentation now but if you could tell me the steps it would help the bug report come along 
quicker.

Thanks
[20 Jul 2004 21:48] Matthew Lord
Please make sure that you have ByteFX.Data.MySqlClient as the resource as the attached source 
file has .data. which will not build.
[5 Aug 2004 21:58] Reggie Burnett
Casper

What version of MySQL are you using?  Also, can you trap the exception you receive and look at the InnerExceptoin.  If it is not null, please let us know what it tells  you.

Thanks
[14 Feb 2005 22:54] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
[31 Jul 2005 7:19] Slava Kisilevich
I had the same bug (plus the bug #11744). It is all about multithreading and
thread-safetiness. It is evident that MySqlConnection.Open is not thread safe.
Just add a LOCK on the Open function and the problem is gone

lock(dummySyncObject)
{
  mySqlConnection.Open()
}