Lock on AgentCircuitData during Scene.AddClient() and RemoveClient() to prevent an inactive connection being left behind if the user closes the viewer whilst the connection is being established.

This should remove the need to run the console command "kick user --force" when these connections are left around.
0.7.3-extended
Justin Clark-Casey (justincc) 2012-10-10 00:26:43 +01:00
parent e54c11e0da
commit 2271986396
3 changed files with 260 additions and 228 deletions

View File

@ -94,7 +94,7 @@ namespace OpenSim.Region.ClientStack.Linden
//scene.CommsManager.HttpServer.AddLLSDHandler("/CAPS/EQG/", EventQueueFallBack); //scene.CommsManager.HttpServer.AddLLSDHandler("/CAPS/EQG/", EventQueueFallBack);
scene.EventManager.OnNewClient += OnNewClient; // scene.EventManager.OnNewClient += OnNewClient;
// TODO: Leaving these open, or closing them when we // TODO: Leaving these open, or closing them when we
// become a child is incorrect. It messes up TP in a big // become a child is incorrect. It messes up TP in a big
@ -102,6 +102,7 @@ namespace OpenSim.Region.ClientStack.Linden
// circuit is there. // circuit is there.
scene.EventManager.OnClientClosed += ClientClosed; scene.EventManager.OnClientClosed += ClientClosed;
scene.EventManager.OnMakeChildAgent += MakeChildAgent; scene.EventManager.OnMakeChildAgent += MakeChildAgent;
scene.EventManager.OnRegisterCaps += OnRegisterCaps; scene.EventManager.OnRegisterCaps += OnRegisterCaps;
@ -226,16 +227,6 @@ namespace OpenSim.Region.ClientStack.Linden
#endregion #endregion
private void OnNewClient(IClientAPI client)
{
//client.OnLogout += ClientClosed;
}
// private void ClientClosed(IClientAPI client)
// {
// ClientClosed(client.AgentId);
// }
private void ClientClosed(UUID agentID, Scene scene) private void ClientClosed(UUID agentID, Scene scene)
{ {
// m_log.DebugFormat("[EVENTQUEUE]: Closed client {0} in region {1}", agentID, m_scene.RegionInfo.RegionName); // m_log.DebugFormat("[EVENTQUEUE]: Closed client {0} in region {1}", agentID, m_scene.RegionInfo.RegionName);

View File

@ -1094,20 +1094,20 @@ namespace OpenSim.Region.ClientStack.LindenUDP
{ {
IClientAPI client = null; IClientAPI client = null;
// In priciple there shouldn't be more than one thread here, ever. // We currently synchronize this code across the whole scene to avoid issues such as
// But in case that happens, we need to synchronize this piece of code // http://opensimulator.org/mantis/view.php?id=5365 However, once locking per agent circuit can be done
// because it's too important // consistently, this lock could probably be removed.
lock (this) lock (this)
{ {
if (!m_scene.TryGetClient(agentID, out client)) if (!m_scene.TryGetClient(agentID, out client))
{ {
LLUDPClient udpClient = new LLUDPClient(this, ThrottleRates, m_throttle, circuitCode, agentID, remoteEndPoint, m_defaultRTO, m_maxRTO); LLUDPClient udpClient = new LLUDPClient(this, ThrottleRates, m_throttle, circuitCode, agentID, remoteEndPoint, m_defaultRTO, m_maxRTO);
client = new LLClientView(m_scene, this, udpClient, sessionInfo, agentID, sessionID, circuitCode); client = new LLClientView(m_scene, this, udpClient, sessionInfo, agentID, sessionID, circuitCode);
client.OnLogout += LogoutHandler; client.OnLogout += LogoutHandler;
((LLClientView)client).DisableFacelights = m_disableFacelights; ((LLClientView)client).DisableFacelights = m_disableFacelights;
client.Start(); client.Start();
} }
} }

View File

@ -78,6 +78,11 @@ namespace OpenSim.Region.Framework.Scenes
public SynchronizeSceneHandler SynchronizeScene; public SynchronizeSceneHandler SynchronizeScene;
/// <summary>
/// Used to prevent simultaneous calls to RemoveClient() for the same agent from interfering with each other.
/// </summary>
private object m_removeClientLock = new object();
/// <summary> /// <summary>
/// Statistical information for this scene. /// Statistical information for this scene.
/// </summary> /// </summary>
@ -2594,77 +2599,89 @@ namespace OpenSim.Region.Framework.Scenes
public override ISceneAgent AddNewClient(IClientAPI client, PresenceType type) public override ISceneAgent AddNewClient(IClientAPI client, PresenceType type)
{ {
ScenePresence sp;
bool vialogin;
// Validation occurs in LLUDPServer // Validation occurs in LLUDPServer
//
// XXX: A race condition exists here where two simultaneous calls to AddNewClient can interfere with
// each other. In practice, this does not currently occur in the code.
AgentCircuitData aCircuit = m_authenticateHandler.GetAgentCircuitData(client.CircuitCode); AgentCircuitData aCircuit = m_authenticateHandler.GetAgentCircuitData(client.CircuitCode);
bool vialogin // We lock here on AgentCircuitData to prevent a race condition between the thread adding a new connection
= (aCircuit.teleportFlags & (uint)Constants.TeleportFlags.ViaHGLogin) != 0 // and a simultaneous one that removes it (as can happen if the client is closed at a particular point
|| (aCircuit.teleportFlags & (uint)Constants.TeleportFlags.ViaLogin) != 0; // whilst connecting).
//
// CheckHeartbeat(); // It would be easier to lock across all NewUserConnection(), AddNewClient() and
// RemoveClient() calls for all agents, but this would allow a slow call (e.g. because of slow service
ScenePresence sp = GetScenePresence(client.AgentId); // response in some module listening to AddNewClient()) from holding up unrelated agent calls.
//
// XXX: Not sure how good it is to add a new client if a scene presence already exists. Possibly this // In practice, the lock (this) in LLUDPServer.AddNewClient() currently lock across all
// could occur if a viewer crashes and relogs before the old client is kicked out. But this could cause // AddNewClient() operations (though not other ops).
// other problems, and possible the code calling AddNewClient() should ensure that no client is already // In the future this can be relieved once locking per agent (not necessarily on AgentCircuitData) is improved.
// connected. lock (aCircuit)
if (sp == null)
{ {
m_log.DebugFormat( vialogin
"[SCENE]: Adding new child scene presence {0} {1} to scene {2} at pos {3}", = (aCircuit.teleportFlags & (uint)Constants.TeleportFlags.ViaHGLogin) != 0
client.Name, client.AgentId, RegionInfo.RegionName, client.StartPos); || (aCircuit.teleportFlags & (uint)Constants.TeleportFlags.ViaLogin) != 0;
// CheckHeartbeat();
sp = GetScenePresence(client.AgentId);
m_clientManager.Add(client); // XXX: Not sure how good it is to add a new client if a scene presence already exists. Possibly this
SubscribeToClientEvents(client); // could occur if a viewer crashes and relogs before the old client is kicked out. But this could cause
// other problems, and possible the code calling AddNewClient() should ensure that no client is already
sp = m_sceneGraph.CreateAndAddChildScenePresence(client, aCircuit.Appearance, type); // connected.
m_eventManager.TriggerOnNewPresence(sp); if (sp == null)
sp.TeleportFlags = (TPFlags)aCircuit.teleportFlags;
// The first agent upon login is a root agent by design.
// For this agent we will have to rez the attachments.
// All other AddNewClient calls find aCircuit.child to be true.
if (aCircuit.child == false)
{ {
// We have to set SP to be a root agent here so that SP.MakeRootAgent() will later not try to m_log.DebugFormat(
// start the scripts again (since this is done in RezAttachments()). "[SCENE]: Adding new child scene presence {0} {1} to scene {2} at pos {3}",
// XXX: This is convoluted. client.Name, client.AgentId, RegionInfo.RegionName, client.StartPos);
sp.IsChildAgent = false;
m_clientManager.Add(client);
if (AttachmentsModule != null) SubscribeToClientEvents(client);
Util.FireAndForget(delegate(object o) { AttachmentsModule.RezAttachments(sp); });
sp = m_sceneGraph.CreateAndAddChildScenePresence(client, aCircuit.Appearance, type);
m_eventManager.TriggerOnNewPresence(sp);
sp.TeleportFlags = (TPFlags)aCircuit.teleportFlags;
// The first agent upon login is a root agent by design.
// For this agent we will have to rez the attachments.
// All other AddNewClient calls find aCircuit.child to be true.
if (aCircuit.child == false)
{
// We have to set SP to be a root agent here so that SP.MakeRootAgent() will later not try to
// start the scripts again (since this is done in RezAttachments()).
// XXX: This is convoluted.
sp.IsChildAgent = false;
if (AttachmentsModule != null)
Util.FireAndForget(delegate(object o) { AttachmentsModule.RezAttachments(sp); });
}
} }
} else
else {
{ m_log.WarnFormat(
m_log.WarnFormat( "[SCENE]: Already found {0} scene presence for {1} in {2} when asked to add new scene presence",
"[SCENE]: Already found {0} scene presence for {1} in {2} when asked to add new scene presence", sp.IsChildAgent ? "child" : "root", sp.Name, RegionInfo.RegionName);
sp.IsChildAgent ? "child" : "root", sp.Name, RegionInfo.RegionName); }
}
// We must set this here so that TriggerOnNewClient and TriggerOnClientLogin can determine whether the
// client is for a root or child agent.
client.SceneAgent = sp;
// We must set this here so that TriggerOnNewClient and TriggerOnClientLogin can determine whether the // Cache the user's name
// client is for a root or child agent. CacheUserName(sp, aCircuit);
client.SceneAgent = sp;
EventManager.TriggerOnNewClient(client);
if (vialogin)
EventManager.TriggerOnClientLogin(client);
}
m_LastLogin = Util.EnvironmentTickCount(); m_LastLogin = Util.EnvironmentTickCount();
// Cache the user's name
CacheUserName(sp, aCircuit);
// Let's send the Suitcase folder for incoming HG agents
if ((aCircuit.teleportFlags & (uint)Constants.TeleportFlags.ViaHGLogin) != 0)
{
m_log.DebugFormat("[SCENE]: Sending root folder to viewer...");
InventoryFolderBase suitcase = InventoryService.GetRootFolder(client.AgentId);
client.SendBulkUpdateInventory(suitcase);
}
EventManager.TriggerOnNewClient(client);
if (vialogin)
EventManager.TriggerOnClientLogin(client);
return sp; return sp;
} }
@ -3195,69 +3212,88 @@ namespace OpenSim.Region.Framework.Scenes
{ {
// CheckHeartbeat(); // CheckHeartbeat();
bool isChildAgent = false; bool isChildAgent = false;
ScenePresence avatar = GetScenePresence(agentID); AgentCircuitData acd;
if (avatar == null) lock (m_removeClientLock)
{ {
m_log.WarnFormat( acd = m_authenticateHandler.GetAgentCircuitData(agentID);
"[SCENE]: Called RemoveClient() with agent ID {0} but no such presence is in the scene.", agentID);
return; if (acd == null)
{
m_log.ErrorFormat("[SCENE]: No agent circuit found for {0}, aborting Scene.RemoveClient", agentID);
return;
}
else
{
// We remove the acd up here to avoid later raec conditions if two RemoveClient() calls occurred
// simultaneously.
m_authenticateHandler.RemoveCircuit(acd.circuitcode);
}
} }
try lock (acd)
{ {
isChildAgent = avatar.IsChildAgent; ScenePresence avatar = GetScenePresence(agentID);
m_log.DebugFormat( if (avatar == null)
"[SCENE]: Removing {0} agent {1} {2} from {3}",
(isChildAgent ? "child" : "root"), avatar.Name, agentID, RegionInfo.RegionName);
// Don't do this to root agents, it's not nice for the viewer
if (closeChildAgents && isChildAgent)
{ {
// Tell a single agent to disconnect from the region. m_log.WarnFormat(
IEventQueue eq = RequestModuleInterface<IEventQueue>(); "[SCENE]: Called RemoveClient() with agent ID {0} but no such presence is in the scene.", agentID);
if (eq != null)
return;
}
try
{
isChildAgent = avatar.IsChildAgent;
m_log.DebugFormat(
"[SCENE]: Removing {0} agent {1} {2} from {3}",
(isChildAgent ? "child" : "root"), avatar.Name, agentID, RegionInfo.RegionName);
// Don't do this to root agents, it's not nice for the viewer
if (closeChildAgents && isChildAgent)
{ {
eq.DisableSimulator(RegionInfo.RegionHandle, avatar.UUID); // Tell a single agent to disconnect from the region.
IEventQueue eq = RequestModuleInterface<IEventQueue>();
if (eq != null)
{
eq.DisableSimulator(RegionInfo.RegionHandle, avatar.UUID);
}
else
{
avatar.ControllingClient.SendShutdownConnectionNotice();
}
} }
else
// Only applies to root agents.
if (avatar.ParentID != 0)
{ {
avatar.ControllingClient.SendShutdownConnectionNotice(); avatar.StandUp();
} }
}
m_sceneGraph.removeUserCount(!isChildAgent);
// Only applies to root agents.
if (avatar.ParentID != 0) // TODO: We shouldn't use closeChildAgents here - it's being used by the NPC module to stop
{ // unnecessary operations. This should go away once NPCs have no accompanying IClientAPI
avatar.StandUp(); if (closeChildAgents && CapsModule != null)
} CapsModule.RemoveCaps(agentID);
m_sceneGraph.removeUserCount(!isChildAgent); // REFACTORING PROBLEM -- well not really a problem, but just to point out that whatever
// this method is doing is HORRIBLE!!!
// TODO: We shouldn't use closeChildAgents here - it's being used by the NPC module to stop avatar.Scene.NeedSceneCacheClear(avatar.UUID);
// unnecessary operations. This should go away once NPCs have no accompanying IClientAPI
if (closeChildAgents && CapsModule != null) if (closeChildAgents && !isChildAgent)
CapsModule.RemoveCaps(agentID); {
List<ulong> regions = avatar.KnownRegionHandles;
// REFACTORING PROBLEM -- well not really a problem, but just to point out that whatever regions.Remove(RegionInfo.RegionHandle);
// this method is doing is HORRIBLE!!! m_sceneGridService.SendCloseChildAgentConnections(agentID, regions);
avatar.Scene.NeedSceneCacheClear(avatar.UUID); }
if (closeChildAgents && !isChildAgent) m_eventManager.TriggerClientClosed(agentID, this);
{ m_eventManager.TriggerOnRemovePresence(agentID);
List<ulong> regions = avatar.KnownRegionHandles;
regions.Remove(RegionInfo.RegionHandle); if (!isChildAgent)
m_sceneGridService.SendCloseChildAgentConnections(agentID, regions);
}
m_eventManager.TriggerClientClosed(agentID, this);
m_eventManager.TriggerOnRemovePresence(agentID);
if (!isChildAgent)
{
if (AttachmentsModule != null)
{ {
// Don't save attachments for HG visitors, it // Don't save attachments for HG visitors, it
// messes up their inventory. When a HG visitor logs // messes up their inventory. When a HG visitor logs
@ -3270,44 +3306,42 @@ namespace OpenSim.Region.Framework.Scenes
&& (UserManagementModule == null || UserManagementModule.IsLocalGridUser(avatar.UUID)); && (UserManagementModule == null || UserManagementModule.IsLocalGridUser(avatar.UUID));
AttachmentsModule.DeRezAttachments(avatar, saveChanged, false); AttachmentsModule.DeRezAttachments(avatar, saveChanged, false);
ForEachClient(
delegate(IClientAPI client)
{
//We can safely ignore null reference exceptions. It means the avatar is dead and cleaned up anyway
try { client.SendKillObject(avatar.RegionHandle, new List<uint> { avatar.LocalId }); }
catch (NullReferenceException) { }
});
} }
ForEachClient(
delegate(IClientAPI client)
{
//We can safely ignore null reference exceptions. It means the avatar is dead and cleaned up anyway
try { client.SendKillObject(avatar.RegionHandle, new List<uint> { avatar.LocalId }); }
catch (NullReferenceException) { }
});
}
// It's possible for child agents to have transactions if changes are being made cross-border.
if (AgentTransactionsModule != null)
AgentTransactionsModule.RemoveAgentAssetTransactions(agentID);
m_authenticateHandler.RemoveCircuit(avatar.ControllingClient.CircuitCode);
}
catch (Exception e)
{
m_log.Error(
string.Format("[SCENE]: Exception removing {0} from {1}. Cleaning up. Exception ", avatar.Name, Name), e);
}
finally
{
try
{
// Always clean these structures up so that any failure above doesn't cause them to remain in the
// scene with possibly bad effects (e.g. continually timing out on unacked packets and triggering
// the same cleanup exception continually.
m_sceneGraph.RemoveScenePresence(agentID);
m_clientManager.Remove(agentID);
avatar.Close(); // It's possible for child agents to have transactions if changes are being made cross-border.
if (AgentTransactionsModule != null)
AgentTransactionsModule.RemoveAgentAssetTransactions(agentID);
} }
catch (Exception e) catch (Exception e)
{ {
m_log.Error( m_log.Error(
string.Format("[SCENE]: Exception in final clean up of {0} in {1}. Exception ", avatar.Name, Name), e); string.Format("[SCENE]: Exception removing {0} from {1}. Cleaning up. Exception ", avatar.Name, Name), e);
}
finally
{
try
{
// Always clean these structures up so that any failure above doesn't cause them to remain in the
// scene with possibly bad effects (e.g. continually timing out on unacked packets and triggering
// the same cleanup exception continually.
m_sceneGraph.RemoveScenePresence(agentID);
m_clientManager.Remove(agentID);
avatar.Close();
}
catch (Exception e)
{
m_log.Error(
string.Format("[SCENE]: Exception in final clean up of {0} in {1}. Exception ", avatar.Name, Name), e);
}
} }
} }
@ -3419,88 +3453,95 @@ namespace OpenSim.Region.Framework.Scenes
return false; return false;
} }
ScenePresence sp = GetScenePresence(agent.AgentID); ILandObject land;
if (sp != null && !sp.IsChildAgent) lock (agent)
{ {
// We have a zombie from a crashed session. ScenePresence sp = GetScenePresence(agent.AgentID);
// Or the same user is trying to be root twice here, won't work.
// Kill it. if (sp != null && !sp.IsChildAgent)
m_log.WarnFormat(
"[SCENE]: Existing root scene presence detected for {0} {1} in {2} when connecting. Removing existing presence.",
sp.Name, sp.UUID, RegionInfo.RegionName);
sp.ControllingClient.Close();
sp = null;
}
ILandObject land = LandChannel.GetLandObject(agent.startpos.X, agent.startpos.Y);
//On login test land permisions
if (vialogin)
{
if (land != null && !TestLandRestrictions(agent, land, out reason))
{ {
return false; // We have a zombie from a crashed session.
// Or the same user is trying to be root twice here, won't work.
// Kill it.
m_log.WarnFormat(
"[SCENE]: Existing root scene presence detected for {0} {1} in {2} when connecting. Removing existing presence.",
sp.Name, sp.UUID, RegionInfo.RegionName);
sp.ControllingClient.Close(true);
sp = null;
} }
}
land = LandChannel.GetLandObject(agent.startpos.X, agent.startpos.Y);
if (sp == null) // We don't have an [child] agent here already
{ //On login test land permisions
if (requirePresenceLookup) if (vialogin)
{ {
if (land != null && !TestLandRestrictions(agent, land, out reason))
{
return false;
}
}
if (sp == null) // We don't have an [child] agent here already
{
if (requirePresenceLookup)
{
try
{
if (!VerifyUserPresence(agent, out reason))
return false;
}
catch (Exception e)
{
m_log.ErrorFormat(
"[SCENE]: Exception verifying presence {0}{1}", e.Message, e.StackTrace);
return false;
}
}
try try
{ {
if (!VerifyUserPresence(agent, out reason)) if (!AuthorizeUser(agent, out reason))
return false; return false;
} }
catch (Exception e) catch (Exception e)
{ {
m_log.ErrorFormat( m_log.ErrorFormat(
"[SCENE]: Exception verifying presence {0}{1}", e.Message, e.StackTrace); "[SCENE]: Exception authorizing user {0}{1}", e.Message, e.StackTrace);
return false; return false;
} }
}
m_log.InfoFormat(
try "[SCENE]: Region {0} authenticated and authorized incoming {1} agent {2} {3} {4} (circuit code {5})",
{ RegionInfo.RegionName, (agent.child ? "child" : "root"), agent.firstname, agent.lastname,
if (!AuthorizeUser(agent, out reason)) agent.AgentID, agent.circuitcode);
return false;
}
catch (Exception e)
{
m_log.ErrorFormat(
"[SCENE]: Exception authorizing user {0}{1}", e.Message, e.StackTrace);
return false;
}
m_log.InfoFormat(
"[SCENE]: Region {0} authenticated and authorized incoming {1} agent {2} {3} {4} (circuit code {5})",
RegionInfo.RegionName, (agent.child ? "child" : "root"), agent.firstname, agent.lastname,
agent.AgentID, agent.circuitcode);
if (CapsModule != null)
{
CapsModule.SetAgentCapsSeeds(agent);
CapsModule.CreateCaps(agent.AgentID);
}
}
else
{
// Let the SP know how we got here. This has a lot of interesting
// uses down the line.
sp.TeleportFlags = (TPFlags)teleportFlags;
if (sp.IsChildAgent)
{
m_log.DebugFormat(
"[SCENE]: Adjusting known seeds for existing agent {0} in {1}",
agent.AgentID, RegionInfo.RegionName);
sp.AdjustKnownSeeds();
if (CapsModule != null) if (CapsModule != null)
{
CapsModule.SetAgentCapsSeeds(agent); CapsModule.SetAgentCapsSeeds(agent);
CapsModule.CreateCaps(agent.AgentID);
}
}
else
{
// Let the SP know how we got here. This has a lot of interesting
// uses down the line.
sp.TeleportFlags = (TPFlags)teleportFlags;
if (sp.IsChildAgent)
{
m_log.DebugFormat(
"[SCENE]: Adjusting known seeds for existing agent {0} in {1}",
agent.AgentID, RegionInfo.RegionName);
sp.AdjustKnownSeeds();
if (CapsModule != null)
CapsModule.SetAgentCapsSeeds(agent);
}
} }
} }