From ce34b359ad0254b6b4f460e2740e1a1ed30c456b Mon Sep 17 00:00:00 2001
From: Justin Clark-Casey (justincc)
Date: Fri, 3 Feb 2012 23:04:26 +0000
Subject: Extend m_avatars lock in NpcModule.CreateNPC over both creation of
 NPC scene presence and population of m_avatars.

This is required to stop a race where the SensorRepeat module can detect an NPC avatar before m_avatars is populated.
Extending the lock is the easiest to understand solution rather than getting complicated with null checks.
Hopefully resolves http://opensimulator.org/mantis/view.php?id=5872
---
 .../Region/OptionalModules/World/NPC/NPCModule.cs  | 30 ++++++++++++----------
 1 file changed, 16 insertions(+), 14 deletions(-)

(limited to 'OpenSim')

diff --git a/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs b/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs
index 3831d7a..6a48b89 100644
--- a/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs
+++ b/OpenSim/Region/OptionalModules/World/NPC/NPCModule.cs
@@ -140,24 +140,26 @@ namespace OpenSim.Region.OptionalModules.World.NPC
 //                    acd.AgentID, i, acd.Appearance.Texture.FaceTextures[i]);
 //            }
 
-            scene.AuthenticateHandler.AddNewCircuit(npcAvatar.CircuitCode, acd);
-            scene.AddNewClient(npcAvatar, PresenceType.Npc);
-
-            ScenePresence sp;
-            if (scene.TryGetScenePresence(npcAvatar.AgentId, out sp))
+            lock (m_avatars)
             {
-                m_log.DebugFormat(
-                    "[NPC MODULE]: Successfully retrieved scene presence for NPC {0} {1}", sp.Name, sp.UUID);
+                scene.AuthenticateHandler.AddNewCircuit(npcAvatar.CircuitCode, acd);
+                scene.AddNewClient(npcAvatar, PresenceType.Npc);
 
-                sp.CompleteMovement(npcAvatar, false);
-            }
-            else
-            {
-                m_log.WarnFormat("[NPC MODULE]: Could not find scene presence for NPC {0} {1}", sp.Name, sp.UUID);
-            }
+                ScenePresence sp;
+                if (scene.TryGetScenePresence(npcAvatar.AgentId, out sp))
+                {
+                    m_log.DebugFormat(
+                        "[NPC MODULE]: Successfully retrieved scene presence for NPC {0} {1}", sp.Name, sp.UUID);
+
+                    sp.CompleteMovement(npcAvatar, false);
+                }
+                else
+                {
+                    m_log.WarnFormat("[NPC MODULE]: Could not find scene presence for NPC {0} {1}", sp.Name, sp.UUID);
+                }
 
-            lock (m_avatars)
                 m_avatars.Add(npcAvatar.AgentId, npcAvatar);
+            }
 
             m_log.DebugFormat("[NPC MODULE]: Created NPC with id {0}", npcAvatar.AgentId);
 
-- 
cgit v1.1