From 9a5e0ede7c86b1fc213948332ae09a14a2d4729e Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Fri, 29 Jul 2011 23:21:57 +0100 Subject: For all Util.FireAndForget invocations, set thread to en_US before continuing wtih the invocation. This is to avoid bugs where the locale is not manually set on the thread and bad data values get sent to the database or over the wire. Lots of code does this manually but as we've seen, a subtle change can hit code which has forgotton to do this. Since en_US show be used throughout the server at present, setting it at FireAndForget seems reasonable. Arguably, it would be better to do this where data is sent, but doing it here is much easier. All the manual BeginInvokes() remaining in the code should probably call FireAndForget instead. --- OpenSim/Framework/Util.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'OpenSim/Framework/Util.cs') diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index fce8999..984a7a8 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -1499,25 +1499,30 @@ namespace OpenSim.Framework public static void FireAndForget(System.Threading.WaitCallback callback, object obj) { + // When OpenSim interacts with a database or sends data over the wire, it must send this in en_US culture + // so that we don't encounter problems where, for instance, data is saved with a culture that uses commas + // for decimals places but is read by a culture that treats commas as number seperators. + WaitCallback realCallback = delegate(object o) { Culture.SetCurrentCulture(); callback(o); }; + switch (FireAndForgetMethod) { case FireAndForgetMethod.UnsafeQueueUserWorkItem: - ThreadPool.UnsafeQueueUserWorkItem(callback, obj); + ThreadPool.UnsafeQueueUserWorkItem(realCallback, obj); break; case FireAndForgetMethod.QueueUserWorkItem: - ThreadPool.QueueUserWorkItem(callback, obj); + ThreadPool.QueueUserWorkItem(realCallback, obj); break; case FireAndForgetMethod.BeginInvoke: FireAndForgetWrapper wrapper = FireAndForgetWrapper.Instance; - wrapper.FireAndForget(callback, obj); + wrapper.FireAndForget(realCallback, obj); break; case FireAndForgetMethod.SmartThreadPool: if (m_ThreadPool == null) m_ThreadPool = new SmartThreadPool(2000, 15, 2); - m_ThreadPool.QueueWorkItem(SmartThreadPoolCallback, new object[] { callback, obj }); + m_ThreadPool.QueueWorkItem(SmartThreadPoolCallback, new object[] { realCallback, obj }); break; case FireAndForgetMethod.Thread: - Thread thread = new Thread(delegate(object o) { callback(o); }); + Thread thread = new Thread(delegate(object o) { realCallback(o); }); thread.Start(obj); break; default: -- cgit v1.1 From 57e54d84d641787d40a2b45549f6f2d373c5f2f2 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Tue, 16 Aug 2011 23:05:08 +0100 Subject: Add new FireAndForgetMethod.None. This executes the callback on the same thread that made the request. Designed for use only by regression tests that rely on a predicable event ordering. --- OpenSim/Framework/Util.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'OpenSim/Framework/Util.cs') diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index 984a7a8..51ced7b 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -56,8 +56,13 @@ namespace OpenSim.Framework /// /// The method used by Util.FireAndForget for asynchronously firing events /// + /// + /// None is used to execute the method in the same thread that made the call. It should only be used by regression + /// test code that relies on predictable event ordering. + /// public enum FireAndForgetMethod { + None, UnsafeQueueUserWorkItem, QueueUserWorkItem, BeginInvoke, @@ -89,7 +94,8 @@ namespace OpenSim.Framework public static readonly Regex UUIDPattern = new Regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); - public static FireAndForgetMethod FireAndForgetMethod = FireAndForgetMethod.SmartThreadPool; + public static FireAndForgetMethod DefaultFireAndForgetMethod = FireAndForgetMethod.SmartThreadPool; + public static FireAndForgetMethod FireAndForgetMethod = DefaultFireAndForgetMethod; /// /// Gets the name of the directory where the current running executable @@ -1506,6 +1512,9 @@ namespace OpenSim.Framework switch (FireAndForgetMethod) { + case FireAndForgetMethod.None: + realCallback.Invoke(obj); + break; case FireAndForgetMethod.UnsafeQueueUserWorkItem: ThreadPool.UnsafeQueueUserWorkItem(realCallback, obj); break; -- cgit v1.1 From 3aa86d22d16cbf82702024bdfc9846a4c3147232 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Wed, 31 Aug 2011 17:38:32 +0100 Subject: If a FireAndForget thread terminates with an exception, then catch and log rather than letting it terminate the simulator. Exceptions don't appear to do this with the SmartThreadPool but they do with UnsafeQueueUserWorkItem (and maybe others) --- OpenSim/Framework/Util.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'OpenSim/Framework/Util.cs') diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index 51ced7b..745da17 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -1508,7 +1508,21 @@ namespace OpenSim.Framework // When OpenSim interacts with a database or sends data over the wire, it must send this in en_US culture // so that we don't encounter problems where, for instance, data is saved with a culture that uses commas // for decimals places but is read by a culture that treats commas as number seperators. - WaitCallback realCallback = delegate(object o) { Culture.SetCurrentCulture(); callback(o); }; + WaitCallback realCallback = delegate(object o) + { + Culture.SetCurrentCulture(); + + try + { + callback(o); + } + catch (Exception e) + { + m_log.ErrorFormat( + "[UTIL]: Continuing after async_call_method thread terminated with exception {0}{1}", + e.Message, e.StackTrace); + } + }; switch (FireAndForgetMethod) { -- cgit v1.1