From e6ddb5de7de4ed61fa19b3805cd77104328eb20a Mon Sep 17 00:00:00 2001 From: Tedd Hansen Date: Wed, 26 Nov 2008 11:12:57 +0000 Subject: Attempt to give script objects a proper lease time (DNE and xengine). Relies on GC. Also removed lease for LSL_Api as it strictly speaking should not be MarshalByRef. Or should it? If so I broke scripting! :) --- .../Region/ScriptEngine/DotNetEngine/ScriptManager.cs | 9 +++++++++ .../ScriptEngine/Shared/Api/Implementation/LSL_Api.cs | 14 +++----------- .../ScriptEngine/Shared/Api/Runtime/ScriptBase.cs | 13 ++++++++----- .../ScriptEngine/Shared/Api/Runtime/ScriptSponsor.cs | 17 +++++++++++++++++ .../ScriptEngine/Shared/Instance/ScriptInstance.cs | 11 +++++++++++ 5 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptSponsor.cs diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs index bd60759..b5cbd25 100644 --- a/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs +++ b/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptManager.cs @@ -28,6 +28,8 @@ using System; using System.Reflection; using System.Globalization; +using System.Runtime.Remoting; +using System.Runtime.Remoting.Lifetime; using log4net; using OpenMetaverse; using OpenSim.Framework; @@ -39,6 +41,7 @@ using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Threading; +using OpenSim.Region.ScriptEngine.Shared.Api.Runtime; using OpenSim.Region.ScriptEngine.Shared.ScriptBase; using OpenSim.Region.ScriptEngine.Shared.CodeTools; @@ -56,6 +59,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine public Dictionary Apis; public Dictionary, KeyValuePair> LineMap; + public ISponsor ScriptSponsor; } public class ScriptManager @@ -164,6 +168,11 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine CompiledScript = m_scriptEngine.m_AppDomainManager.LoadScript( CompiledScriptFile, out id.AppDomain); + //Register the sponsor + ISponsor scriptSponsor = new ScriptSponsor(); + ILease lease = (ILease)RemotingServices.GetLifetimeService(CompiledScript as MarshalByRefObject); + lease.Register(scriptSponsor); + id.ScriptSponsor = scriptSponsor; id.LineMap = LSLCompiler.LineMap(); id.Script = CompiledScript; diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs index 17dca98..cbd99bb 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs @@ -29,6 +29,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Runtime.Remoting.Lifetime; +using System.Security.Permissions; using System.Text; using System.Threading; using Nini.Config; @@ -64,7 +65,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api /// /// Contains all LSL ll-functions. This class will be in Default AppDomain. /// - public class LSL_Api : MarshalByRefObject, ILSL_Api, IScriptApi + public class LSL_Api : ILSL_Api, IScriptApi { protected IScriptEngine m_ScriptEngine; protected SceneObjectPart m_host; @@ -98,17 +99,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api AsyncCommands = new AsyncCommandManager(ScriptEngine); } - // Object never expires - public override Object InitializeLifetimeService() - { - ILease lease = (ILease)base.InitializeLifetimeService(); - if (lease.CurrentState == LeaseState.Initial) - { - lease.InitialLeaseTime = TimeSpan.Zero; - } - return lease; - } + protected void ScriptSleep(int delay) { diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptBase.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptBase.cs index 25c823d..06307bb 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptBase.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptBase.cs @@ -27,6 +27,7 @@ using System; using System.Runtime.Remoting.Lifetime; +using System.Security.Permissions; using System.Threading; using System.Reflection; using System.Collections; @@ -40,16 +41,18 @@ namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase { private Dictionary inits = new Dictionary(); - // - // Never expire this object - // + // Object expires if we don't keep it alive + // sponsor will be added on object load + [SecurityPermissionAttribute(SecurityAction.Demand, + Flags = SecurityPermissionFlag.Infrastructure)] public override Object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); - if (lease.CurrentState == LeaseState.Initial) { - lease.InitialLeaseTime = TimeSpan.Zero; + lease.InitialLeaseTime = TimeSpan.FromMinutes(1); + lease.SponsorshipTimeout = TimeSpan.FromMinutes(2); + lease.RenewOnCallTime = TimeSpan.FromSeconds(2); } return lease; } diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptSponsor.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptSponsor.cs new file mode 100644 index 0000000..87401bb --- /dev/null +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/ScriptSponsor.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Runtime.Remoting.Lifetime; +using System.Text; + +namespace OpenSim.Region.ScriptEngine.Shared.Api.Runtime +{ + public class ScriptSponsor: ISponsor + { + // In theory: I execute, therefore I am. + // If GC collects this class then sponsorship will expire + public TimeSpan Renewal(ILease lease) + { + return TimeSpan.FromMinutes(2); + } + } +} diff --git a/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs b/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs index ad86494..2a8bba8 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs @@ -27,6 +27,8 @@ using System; using System.IO; +using System.Runtime.Remoting; +using System.Runtime.Remoting.Lifetime; using System.Threading; using System.Collections; using System.Collections.Generic; @@ -44,6 +46,7 @@ using OpenSim.Region.Environment.Scenes; using OpenSim.Region.Environment.Interfaces; using OpenSim.Region.ScriptEngine.Shared; using OpenSim.Region.ScriptEngine.Shared.Api; +using OpenSim.Region.ScriptEngine.Shared.Api.Runtime; using OpenSim.Region.ScriptEngine.Shared.ScriptBase; using OpenSim.Region.ScriptEngine.Shared.CodeTools; using OpenSim.Region.ScriptEngine.Interfaces; @@ -80,6 +83,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance private int m_ControlEventsInQueue = 0; private int m_LastControlLevel = 0; private bool m_CollisionInQueue = false; + private ISponsor m_ScriptSponsor; private Dictionary, KeyValuePair> m_LineMap; @@ -204,6 +208,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance m_Script = (IScript)dom.CreateInstanceAndUnwrap( Path.GetFileNameWithoutExtension(assembly), "SecondLife.Script"); + + // Add a sponsor to the script + ISponsor scriptSponsor = new ScriptSponsor(); + ILease lease = (ILease)RemotingServices.GetLifetimeService(m_Script as MarshalByRefObject); + lease.Register(scriptSponsor); + m_ScriptSponsor = scriptSponsor; + } catch (Exception e) { -- cgit v1.1