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.cs422
1 files changed, 252 insertions, 170 deletions
diff --git a/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs b/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs
index 54074ed..463b052 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 =
@@ -123,6 +126,71 @@ namespace OpenSim.Region.ScriptEngine.XEngine
123 private ScriptCompileQueue m_CompileQueue = new ScriptCompileQueue(); 126 private ScriptCompileQueue m_CompileQueue = new ScriptCompileQueue();
124 IWorkItemResult m_CurrentCompile = null; 127 IWorkItemResult m_CurrentCompile = null;
125 128
129 private void lockScriptsForRead(bool locked)
130 {
131 if (locked)
132 {
133 if (m_scriptsLock.RecursiveReadCount > 0)
134 {
135 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.");
136 m_scriptsLock.ExitReadLock();
137 }
138 if (m_scriptsLock.RecursiveWriteCount > 0)
139 {
140 m_log.Error("[XEngine.m_Scripts] Recursive write lock requested. This should not happen and means something needs to be fixed.");
141 m_scriptsLock.ExitWriteLock();
142 }
143
144 while (!m_scriptsLock.TryEnterReadLock(60000))
145 {
146 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.");
147 if (m_scriptsLock.IsWriteLockHeld)
148 {
149 m_scriptsLock = new OpenMetaverse.ReaderWriterLockSlim();
150 }
151 }
152 }
153 else
154 {
155 if (m_scriptsLock.RecursiveReadCount > 0)
156 {
157 m_scriptsLock.ExitReadLock();
158 }
159 }
160 }
161 private void lockScriptsForWrite(bool locked)
162 {
163 if (locked)
164 {
165 if (m_scriptsLock.RecursiveReadCount > 0)
166 {
167 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.");
168 m_scriptsLock.ExitReadLock();
169 }
170 if (m_scriptsLock.RecursiveWriteCount > 0)
171 {
172 m_log.Error("[XEngine.m_Scripts] Recursive write lock requested. This should not happen and means something needs to be fixed.");
173 m_scriptsLock.ExitWriteLock();
174 }
175
176 while (!m_scriptsLock.TryEnterWriteLock(60000))
177 {
178 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.");
179 if (m_scriptsLock.IsWriteLockHeld)
180 {
181 m_scriptsLock = new OpenMetaverse.ReaderWriterLockSlim();
182 }
183 }
184 }
185 else
186 {
187 if (m_scriptsLock.RecursiveWriteCount > 0)
188 {
189 m_scriptsLock.ExitWriteLock();
190 }
191 }
192 }
193
126 public string ScriptEngineName 194 public string ScriptEngineName
127 { 195 {
128 get { return "XEngine"; } 196 get { return "XEngine"; }
@@ -262,43 +330,45 @@ namespace OpenSim.Region.ScriptEngine.XEngine
262 330
263 public void RemoveRegion(Scene scene) 331 public void RemoveRegion(Scene scene)
264 { 332 {
265 lock (m_Scripts) 333 lockScriptsForRead(true);
334 foreach (IScriptInstance instance in m_Scripts.Values)
266 { 335 {
267 foreach (IScriptInstance instance in m_Scripts.Values) 336 // Force a final state save
337 //
338 if (m_Assemblies.ContainsKey(instance.AssetID))
268 { 339 {
269 // Force a final state save 340 string assembly = m_Assemblies[instance.AssetID];
270 // 341 instance.SaveState(assembly);
271 if (m_Assemblies.ContainsKey(instance.AssetID)) 342 }
272 {
273 string assembly = m_Assemblies[instance.AssetID];
274 instance.SaveState(assembly);
275 }
276 343
277 // Clear the event queue and abort the instance thread 344 // Clear the event queue and abort the instance thread
278 // 345 //
279 instance.ClearQueue(); 346 instance.ClearQueue();
280 instance.Stop(0); 347 instance.Stop(0);
281 348
282 // Release events, timer, etc 349 // Release events, timer, etc
283 // 350 //
284 instance.DestroyScriptInstance(); 351 instance.DestroyScriptInstance();
285 352
286 // Unload scripts and app domains 353 // Unload scripts and app domains
287 // Must be done explicitly because they have infinite 354 // Must be done explicitly because they have infinite
288 // lifetime 355 // lifetime
289 // 356 //
290 m_DomainScripts[instance.AppDomain].Remove(instance.ItemID); 357 m_DomainScripts[instance.AppDomain].Remove(instance.ItemID);
291 if (m_DomainScripts[instance.AppDomain].Count == 0) 358 if (m_DomainScripts[instance.AppDomain].Count == 0)
292 { 359 {
293 m_DomainScripts.Remove(instance.AppDomain); 360 m_DomainScripts.Remove(instance.AppDomain);
294 UnloadAppDomain(instance.AppDomain); 361 UnloadAppDomain(instance.AppDomain);
295 }
296 } 362 }
297 m_Scripts.Clear();
298 m_PrimObjects.Clear();
299 m_Assemblies.Clear();
300 m_DomainScripts.Clear();
301 } 363 }
364 lockScriptsForRead(false);
365 lockScriptsForWrite(true);
366 m_Scripts.Clear();
367 lockScriptsForWrite(false);
368 m_PrimObjects.Clear();
369 m_Assemblies.Clear();
370 m_DomainScripts.Clear();
371
302 lock (m_ScriptEngines) 372 lock (m_ScriptEngines)
303 { 373 {
304 m_ScriptEngines.Remove(this); 374 m_ScriptEngines.Remove(this);
@@ -357,22 +427,20 @@ namespace OpenSim.Region.ScriptEngine.XEngine
357 427
358 List<IScriptInstance> instances = new List<IScriptInstance>(); 428 List<IScriptInstance> instances = new List<IScriptInstance>();
359 429
360 lock (m_Scripts) 430 lockScriptsForRead(true);
361 { 431 foreach (IScriptInstance instance in m_Scripts.Values)
362 foreach (IScriptInstance instance in m_Scripts.Values)
363 instances.Add(instance); 432 instances.Add(instance);
364 } 433 lockScriptsForRead(false);
365 434
366 foreach (IScriptInstance i in instances) 435 foreach (IScriptInstance i in instances)
367 { 436 {
368 string assembly = String.Empty; 437 string assembly = String.Empty;
369 438
370 lock (m_Scripts) 439
371 {
372 if (!m_Assemblies.ContainsKey(i.AssetID)) 440 if (!m_Assemblies.ContainsKey(i.AssetID))
373 continue; 441 continue;
374 assembly = m_Assemblies[i.AssetID]; 442 assembly = m_Assemblies[i.AssetID];
375 } 443
376 444
377 i.SaveState(assembly); 445 i.SaveState(assembly);
378 } 446 }
@@ -684,170 +752,181 @@ namespace OpenSim.Region.ScriptEngine.XEngine
684 } 752 }
685 } 753 }
686 754
687 lock (m_Scripts) 755
756
757 ScriptInstance instance = null;
758 // Create the object record
759 lockScriptsForRead(true);
760 if ((!m_Scripts.ContainsKey(itemID)) ||
761 (m_Scripts[itemID].AssetID != assetID))
688 { 762 {
689 ScriptInstance instance = null; 763 lockScriptsForRead(false);
690 // Create the object record
691 764
692 if ((!m_Scripts.ContainsKey(itemID)) || 765 UUID appDomain = assetID;
693 (m_Scripts[itemID].AssetID != assetID))
694 {
695 UUID appDomain = assetID;
696 766
697 if (part.ParentGroup.IsAttachment) 767 if (part.ParentGroup.IsAttachment)
698 appDomain = part.ParentGroup.RootPart.UUID; 768 appDomain = part.ParentGroup.RootPart.UUID;
699 769
700 if (!m_AppDomains.ContainsKey(appDomain)) 770 if (!m_AppDomains.ContainsKey(appDomain))
771 {
772 try
701 { 773 {
702 try 774 AppDomainSetup appSetup = new AppDomainSetup();
703 { 775 // appSetup.ApplicationBase = Path.Combine(
704 AppDomainSetup appSetup = new AppDomainSetup(); 776 // "ScriptEngines",
705// appSetup.ApplicationBase = Path.Combine( 777 // m_Scene.RegionInfo.RegionID.ToString());
706// "ScriptEngines", 778
707// m_Scene.RegionInfo.RegionID.ToString()); 779 Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
708 780 Evidence evidence = new Evidence(baseEvidence);
709 Evidence baseEvidence = AppDomain.CurrentDomain.Evidence; 781
710 Evidence evidence = new Evidence(baseEvidence); 782 AppDomain sandbox;
711 783 if (m_AppDomainLoading)
712 AppDomain sandbox; 784 sandbox = AppDomain.CreateDomain(
713 if (m_AppDomainLoading) 785 m_Scene.RegionInfo.RegionID.ToString(),
714 sandbox = AppDomain.CreateDomain( 786 evidence, appSetup);
715 m_Scene.RegionInfo.RegionID.ToString(), 787 else
716 evidence, appSetup); 788 sandbox = AppDomain.CurrentDomain;
717 else 789
718 sandbox = AppDomain.CurrentDomain; 790 //PolicyLevel sandboxPolicy = PolicyLevel.CreateAppDomainLevel();
719 791 //AllMembershipCondition sandboxMembershipCondition = new AllMembershipCondition();
720 //PolicyLevel sandboxPolicy = PolicyLevel.CreateAppDomainLevel(); 792 //PermissionSet sandboxPermissionSet = sandboxPolicy.GetNamedPermissionSet("Internet");
721 //AllMembershipCondition sandboxMembershipCondition = new AllMembershipCondition(); 793 //PolicyStatement sandboxPolicyStatement = new PolicyStatement(sandboxPermissionSet);
722 //PermissionSet sandboxPermissionSet = sandboxPolicy.GetNamedPermissionSet("Internet"); 794 //CodeGroup sandboxCodeGroup = new UnionCodeGroup(sandboxMembershipCondition, sandboxPolicyStatement);
723 //PolicyStatement sandboxPolicyStatement = new PolicyStatement(sandboxPermissionSet); 795 //sandboxPolicy.RootCodeGroup = sandboxCodeGroup;
724 //CodeGroup sandboxCodeGroup = new UnionCodeGroup(sandboxMembershipCondition, sandboxPolicyStatement); 796 //sandbox.SetAppDomainPolicy(sandboxPolicy);
725 //sandboxPolicy.RootCodeGroup = sandboxCodeGroup; 797
726 //sandbox.SetAppDomainPolicy(sandboxPolicy); 798 m_AppDomains[appDomain] = sandbox;
727 799
728 m_AppDomains[appDomain] = sandbox; 800 m_AppDomains[appDomain].AssemblyResolve +=
729 801 new ResolveEventHandler(
730 m_AppDomains[appDomain].AssemblyResolve += 802 AssemblyResolver.OnAssemblyResolve);
731 new ResolveEventHandler( 803 m_DomainScripts[appDomain] = new List<UUID>();
732 AssemblyResolver.OnAssemblyResolve);
733 m_DomainScripts[appDomain] = new List<UUID>();
734 }
735 catch (Exception e)
736 {
737 m_log.ErrorFormat("[XEngine] Exception creating app domain:\n {0}", e.ToString());
738 m_ScriptErrorMessage += "Exception creating app domain:\n";
739 m_ScriptFailCount++;
740 lock (m_AddingAssemblies)
741 {
742 m_AddingAssemblies[assembly]--;
743 }
744 return false;
745 }
746 } 804 }
747 m_DomainScripts[appDomain].Add(itemID); 805 catch (Exception e)
748
749 instance = new ScriptInstance(this, part,
750 itemID, assetID, assembly,
751 m_AppDomains[appDomain],
752 part.ParentGroup.RootPart.Name,
753 item.Name, startParam, postOnRez,
754 stateSource, m_MaxScriptQueue);
755
756 m_log.DebugFormat("[XEngine] Loaded script {0}.{1}, script UUID {2}, prim UUID {3} @ {4}",
757 part.ParentGroup.RootPart.Name, item.Name, assetID, part.UUID, part.ParentGroup.RootPart.AbsolutePosition.ToString());
758
759 if (presence != null)
760 { 806 {
761 ShowScriptSaveResponse(item.OwnerID, 807 m_log.ErrorFormat("[XEngine] Exception creating app domain:\n {0}", e.ToString());
762 assetID, "Compile successful", true); 808 m_ScriptErrorMessage += "Exception creating app domain:\n";
809 m_ScriptFailCount++;
810 lock (m_AddingAssemblies)
811 {
812 m_AddingAssemblies[assembly]--;
813 }
814 return false;
763 } 815 }
816 }
817 m_DomainScripts[appDomain].Add(itemID);
764 818
765 instance.AppDomain = appDomain; 819 instance = new ScriptInstance(this, part,
766 instance.LineMap = linemap; 820 itemID, assetID, assembly,
821 m_AppDomains[appDomain],
822 part.ParentGroup.RootPart.Name,
823 item.Name, startParam, postOnRez,
824 stateSource, m_MaxScriptQueue);
767 825
768 m_Scripts[itemID] = instance; 826 m_log.DebugFormat("[XEngine] Loaded script {0}.{1}, script UUID {2}, prim UUID {3} @ {4}",
769 } 827 part.ParentGroup.RootPart.Name, item.Name, assetID, part.UUID, part.ParentGroup.RootPart.AbsolutePosition.ToString());
770 828
771 lock (m_PrimObjects) 829 if (presence != null)
772 { 830 {
773 if (!m_PrimObjects.ContainsKey(localID)) 831 ShowScriptSaveResponse(item.OwnerID,
774 m_PrimObjects[localID] = new List<UUID>(); 832 assetID, "Compile successful", true);
833 }
775 834
776 if (!m_PrimObjects[localID].Contains(itemID)) 835 instance.AppDomain = appDomain;
777 m_PrimObjects[localID].Add(itemID); 836 instance.LineMap = linemap;
837 lockScriptsForWrite(true);
838 m_Scripts[itemID] = instance;
839 lockScriptsForWrite(false);
840 }
841 else
842 {
843 lockScriptsForRead(false);
844 }
845 lock (m_PrimObjects)
846 {
847 if (!m_PrimObjects.ContainsKey(localID))
848 m_PrimObjects[localID] = new List<UUID>();
778 849
779 } 850 if (!m_PrimObjects[localID].Contains(itemID))
851 m_PrimObjects[localID].Add(itemID);
780 852
781 if (!m_Assemblies.ContainsKey(assetID)) 853 }
782 m_Assemblies[assetID] = assembly;
783 854
784 lock (m_AddingAssemblies) 855 if (!m_Assemblies.ContainsKey(assetID))
785 { 856 m_Assemblies[assetID] = assembly;
786 m_AddingAssemblies[assembly]--;
787 }
788 857
789 if (instance!=null) 858 lock (m_AddingAssemblies)
790 instance.Init(); 859 {
860 m_AddingAssemblies[assembly]--;
791 } 861 }
862
863 if (instance!=null)
864 instance.Init();
865
792 return true; 866 return true;
793 } 867 }
794 868
795 public void OnRemoveScript(uint localID, UUID itemID) 869 public void OnRemoveScript(uint localID, UUID itemID)
796 { 870 {
797 lock (m_Scripts) 871 lockScriptsForRead(true);
872 // Do we even have it?
873 if (!m_Scripts.ContainsKey(itemID))
798 { 874 {
799 // Do we even have it? 875 lockScriptsForRead(false);
800 if (!m_Scripts.ContainsKey(itemID)) 876 return;
801 return; 877 }
802 878
803 IScriptInstance instance=m_Scripts[itemID];
804 m_Scripts.Remove(itemID);
805 879
806 instance.ClearQueue(); 880 IScriptInstance instance=m_Scripts[itemID];
807 instance.Stop(0); 881 lockScriptsForRead(false);
882 lockScriptsForWrite(true);
883 m_Scripts.Remove(itemID);
884 lockScriptsForWrite(false);
885 instance.ClearQueue();
886 instance.Stop(0);
808 887
809// bool objectRemoved = false; 888// bool objectRemoved = false;
810 889
811 lock (m_PrimObjects) 890 lock (m_PrimObjects)
891 {
892 // Remove the script from it's prim
893 if (m_PrimObjects.ContainsKey(localID))
812 { 894 {
813 // Remove the script from it's prim 895 // Remove inventory item record
814 if (m_PrimObjects.ContainsKey(localID)) 896 if (m_PrimObjects[localID].Contains(itemID))
815 { 897 m_PrimObjects[localID].Remove(itemID);
816 // Remove inventory item record
817 if (m_PrimObjects[localID].Contains(itemID))
818 m_PrimObjects[localID].Remove(itemID);
819 898
820 // If there are no more scripts, remove prim 899 // If there are no more scripts, remove prim
821 if (m_PrimObjects[localID].Count == 0) 900 if (m_PrimObjects[localID].Count == 0)
822 { 901 {
823 m_PrimObjects.Remove(localID); 902 m_PrimObjects.Remove(localID);
824// objectRemoved = true; 903// objectRemoved = true;
825 }
826 } 904 }
827 } 905 }
906 }
828 907
829 instance.RemoveState(); 908 instance.RemoveState();
830 instance.DestroyScriptInstance(); 909 instance.DestroyScriptInstance();
831
832 m_DomainScripts[instance.AppDomain].Remove(instance.ItemID);
833 if (m_DomainScripts[instance.AppDomain].Count == 0)
834 {
835 m_DomainScripts.Remove(instance.AppDomain);
836 UnloadAppDomain(instance.AppDomain);
837 }
838 910
839 instance = null; 911 m_DomainScripts[instance.AppDomain].Remove(instance.ItemID);
912 if (m_DomainScripts[instance.AppDomain].Count == 0)
913 {
914 m_DomainScripts.Remove(instance.AppDomain);
915 UnloadAppDomain(instance.AppDomain);
916 }
840 917
841 ObjectRemoved handlerObjectRemoved = OnObjectRemoved; 918 instance = null;
842 if (handlerObjectRemoved != null)
843 {
844 SceneObjectPart part = m_Scene.GetSceneObjectPart(localID);
845 handlerObjectRemoved(part.UUID);
846 }
847 919
848 CleanAssemblies(); 920 ObjectRemoved handlerObjectRemoved = OnObjectRemoved;
921 if (handlerObjectRemoved != null)
922 {
923 SceneObjectPart part = m_Scene.GetSceneObjectPart(localID);
924 handlerObjectRemoved(part.UUID);
849 } 925 }
850 926
927 CleanAssemblies();
928
929
851 ScriptRemoved handlerScriptRemoved = OnScriptRemoved; 930 ScriptRemoved handlerScriptRemoved = OnScriptRemoved;
852 if (handlerScriptRemoved != null) 931 if (handlerScriptRemoved != null)
853 handlerScriptRemoved(itemID); 932 handlerScriptRemoved(itemID);
@@ -1099,12 +1178,14 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1099 private IScriptInstance GetInstance(UUID itemID) 1178 private IScriptInstance GetInstance(UUID itemID)
1100 { 1179 {
1101 IScriptInstance instance; 1180 IScriptInstance instance;
1102 lock (m_Scripts) 1181 lockScriptsForRead(true);
1182 if (!m_Scripts.ContainsKey(itemID))
1103 { 1183 {
1104 if (!m_Scripts.ContainsKey(itemID)) 1184 lockScriptsForRead(false);
1105 return null; 1185 return null;
1106 instance = m_Scripts[itemID];
1107 } 1186 }
1187 instance = m_Scripts[itemID];
1188 lockScriptsForRead(false);
1108 return instance; 1189 return instance;
1109 } 1190 }
1110 1191
@@ -1128,6 +1209,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1128 return false; 1209 return false;
1129 } 1210 }
1130 1211
1212 [DebuggerNonUserCode]
1131 public void ApiResetScript(UUID itemID) 1213 public void ApiResetScript(UUID itemID)
1132 { 1214 {
1133 IScriptInstance instance = GetInstance(itemID); 1215 IScriptInstance instance = GetInstance(itemID);
@@ -1179,6 +1261,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1179 return UUID.Zero; 1261 return UUID.Zero;
1180 } 1262 }
1181 1263
1264 [DebuggerNonUserCode]
1182 public void SetState(UUID itemID, string newState) 1265 public void SetState(UUID itemID, string newState)
1183 { 1266 {
1184 IScriptInstance instance = GetInstance(itemID); 1267 IScriptInstance instance = GetInstance(itemID);
@@ -1199,11 +1282,10 @@ namespace OpenSim.Region.ScriptEngine.XEngine
1199 { 1282 {
1200 List<IScriptInstance> instances = new List<IScriptInstance>(); 1283 List<IScriptInstance> instances = new List<IScriptInstance>();
1201 1284
1202 lock (m_Scripts) 1285 lockScriptsForRead(true);
1203 { 1286 foreach (IScriptInstance instance in m_Scripts.Values)
1204 foreach (IScriptInstance instance in m_Scripts.Values)
1205 instances.Add(instance); 1287 instances.Add(instance);
1206 } 1288 lockScriptsForRead(false);
1207 1289
1208 foreach (IScriptInstance i in instances) 1290 foreach (IScriptInstance i in instances)
1209 { 1291 {