Bug 2952 - Race condition in System.Data.Common.DbConnection
Summary: Race condition in System.Data.Common.DbConnection
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Data ()
Version: 2.10.x
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-01-18 04:34 UTC by Vyacheslav
Modified: 2012-01-18 10:52 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Vyacheslav 2012-01-18 04:34:29 UTC
When SqlConnection+MetaDataCollections.get_Instance() is called in more than one thread System.Data.DuplicateNameException is thrown.

Stack trace:

System.Data.DuplicateNameException: A DataColumn named 'NumberOfRestrictions' already belongs to this DataTable.
  at System.Data.DataColumnCollection.RegisterName (System.String name, System.Data.DataColumn column) [0x00000] in <filename unknown>:0 
  at System.Data.DataColumnCollection.Add (System.Data.DataColumn column) [0x00000] in <filename unknown>:0 
  at System.Data.DataColumnCollection.Add (System.String columnName, System.Type type) [0x00000] in <filename unknown>:0 
  at System.Data.SqlClient.SqlConnection+MetaDataCollections.get_Instance () [0x00000] in <filename unknown>:0 
  at System.Data.SqlClient.SqlConnection.GetSchema (System.String collectionName, System.String[] restrictionValues) [0x00000] in <filename unknown>:0 
  at System.Data.SqlClient.SqlConnection.GetSchema (System.String collectionName) [0x00000] in <filename unknown>:0 

Code to reproduce the bug (connection data omitted):

using System;
using System.Data.SqlClient;
using System.Threading;

namespace Test_System.Data.SqlClient
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 2; ++i)
                new Thread(RunQuery).Start();
        }

        private static void RunQuery()
        {
            using (var con = new SqlConnection("Server=...;initial catalog=...;user=...;password=..."))
            {
                con.Open();
                var schema = con.GetSchema();
                Console.WriteLine(schema.TableName);
            }
        }
    }
}


Current implementation of the SqlConnection+MetaDataCollections.Instance property:

static public DataTable Instance {
	get {
		if (instance == null) {
			instance = new DataTable ("GetSchema");
			foreach (ColumnInfo c in columns)
				instance.Columns.Add (c.name, c.type);
			foreach (object [] row in rows)
				instance.LoadDataRow (row, true);
		}
		return instance;
	}
}


Maybe it should look something like this:

static public DataTable Instance {
	get {
		if (instance == null) {
			DataTable newInstance = new DataTable ("GetSchema");
			foreach (ColumnInfo c in columns)
				newInstance.Columns.Add (c.name, c.type);
			foreach (object [] row in rows)
				newInstance.LoadDataRow (row, true);
			instance = newInstance;
		}
		return instance;
	}
}


It seems the same applies to SqlConnection+DataTypes.Instance, SqlConnection+Restrictions.Instance, and SqlConnection+ReservedWords.Instance.
Comment 1 Miguel de Icaza [MSFT] 2012-01-18 10:52:47 UTC
Thanks fro the bug report and the suggestion, I have applied this patch.