[Mono-bugs] [Bug 61582][Nor] New - GetNamedDataSlot is not thread-safe

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Wed, 14 Jul 2004 21:11:35 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by sebastien.robitaille@croesus.com.

http://bugzilla.ximian.com/show_bug.cgi?id=61582

--- shadow/61582	2004-07-14 21:11:35.000000000 -0400
+++ shadow/61582.tmp.19369	2004-07-14 21:11:35.000000000 -0400
@@ -0,0 +1,106 @@
+Bug#: 61582
+Product: Mono: Class Libraries
+Version: unspecified
+OS: Red Hat 9.0
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: CORLIB
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: sebastien.robitaille@croesus.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: GetNamedDataSlot is not thread-safe
+
+Description of Problem:
+Calling GetNamedDataSlot from multiple threads throws an exception:
+
+Unhandled Exception: System.ArgumentException: Named data slot already 
+added
+
+Steps to reproduce the problem:
+1. Execute the following code:
+
+using System;
+using System.Threading;
+
+namespace MonoThreadingTest
+{
+	public class ThreadTest
+	{
+		static void ThreadCallback()
+		{
+			Thread.GetNamedDataSlot("TEST_SEBAS");
+		}
+
+		static void Main(string[] args)
+		{
+			for(int nIndex = 0; nIndex < 64; nIndex++)
+			{
+				(new Thread(new ThreadStart
+(ThreadCallback))).Start();
+			}
+
+			Thread.Sleep(5000);
+		}
+	}
+}
+
+
+Actual Results:
+Exception thrown.
+
+Expected Results:
+No exception.
+
+How often does this happen? 
+Very often.
+
+This problem also happens when using Remoting with multiple threads 
+(because GetNamedDataSlot is used by 
+System.Runtime.Remoting.Messaging.CallContext:SetCurrentCallContext )
+
+Additional Information:
+
+Proposed patch:
+
+Index: Thread.cs
+===================================================================
+RCS file: /mono/mcs/class/corlib/System.Threading/Thread.cs,v
+retrieving revision 1.54
+diff -u -r1.54 Thread.cs
+--- Thread.cs	15 Jun 2004 17:55:07 -0000	1.54
++++ Thread.cs	15 Jul 2004 00:58:25 -0000
+@@ -210,15 +210,18 @@
+ 		public extern static int GetDomainID();
+ 
+ 		public static LocalDataStoreSlot GetNamedDataSlot(string 
+name) {
+-			if (datastorehash == null)
+-				InitDataStoreHash ();
+-			LocalDataStoreSlot slot=(LocalDataStoreSlot)
+datastorehash[name];
++			lock(typeof(Thread))
++			{
++				if (datastorehash == null)
++					InitDataStoreHash ();
++				LocalDataStoreSlot slot=
+(LocalDataStoreSlot)datastorehash[name];
+ 
+-			if(slot==null) {
+-				slot=AllocateNamedDataSlot(name);
+-			}
++				if(slot==null) {
++					slot=AllocateNamedDataSlot(name);
++				}
+ 			
+-			return(slot);
++				return(slot);
++			}
+ 		}
+ 		
+ 		[MethodImplAttribute(MethodImplOptions.InternalCall)]