aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/XEngine
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine/XEngine')
-rw-r--r--OpenSim/Region/ScriptEngine/XEngine/XEngine.cs353
1 files changed, 222 insertions, 131 deletions
diff --git a/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs b/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs
index 97ab411..9a78a42 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"; }
@@ -269,43 +337,45 @@ namespace OpenSim.Region.ScriptEngine.XEngine
269 337
270 public void RemoveRegion(Scene scene) 338 public void RemoveRegion(Scene scene)
271 { 339 {
272 lock (m_Scripts) 340 lockScriptsForRead(true);
341 foreach (IScriptInstance instance in m_Scripts.Values)
273 { 342 {
274 foreach (IScriptInstance instance in m_Scripts.Values) 343 // Force a final state save
344 //
345 if (m_Assemblies.ContainsKey(instance.AssetID))
275 { 346 {
276 // Force a final state save 347 string assembly = m_Assemblies[instance.AssetID];
277 // 348 instance.SaveState(assembly);
278 if (m_Assemblies.ContainsKey(instance.AssetID)) 349 }
279 {
280 string assembly = m_Assemblies[instance.AssetID];
281 instance.SaveState(assembly);
282 }
283 350
284 // Clear the event queue and abort the instance thread 351 // Clear the event queue and abort the instance thread
285 // 352 //
286 instance.ClearQueue(); 353 instance.ClearQueue();
287 instance.Stop(0); 354 instance.Stop(0);
288 355
289 // Release events, timer, etc 356 // Release events, timer, etc
290 // 357 //
291 instance.DestroyScriptInstance(); 358 instance.DestroyScriptInstance();
292 359
293 // Unload scripts and app domains 360 // Unload scripts and app domains
294 // Must be done explicitly because they have infinite 361 // Must be done explicitly because they have infinite
295 // lifetime 362 // lifetime
296 // 363 //
297 m_DomainScripts[instance.AppDomain].Remove(instance.ItemID); 364 m_DomainScripts[instance.AppDomain].Remove(instance.ItemID);
298 if (m_DomainScripts[instance.AppDomain].Count == 0) 365 if (m_DomainScripts[instance.AppDomain].Count == 0)
299 { 366 {
300 m_DomainScripts.Remove(instance.AppDomain); 367 m_DomainScripts.Remove(instance.AppDomain);
301 UnloadAppDomain(instance.AppDomain); 368 UnloadAppDomain(instance.AppDomain);
302 }
303 } 369 }
304 m_Scripts.Clear();
305 m_PrimObjects.Clear();
306 m_Assemblies.Clear();
307 m_DomainScripts.Clear();
308 } 370 }
371 lockScriptsForRead(false);
372 lockScriptsForWrite(true);
373 m_Scripts.Clear();
374 lockScriptsForWrite(false);
375 m_PrimObjects.Clear();
376 m_Assemblies.Clear();
377 m_DomainScripts.Clear();
378
309 lock (m_ScriptEngines) 379 lock (m_ScriptEngines)
310 { 380 {
311 m_ScriptEngines.Remove(this); 381 m_ScriptEngines.Remove(this);
@@ -364,22 +434,20 @@ namespace OpenSim.Region.ScriptEngine.XEngine
364 434
365 List<IScriptInstance> instances = new List<IScriptInstance>(); 435 List<IScriptInstance> instances = new List<IScriptInstance>();
366 436
367 lock (m_Scripts) 437 lockScriptsForRead(true);
368 { 438 foreach (IScriptInstance instance in m_Scripts.Values)
369 foreach (IScriptInstance instance in m_Scripts.Values)
370 instances.Add(instance); 439 instances.Add(instance);
371 } 440 lockScriptsForRead(false);
372 441
373 foreach (IScriptInstance i in instances) 442 foreach (IScriptInstance i in instances)
374 { 443 {
375 string assembly = String.Empty; 444 string assembly = String.Empty;
376 445
377 lock (m_Scripts) 446
378 {
379 if (!m_Assemblies.ContainsKey(i.AssetID)) 447 if (!m_Assemblies.ContainsKey(i.AssetID))
380 continue; 448 continue;
381 assembly = m_Assemblies[i.AssetID]; 449 assembly = m_Assemblies[i.AssetID];
382 } 450
383 451
384 i.SaveState(assembly); 452 i.SaveState(assembly);
385 } 453 }
@@ -708,92 +776,95 @@ namespace OpenSim.Region.ScriptEngine.XEngine
708 } 776 }
709 777
710 ScriptInstance instance = null; 778 ScriptInstance instance = null;
711 lock (m_Scripts) 779 // Create the object record
780 lockScriptsForRead(true);
781 if ((!m_Scripts.ContainsKey(itemID)) ||
782 (m_Scripts[itemID].AssetID != assetID))
712 { 783 {
713 // Create the object record 784 lockScriptsForRead(false);
714 785
715 if ((!m_Scripts.ContainsKey(itemID)) || 786 UUID appDomain = assetID;
716 (m_Scripts[itemID].AssetID != assetID))
717 {
718 UUID appDomain = assetID;
719 787
720 if (part.ParentGroup.IsAttachment) 788 if (part.ParentGroup.IsAttachment)
721 appDomain = part.ParentGroup.RootPart.UUID; 789 appDomain = part.ParentGroup.RootPart.UUID;
722 790
723 if (!m_AppDomains.ContainsKey(appDomain)) 791 if (!m_AppDomains.ContainsKey(appDomain))
792 {
793 try
724 { 794 {
725 try 795 AppDomainSetup appSetup = new AppDomainSetup();
726 { 796 appSetup.PrivateBinPath = Path.Combine(
727 AppDomainSetup appSetup = new AppDomainSetup(); 797 m_ScriptEnginesPath,
728 appSetup.PrivateBinPath = Path.Combine( 798 m_Scene.RegionInfo.RegionID.ToString());
729 m_ScriptEnginesPath, 799
730 m_Scene.RegionInfo.RegionID.ToString()); 800 Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
731 801 Evidence evidence = new Evidence(baseEvidence);
732 Evidence baseEvidence = AppDomain.CurrentDomain.Evidence; 802
733 Evidence evidence = new Evidence(baseEvidence); 803 AppDomain sandbox;
734 804 if (m_AppDomainLoading)
735 AppDomain sandbox; 805 sandbox = AppDomain.CreateDomain(
736 if (m_AppDomainLoading) 806 m_Scene.RegionInfo.RegionID.ToString(),
737 sandbox = AppDomain.CreateDomain( 807 evidence, appSetup);
738 m_Scene.RegionInfo.RegionID.ToString(), 808 else
739 evidence, appSetup); 809 sandbox = AppDomain.CurrentDomain;
740 else 810
741 sandbox = AppDomain.CurrentDomain; 811 //PolicyLevel sandboxPolicy = PolicyLevel.CreateAppDomainLevel();
742 812 //AllMembershipCondition sandboxMembershipCondition = new AllMembershipCondition();
743 //PolicyLevel sandboxPolicy = PolicyLevel.CreateAppDomainLevel(); 813 //PermissionSet sandboxPermissionSet = sandboxPolicy.GetNamedPermissionSet("Internet");
744 //AllMembershipCondition sandboxMembershipCondition = new AllMembershipCondition(); 814 //PolicyStatement sandboxPolicyStatement = new PolicyStatement(sandboxPermissionSet);
745 //PermissionSet sandboxPermissionSet = sandboxPolicy.GetNamedPermissionSet("Internet"); 815 //CodeGroup sandboxCodeGroup = new UnionCodeGroup(sandboxMembershipCondition, sandboxPolicyStatement);
746 //PolicyStatement sandboxPolicyStatement = new PolicyStatement(sandboxPermissionSet); 816 //sandboxPolicy.RootCodeGroup = sandboxCodeGroup;
747 //CodeGroup sandboxCodeGroup = new UnionCodeGroup(sandboxMembershipCondition, sandboxPolicyStatement); 817 //sandbox.SetAppDomainPolicy(sandboxPolicy);
748 //sandboxPolicy.RootCodeGroup = sandboxCodeGroup; 818
749 //sandbox.SetAppDomainPolicy(sandboxPolicy); 819 m_AppDomains[appDomain] = sandbox;
750 820
751 m_AppDomains[appDomain] = sandbox; 821 m_AppDomains[appDomain].AssemblyResolve +=
752 822 new ResolveEventHandler(
753 m_AppDomains[appDomain].AssemblyResolve += 823 AssemblyResolver.OnAssemblyResolve);
754 new ResolveEventHandler( 824 m_DomainScripts[appDomain] = new List<UUID>();
755 AssemblyResolver.OnAssemblyResolve); 825 }
756 m_DomainScripts[appDomain] = new List<UUID>(); 826 catch (Exception e)
757 } 827 {
758 catch (Exception e) 828 m_log.ErrorFormat("[XEngine] Exception creating app domain:\n {0}", e.ToString());
829 m_ScriptErrorMessage += "Exception creating app domain:\n";
830 m_ScriptFailCount++;
831 lock (m_AddingAssemblies)
759 { 832 {
760 m_log.ErrorFormat("[XEngine] Exception creating app domain:\n {0}", e.ToString()); 833 m_AddingAssemblies[assembly]--;
761 m_ScriptErrorMessage += "Exception creating app domain:\n";
762 m_ScriptFailCount++;
763 lock (m_AddingAssemblies)
764 {
765 m_AddingAssemblies[assembly]--;
766 }
767 return false;
768 } 834 }
835 return false;
769 } 836 }
770 m_DomainScripts[appDomain].Add(itemID); 837 }
771 838 m_DomainScripts[appDomain].Add(itemID);
772 instance = new ScriptInstance(this, part, 839
773 itemID, assetID, assembly, 840 instance = new ScriptInstance(this, part,
774 m_AppDomains[appDomain], 841 itemID, assetID, assembly,
775 part.ParentGroup.RootPart.Name, 842 m_AppDomains[appDomain],
776 item.Name, startParam, postOnRez, 843 part.ParentGroup.RootPart.Name,
777 stateSource, m_MaxScriptQueue); 844 item.Name, startParam, postOnRez,
778 845 stateSource, m_MaxScriptQueue);
779 m_log.DebugFormat( 846
847 m_log.DebugFormat(
780 "[XEngine] Loaded script {0}.{1}, script UUID {2}, prim UUID {3} @ {4}.{5}", 848 "[XEngine] Loaded script {0}.{1}, script UUID {2}, prim UUID {3} @ {4}.{5}",
781 part.ParentGroup.RootPart.Name, item.Name, assetID, part.UUID, 849 part.ParentGroup.RootPart.Name, item.Name, assetID, part.UUID,
782 part.ParentGroup.RootPart.AbsolutePosition, part.ParentGroup.Scene.RegionInfo.RegionName); 850 part.ParentGroup.RootPart.AbsolutePosition, part.ParentGroup.Scene.RegionInfo.RegionName);
783 851
784 if (presence != null) 852 if (presence != null)
785 { 853 {
786 ShowScriptSaveResponse(item.OwnerID, 854 ShowScriptSaveResponse(item.OwnerID,
787 assetID, "Compile successful", true); 855 assetID, "Compile successful", true);
788 }
789
790 instance.AppDomain = appDomain;
791 instance.LineMap = linemap;
792
793 m_Scripts[itemID] = instance;
794 } 856 }
795 }
796 857
858 instance.AppDomain = appDomain;
859 instance.LineMap = linemap;
860 lockScriptsForWrite(true);
861 m_Scripts[itemID] = instance;
862 lockScriptsForWrite(false);
863 }
864 else
865 {
866 lockScriptsForRead(false);
867 }
797 lock (m_PrimObjects) 868 lock (m_PrimObjects)
798 { 869 {
799 if (!m_PrimObjects.ContainsKey(localID)) 870 if (!m_PrimObjects.ContainsKey(localID))
@@ -812,9 +883,9 @@ namespace OpenSim.Region.ScriptEngine.XEngine
812 m_AddingAssemblies[assembly]--; 883 m_AddingAssemblies[assembly]--;
813 } 884 }
814 885
815 if (instance != null) 886 if (instance!=null)
816 instance.Init(); 887 instance.Init();
817 888
818 return true; 889 return true;
819 } 890 }
820 891
@@ -827,20 +898,23 @@ namespace OpenSim.Region.ScriptEngine.XEngine
827 m_CompileDict.Remove(itemID); 898 m_CompileDict.Remove(itemID);
828 } 899 }
829 900
830 IScriptInstance instance = null; 901 lockScriptsForRead(true);
831 902 // Do we even have it?
832 lock (m_Scripts) 903 if (!m_Scripts.ContainsKey(itemID))
833 { 904 {
834 // Do we even have it? 905 lockScriptsForRead(false);
835 if (!m_Scripts.ContainsKey(itemID)) 906 return;
836 return;
837
838 instance=m_Scripts[itemID];
839 m_Scripts.Remove(itemID);
840 } 907 }
908
841 909
910 IScriptInstance instance=m_Scripts[itemID];
911 lockScriptsForRead(false);
912 lockScriptsForWrite(true);
913 m_Scripts.Remove(itemID);
914 lockScriptsForWrite(false);
842 instance.ClearQueue(); 915 instance.ClearQueue();
843 instance.Stop(0); 916 instance.Stop(0);
917
844// bool objectRemoved = false; 918// bool objectRemoved = false;
845 919
846 lock (m_PrimObjects) 920 lock (m_PrimObjects)
@@ -876,11 +950,12 @@ namespace OpenSim.Region.ScriptEngine.XEngine
876 ObjectRemoved handlerObjectRemoved = OnObjectRemoved; 950 ObjectRemoved handlerObjectRemoved = OnObjectRemoved;
877 if (handlerObjectRemoved != null) 951 if (handlerObjectRemoved != null)
878 { 952 {
879 SceneObjectPart part = m_Scene.GetSceneObjectPart(localID); 953 SceneObjectPart part = m_Scene.GetSceneObjectPart(localID);
880 handlerObjectRemoved(part.UUID); 954 handlerObjectRemoved(part.UUID);
881 } 955 }
882 956
883 957 CleanAssemblies();
958
884 ScriptRemoved handlerScriptRemoved = OnScriptRemoved; 959 ScriptRemoved handlerScriptRemoved = OnScriptRemoved;
885 if (handlerScriptRemoved != null) 960 if (handlerScriptRemoved != null)
886 handlerScriptRemoved(itemID); 961 handlerScriptRemoved(itemID);
@@ -1022,7 +1097,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1022 return false; 1097 return false;
1023 1098
1024 uuids = m_PrimObjects[localID]; 1099 uuids = m_PrimObjects[localID];
1025 } 1100
1026 1101
1027 foreach (UUID itemID in uuids) 1102 foreach (UUID itemID in uuids)
1028 { 1103 {
@@ -1040,6 +1115,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1040 result = true; 1115 result = true;
1041 } 1116 }
1042 } 1117 }
1118 }
1043 1119
1044 return result; 1120 return result;
1045 } 1121 }
@@ -1139,12 +1215,14 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1139 private IScriptInstance GetInstance(UUID itemID) 1215 private IScriptInstance GetInstance(UUID itemID)
1140 { 1216 {
1141 IScriptInstance instance; 1217 IScriptInstance instance;
1142 lock (m_Scripts) 1218 lockScriptsForRead(true);
1219 if (!m_Scripts.ContainsKey(itemID))
1143 { 1220 {
1144 if (!m_Scripts.ContainsKey(itemID)) 1221 lockScriptsForRead(false);
1145 return null; 1222 return null;
1146 instance = m_Scripts[itemID];
1147 } 1223 }
1224 instance = m_Scripts[itemID];
1225 lockScriptsForRead(false);
1148 return instance; 1226 return instance;
1149 } 1227 }
1150 1228
@@ -1168,6 +1246,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1168 return false; 1246 return false;
1169 } 1247 }
1170 1248
1249 [DebuggerNonUserCode]
1171 public void ApiResetScript(UUID itemID) 1250 public void ApiResetScript(UUID itemID)
1172 { 1251 {
1173 IScriptInstance instance = GetInstance(itemID); 1252 IScriptInstance instance = GetInstance(itemID);
@@ -1219,6 +1298,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1219 return UUID.Zero; 1298 return UUID.Zero;
1220 } 1299 }
1221 1300
1301 [DebuggerNonUserCode]
1222 public void SetState(UUID itemID, string newState) 1302 public void SetState(UUID itemID, string newState)
1223 { 1303 {
1224 IScriptInstance instance = GetInstance(itemID); 1304 IScriptInstance instance = GetInstance(itemID);
@@ -1239,11 +1319,10 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1239 { 1319 {
1240 List<IScriptInstance> instances = new List<IScriptInstance>(); 1320 List<IScriptInstance> instances = new List<IScriptInstance>();
1241 1321
1242 lock (m_Scripts) 1322 lockScriptsForRead(true);
1243 { 1323 foreach (IScriptInstance instance in m_Scripts.Values)
1244 foreach (IScriptInstance instance in m_Scripts.Values)
1245 instances.Add(instance); 1324 instances.Add(instance);
1246 } 1325 lockScriptsForRead(false);
1247 1326
1248 foreach (IScriptInstance i in instances) 1327 foreach (IScriptInstance i in instances)
1249 { 1328 {
@@ -1615,5 +1694,17 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1615 1694
1616 instance.Resume(); 1695 instance.Resume();
1617 } 1696 }
1697
1698 public bool HasScript(UUID itemID, out bool running)
1699 {
1700 running = true;
1701
1702 IScriptInstance instance = GetInstance(itemID);
1703 if (instance == null)
1704 return false;
1705
1706 running = instance.Running;
1707 return true;
1708 }
1618 } 1709 }
1619} 1710}