aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine/XEngine/XEngine.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/XEngine/XEngine.cs418
1 files changed, 249 insertions, 169 deletions
diff --git a/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs b/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs
index 92fb934..c8f6559 100644
--- a/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs
+++ b/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs
@@ -30,6 +30,7 @@ using System.IO;
30using System.Threading; 30using System.Threading;
31using System.Collections; 31using System.Collections;
32using System.Collections.Generic; 32using System.Collections.Generic;
33using System.Diagnostics; //for [DebuggerNonUserCode]
33using System.Security; 34using System.Security;
34using System.Security.Policy; 35using System.Security.Policy;
35using System.Reflection; 36using System.Reflection;
@@ -102,6 +103,8 @@ namespace OpenSim.Region.ScriptEngine.XEngine
102 private Dictionary<UUID, IScriptInstance> m_Scripts = 103 private Dictionary<UUID, IScriptInstance> m_Scripts =
103 new Dictionary<UUID, IScriptInstance>(); 104 new Dictionary<UUID, IScriptInstance>();
104 105
106 private OpenMetaverse.ReaderWriterLockSlim m_scriptsLock = new OpenMetaverse.ReaderWriterLockSlim();
107
105 // Maps the asset ID to the assembly 108 // Maps the asset ID to the assembly
106 109
107 private Dictionary<UUID, string> m_Assemblies = 110 private Dictionary<UUID, string> m_Assemblies =
@@ -124,6 +127,71 @@ namespace OpenSim.Region.ScriptEngine.XEngine
124 IWorkItemResult m_CurrentCompile = null; 127 IWorkItemResult m_CurrentCompile = null;
125 private Dictionary<UUID, int> m_CompileDict = new Dictionary<UUID, int>(); 128 private Dictionary<UUID, int> m_CompileDict = new Dictionary<UUID, int>();
126 129
130 private void lockScriptsForRead(bool locked)
131 {
132 if (locked)
133 {
134 if (m_scriptsLock.RecursiveReadCount > 0)
135 {
136 m_log.Error("[XEngine.m_Scripts] Recursive read lock requested. This should not happen and means something needs to be fixed. For now though, it's safe to continue.");
137 m_scriptsLock.ExitReadLock();
138 }
139 if (m_scriptsLock.RecursiveWriteCount > 0)
140 {
141 m_log.Error("[XEngine.m_Scripts] Recursive write lock requested. This should not happen and means something needs to be fixed.");
142 m_scriptsLock.ExitWriteLock();
143 }
144
145 while (!m_scriptsLock.TryEnterReadLock(60000))
146 {
147 m_log.Error("[XEngine.m_Scripts] Thread lock detected while trying to aquire READ lock of m_scripts in XEngine. I'm going to try to solve the thread lock automatically to preserve region stability, but this needs to be fixed.");
148 if (m_scriptsLock.IsWriteLockHeld)
149 {
150 m_scriptsLock = new OpenMetaverse.ReaderWriterLockSlim();
151 }
152 }
153 }
154 else
155 {
156 if (m_scriptsLock.RecursiveReadCount > 0)
157 {
158 m_scriptsLock.ExitReadLock();
159 }
160 }
161 }
162 private void lockScriptsForWrite(bool locked)
163 {
164 if (locked)
165 {
166 if (m_scriptsLock.RecursiveReadCount > 0)
167 {
168 m_log.Error("[XEngine.m_Scripts] Recursive read lock requested. This should not happen and means something needs to be fixed. For now though, it's safe to continue.");
169 m_scriptsLock.ExitReadLock();
170 }
171 if (m_scriptsLock.RecursiveWriteCount > 0)
172 {
173 m_log.Error("[XEngine.m_Scripts] Recursive write lock requested. This should not happen and means something needs to be fixed.");
174 m_scriptsLock.ExitWriteLock();
175 }
176
177 while (!m_scriptsLock.TryEnterWriteLock(60000))
178 {
179 m_log.Error("[XEngine.m_Scripts] Thread lock detected while trying to aquire WRITE lock of m_scripts in XEngine. I'm going to try to solve the thread lock automatically to preserve region stability, but this needs to be fixed.");
180 if (m_scriptsLock.IsWriteLockHeld)
181 {
182 m_scriptsLock = new OpenMetaverse.ReaderWriterLockSlim();
183 }
184 }
185 }
186 else
187 {
188 if (m_scriptsLock.RecursiveWriteCount > 0)
189 {
190 m_scriptsLock.ExitWriteLock();
191 }
192 }
193 }
194
127 public string ScriptEngineName 195 public string ScriptEngineName
128 { 196 {
129 get { return "XEngine"; } 197 get { return "XEngine"; }
@@ -263,43 +331,45 @@ namespace OpenSim.Region.ScriptEngine.XEngine
263 331
264 public void RemoveRegion(Scene scene) 332 public void RemoveRegion(Scene scene)
265 { 333 {
266 lock (m_Scripts) 334 lockScriptsForRead(true);
335 foreach (IScriptInstance instance in m_Scripts.Values)
267 { 336 {
268 foreach (IScriptInstance instance in m_Scripts.Values) 337 // Force a final state save
338 //
339 if (m_Assemblies.ContainsKey(instance.AssetID))
269 { 340 {
270 // Force a final state save 341 string assembly = m_Assemblies[instance.AssetID];
271 // 342 instance.SaveState(assembly);
272 if (m_Assemblies.ContainsKey(instance.AssetID)) 343 }
273 {
274 string assembly = m_Assemblies[instance.AssetID];
275 instance.SaveState(assembly);
276 }
277 344
278 // Clear the event queue and abort the instance thread 345 // Clear the event queue and abort the instance thread
279 // 346 //
280 instance.ClearQueue(); 347 instance.ClearQueue();
281 instance.Stop(0); 348 instance.Stop(0);
282 349
283 // Release events, timer, etc 350 // Release events, timer, etc
284 // 351 //
285 instance.DestroyScriptInstance(); 352 instance.DestroyScriptInstance();
286 353
287 // Unload scripts and app domains 354 // Unload scripts and app domains
288 // Must be done explicitly because they have infinite 355 // Must be done explicitly because they have infinite
289 // lifetime 356 // lifetime
290 // 357 //
291 m_DomainScripts[instance.AppDomain].Remove(instance.ItemID); 358 m_DomainScripts[instance.AppDomain].Remove(instance.ItemID);
292 if (m_DomainScripts[instance.AppDomain].Count == 0) 359 if (m_DomainScripts[instance.AppDomain].Count == 0)
293 { 360 {
294 m_DomainScripts.Remove(instance.AppDomain); 361 m_DomainScripts.Remove(instance.AppDomain);
295 UnloadAppDomain(instance.AppDomain); 362 UnloadAppDomain(instance.AppDomain);
296 }
297 } 363 }
298 m_Scripts.Clear();
299 m_PrimObjects.Clear();
300 m_Assemblies.Clear();
301 m_DomainScripts.Clear();
302 } 364 }
365 lockScriptsForRead(false);
366 lockScriptsForWrite(true);
367 m_Scripts.Clear();
368 lockScriptsForWrite(false);
369 m_PrimObjects.Clear();
370 m_Assemblies.Clear();
371 m_DomainScripts.Clear();
372
303 lock (m_ScriptEngines) 373 lock (m_ScriptEngines)
304 { 374 {
305 m_ScriptEngines.Remove(this); 375 m_ScriptEngines.Remove(this);
@@ -358,22 +428,20 @@ namespace OpenSim.Region.ScriptEngine.XEngine
358 428
359 List<IScriptInstance> instances = new List<IScriptInstance>(); 429 List<IScriptInstance> instances = new List<IScriptInstance>();
360 430
361 lock (m_Scripts) 431 lockScriptsForRead(true);
362 { 432 foreach (IScriptInstance instance in m_Scripts.Values)
363 foreach (IScriptInstance instance in m_Scripts.Values)
364 instances.Add(instance); 433 instances.Add(instance);
365 } 434 lockScriptsForRead(false);
366 435
367 foreach (IScriptInstance i in instances) 436 foreach (IScriptInstance i in instances)
368 { 437 {
369 string assembly = String.Empty; 438 string assembly = String.Empty;
370 439
371 lock (m_Scripts) 440
372 {
373 if (!m_Assemblies.ContainsKey(i.AssetID)) 441 if (!m_Assemblies.ContainsKey(i.AssetID))
374 continue; 442 continue;
375 assembly = m_Assemblies[i.AssetID]; 443 assembly = m_Assemblies[i.AssetID];
376 } 444
377 445
378 i.SaveState(assembly); 446 i.SaveState(assembly);
379 } 447 }
@@ -701,111 +769,117 @@ namespace OpenSim.Region.ScriptEngine.XEngine
701 } 769 }
702 } 770 }
703 771
704 lock (m_Scripts) 772
773
774 ScriptInstance instance = null;
775 // Create the object record
776 lockScriptsForRead(true);
777 if ((!m_Scripts.ContainsKey(itemID)) ||
778 (m_Scripts[itemID].AssetID != assetID))
705 { 779 {
706 ScriptInstance instance = null; 780 lockScriptsForRead(false);
707 // Create the object record
708 781
709 if ((!m_Scripts.ContainsKey(itemID)) || 782 UUID appDomain = assetID;
710 (m_Scripts[itemID].AssetID != assetID))
711 {
712 UUID appDomain = assetID;
713 783
714 if (part.ParentGroup.IsAttachment) 784 if (part.ParentGroup.IsAttachment)
715 appDomain = part.ParentGroup.RootPart.UUID; 785 appDomain = part.ParentGroup.RootPart.UUID;
716 786
717 if (!m_AppDomains.ContainsKey(appDomain)) 787 if (!m_AppDomains.ContainsKey(appDomain))
788 {
789 try
718 { 790 {
719 try 791 AppDomainSetup appSetup = new AppDomainSetup();
720 { 792 // appSetup.ApplicationBase = Path.Combine(
721 AppDomainSetup appSetup = new AppDomainSetup(); 793 // "ScriptEngines",
722// appSetup.ApplicationBase = Path.Combine( 794 // m_Scene.RegionInfo.RegionID.ToString());
723// "ScriptEngines", 795
724// m_Scene.RegionInfo.RegionID.ToString()); 796 Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
725 797 Evidence evidence = new Evidence(baseEvidence);
726 Evidence baseEvidence = AppDomain.CurrentDomain.Evidence; 798
727 Evidence evidence = new Evidence(baseEvidence); 799 AppDomain sandbox;
728 800 if (m_AppDomainLoading)
729 AppDomain sandbox; 801 sandbox = AppDomain.CreateDomain(
730 if (m_AppDomainLoading) 802 m_Scene.RegionInfo.RegionID.ToString(),
731 sandbox = AppDomain.CreateDomain( 803 evidence, appSetup);
732 m_Scene.RegionInfo.RegionID.ToString(), 804 else
733 evidence, appSetup); 805 sandbox = AppDomain.CurrentDomain;
734 else 806
735 sandbox = AppDomain.CurrentDomain; 807 //PolicyLevel sandboxPolicy = PolicyLevel.CreateAppDomainLevel();
736 808 //AllMembershipCondition sandboxMembershipCondition = new AllMembershipCondition();
737 //PolicyLevel sandboxPolicy = PolicyLevel.CreateAppDomainLevel(); 809 //PermissionSet sandboxPermissionSet = sandboxPolicy.GetNamedPermissionSet("Internet");
738 //AllMembershipCondition sandboxMembershipCondition = new AllMembershipCondition(); 810 //PolicyStatement sandboxPolicyStatement = new PolicyStatement(sandboxPermissionSet);
739 //PermissionSet sandboxPermissionSet = sandboxPolicy.GetNamedPermissionSet("Internet"); 811 //CodeGroup sandboxCodeGroup = new UnionCodeGroup(sandboxMembershipCondition, sandboxPolicyStatement);
740 //PolicyStatement sandboxPolicyStatement = new PolicyStatement(sandboxPermissionSet); 812 //sandboxPolicy.RootCodeGroup = sandboxCodeGroup;
741 //CodeGroup sandboxCodeGroup = new UnionCodeGroup(sandboxMembershipCondition, sandboxPolicyStatement); 813 //sandbox.SetAppDomainPolicy(sandboxPolicy);
742 //sandboxPolicy.RootCodeGroup = sandboxCodeGroup; 814
743 //sandbox.SetAppDomainPolicy(sandboxPolicy); 815 m_AppDomains[appDomain] = sandbox;
744 816
745 m_AppDomains[appDomain] = sandbox; 817 m_AppDomains[appDomain].AssemblyResolve +=
746 818 new ResolveEventHandler(
747 m_AppDomains[appDomain].AssemblyResolve += 819 AssemblyResolver.OnAssemblyResolve);
748 new ResolveEventHandler( 820 m_DomainScripts[appDomain] = new List<UUID>();
749 AssemblyResolver.OnAssemblyResolve);
750 m_DomainScripts[appDomain] = new List<UUID>();
751 }
752 catch (Exception e)
753 {
754 m_log.ErrorFormat("[XEngine] Exception creating app domain:\n {0}", e.ToString());
755 m_ScriptErrorMessage += "Exception creating app domain:\n";
756 m_ScriptFailCount++;
757 lock (m_AddingAssemblies)
758 {
759 m_AddingAssemblies[assembly]--;
760 }
761 return false;
762 }
763 } 821 }
764 m_DomainScripts[appDomain].Add(itemID); 822 catch (Exception e)
765
766 instance = new ScriptInstance(this, part,
767 itemID, assetID, assembly,
768 m_AppDomains[appDomain],
769 part.ParentGroup.RootPart.Name,
770 item.Name, startParam, postOnRez,
771 stateSource, m_MaxScriptQueue);
772
773 m_log.DebugFormat("[XEngine] Loaded script {0}.{1}, script UUID {2}, prim UUID {3} @ {4}",
774 part.ParentGroup.RootPart.Name, item.Name, assetID, part.UUID, part.ParentGroup.RootPart.AbsolutePosition.ToString());
775
776 if (presence != null)
777 { 823 {
778 ShowScriptSaveResponse(item.OwnerID, 824 m_log.ErrorFormat("[XEngine] Exception creating app domain:\n {0}", e.ToString());
779 assetID, "Compile successful", true); 825 m_ScriptErrorMessage += "Exception creating app domain:\n";
826 m_ScriptFailCount++;
827 lock (m_AddingAssemblies)
828 {
829 m_AddingAssemblies[assembly]--;
830 }
831 return false;
780 } 832 }
833 }
834 m_DomainScripts[appDomain].Add(itemID);
781 835
782 instance.AppDomain = appDomain; 836 instance = new ScriptInstance(this, part,
783 instance.LineMap = linemap; 837 itemID, assetID, assembly,
838 m_AppDomains[appDomain],
839 part.ParentGroup.RootPart.Name,
840 item.Name, startParam, postOnRez,
841 stateSource, m_MaxScriptQueue);
784 842
785 m_Scripts[itemID] = instance; 843 m_log.DebugFormat("[XEngine] Loaded script {0}.{1}, script UUID {2}, prim UUID {3} @ {4}",
786 } 844 part.ParentGroup.RootPart.Name, item.Name, assetID, part.UUID, part.ParentGroup.RootPart.AbsolutePosition.ToString());
787 845
788 lock (m_PrimObjects) 846 if (presence != null)
789 { 847 {
790 if (!m_PrimObjects.ContainsKey(localID)) 848 ShowScriptSaveResponse(item.OwnerID,
791 m_PrimObjects[localID] = new List<UUID>(); 849 assetID, "Compile successful", true);
850 }
792 851
793 if (!m_PrimObjects[localID].Contains(itemID)) 852 instance.AppDomain = appDomain;
794 m_PrimObjects[localID].Add(itemID); 853 instance.LineMap = linemap;
854 lockScriptsForWrite(true);
855 m_Scripts[itemID] = instance;
856 lockScriptsForWrite(false);
857 }
858 else
859 {
860 lockScriptsForRead(false);
861 }
862 lock (m_PrimObjects)
863 {
864 if (!m_PrimObjects.ContainsKey(localID))
865 m_PrimObjects[localID] = new List<UUID>();
795 866
796 } 867 if (!m_PrimObjects[localID].Contains(itemID))
868 m_PrimObjects[localID].Add(itemID);
797 869
798 if (!m_Assemblies.ContainsKey(assetID)) 870 }
799 m_Assemblies[assetID] = assembly;
800 871
801 lock (m_AddingAssemblies) 872 if (!m_Assemblies.ContainsKey(assetID))
802 { 873 m_Assemblies[assetID] = assembly;
803 m_AddingAssemblies[assembly]--;
804 }
805 874
806 if (instance!=null) 875 lock (m_AddingAssemblies)
807 instance.Init(); 876 {
877 m_AddingAssemblies[assembly]--;
808 } 878 }
879
880 if (instance!=null)
881 instance.Init();
882
809 return true; 883 return true;
810 } 884 }
811 885
@@ -820,58 +894,61 @@ namespace OpenSim.Region.ScriptEngine.XEngine
820 894
821 lock (m_Scripts) 895 lock (m_Scripts)
822 { 896 {
823 // Do we even have it? 897 lockScriptsForRead(false);
824 if (!m_Scripts.ContainsKey(itemID)) 898 return;
825 return; 899 }
900
826 901
827 IScriptInstance instance=m_Scripts[itemID]; 902 IScriptInstance instance=m_Scripts[itemID];
828 m_Scripts.Remove(itemID); 903 lockScriptsForRead(false);
829 904 lockScriptsForWrite(true);
830 instance.ClearQueue(); 905 m_Scripts.Remove(itemID);
831 instance.Stop(0); 906 lockScriptsForWrite(false);
907 instance.ClearQueue();
908 instance.Stop(0);
832 909
833// bool objectRemoved = false; 910// bool objectRemoved = false;
834 911
835 lock (m_PrimObjects) 912 lock (m_PrimObjects)
913 {
914 // Remove the script from it's prim
915 if (m_PrimObjects.ContainsKey(localID))
836 { 916 {
837 // Remove the script from it's prim 917 // Remove inventory item record
838 if (m_PrimObjects.ContainsKey(localID)) 918 if (m_PrimObjects[localID].Contains(itemID))
839 { 919 m_PrimObjects[localID].Remove(itemID);
840 // Remove inventory item record
841 if (m_PrimObjects[localID].Contains(itemID))
842 m_PrimObjects[localID].Remove(itemID);
843 920
844 // If there are no more scripts, remove prim 921 // If there are no more scripts, remove prim
845 if (m_PrimObjects[localID].Count == 0) 922 if (m_PrimObjects[localID].Count == 0)
846 { 923 {
847 m_PrimObjects.Remove(localID); 924 m_PrimObjects.Remove(localID);
848// objectRemoved = true; 925// objectRemoved = true;
849 }
850 } 926 }
851 } 927 }
928 }
852 929
853 instance.RemoveState(); 930 instance.RemoveState();
854 instance.DestroyScriptInstance(); 931 instance.DestroyScriptInstance();
855 932
856 m_DomainScripts[instance.AppDomain].Remove(instance.ItemID); 933 m_DomainScripts[instance.AppDomain].Remove(instance.ItemID);
857 if (m_DomainScripts[instance.AppDomain].Count == 0) 934 if (m_DomainScripts[instance.AppDomain].Count == 0)
858 { 935 {
859 m_DomainScripts.Remove(instance.AppDomain); 936 m_DomainScripts.Remove(instance.AppDomain);
860 UnloadAppDomain(instance.AppDomain); 937 UnloadAppDomain(instance.AppDomain);
861 } 938 }
862
863 instance = null;
864 939
865 ObjectRemoved handlerObjectRemoved = OnObjectRemoved; 940 instance = null;
866 if (handlerObjectRemoved != null)
867 {
868 SceneObjectPart part = m_Scene.GetSceneObjectPart(localID);
869 handlerObjectRemoved(part.UUID);
870 }
871 941
872 CleanAssemblies(); 942 ObjectRemoved handlerObjectRemoved = OnObjectRemoved;
943 if (handlerObjectRemoved != null)
944 {
945 SceneObjectPart part = m_Scene.GetSceneObjectPart(localID);
946 handlerObjectRemoved(part.UUID);
873 } 947 }
874 948
949 CleanAssemblies();
950
951
875 ScriptRemoved handlerScriptRemoved = OnScriptRemoved; 952 ScriptRemoved handlerScriptRemoved = OnScriptRemoved;
876 if (handlerScriptRemoved != null) 953 if (handlerScriptRemoved != null)
877 handlerScriptRemoved(itemID); 954 handlerScriptRemoved(itemID);
@@ -1123,12 +1200,14 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1123 private IScriptInstance GetInstance(UUID itemID) 1200 private IScriptInstance GetInstance(UUID itemID)
1124 { 1201 {
1125 IScriptInstance instance; 1202 IScriptInstance instance;
1126 lock (m_Scripts) 1203 lockScriptsForRead(true);
1204 if (!m_Scripts.ContainsKey(itemID))
1127 { 1205 {
1128 if (!m_Scripts.ContainsKey(itemID)) 1206 lockScriptsForRead(false);
1129 return null; 1207 return null;
1130 instance = m_Scripts[itemID];
1131 } 1208 }
1209 instance = m_Scripts[itemID];
1210 lockScriptsForRead(false);
1132 return instance; 1211 return instance;
1133 } 1212 }
1134 1213
@@ -1152,6 +1231,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1152 return false; 1231 return false;
1153 } 1232 }
1154 1233
1234 [DebuggerNonUserCode]
1155 public void ApiResetScript(UUID itemID) 1235 public void ApiResetScript(UUID itemID)
1156 { 1236 {
1157 IScriptInstance instance = GetInstance(itemID); 1237 IScriptInstance instance = GetInstance(itemID);
@@ -1203,6 +1283,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1203 return UUID.Zero; 1283 return UUID.Zero;
1204 } 1284 }
1205 1285
1286 [DebuggerNonUserCode]
1206 public void SetState(UUID itemID, string newState) 1287 public void SetState(UUID itemID, string newState)
1207 { 1288 {
1208 IScriptInstance instance = GetInstance(itemID); 1289 IScriptInstance instance = GetInstance(itemID);
@@ -1223,11 +1304,10 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1223 { 1304 {
1224 List<IScriptInstance> instances = new List<IScriptInstance>(); 1305 List<IScriptInstance> instances = new List<IScriptInstance>();
1225 1306
1226 lock (m_Scripts) 1307 lockScriptsForRead(true);
1227 { 1308 foreach (IScriptInstance instance in m_Scripts.Values)
1228 foreach (IScriptInstance instance in m_Scripts.Values)
1229 instances.Add(instance); 1309 instances.Add(instance);
1230 } 1310 lockScriptsForRead(false);
1231 1311
1232 foreach (IScriptInstance i in instances) 1312 foreach (IScriptInstance i in instances)
1233 { 1313 {