Start locking entire add/remove operations on an IScenePresence.AttachmentsSyncLock object

Attach and detach packets are processed asynchronously when received from a viewer.
Bugs like http://opensimulator.org/mantis/view.php?id=5644 indicate that in some situations (such as attaching/detaching entire folders of objects at once), there are race conditions between these threads.
Since multiple data structures need to be updated on attach/detach, it's not enough to lock the individual collections.
Therefore, this commit introduces a new IScenePresence.AttachmentsSyncLock which add/remove operations lock on.
remove-scene-viewer
Justin Clark-Casey (justincc) 2011-09-12 21:57:22 +01:00
parent dab6387bba
commit ea0f78c971
4 changed files with 225 additions and 172 deletions

View File

@ -240,15 +240,17 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
private bool AttachObject(IScenePresence sp, SceneObjectGroup group, uint attachmentPt, bool silent)
{
// m_log.DebugFormat(
// "[ATTACHMENTS MODULE]: Attaching object {0} {1} to {2} point {3} from ground (silent = {4})",
// group.Name, group.LocalId, sp.Name, attachmentPt, silent);
lock (sp.AttachmentsSyncLock)
{
// m_log.DebugFormat(
// "[ATTACHMENTS MODULE]: Attaching object {0} {1} to {2} point {3} from ground (silent = {4})",
// group.Name, group.LocalId, sp.Name, attachmentPt, silent);
if (sp.GetAttachments(attachmentPt).Contains(group))
{
// m_log.WarnFormat(
// "[ATTACHMENTS MODULE]: Ignoring request to attach {0} {1} to {2} on {3} since it's already attached",
// group.Name, group.LocalId, sp.Name, AttachmentPt);
// m_log.WarnFormat(
// "[ATTACHMENTS MODULE]: Ignoring request to attach {0} {1} to {2} on {3} since it's already attached",
// group.Name, group.LocalId, sp.Name, AttachmentPt);
return false;
}
@ -313,6 +315,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
}
AttachToAgent(sp, group, attachmentPt, attachPos, silent);
}
return true;
}
@ -321,15 +324,27 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
IClientAPI remoteClient,
RezMultipleAttachmentsFromInvPacket.HeaderDataBlock header,
RezMultipleAttachmentsFromInvPacket.ObjectDataBlock[] objects)
{
ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
if (sp == null)
{
m_log.ErrorFormat(
"[ATTACHMENTS MODULE]: Could not find presence for client {0} {1} in RezMultipleAttachmentsFromInventory()",
remoteClient.Name, remoteClient.AgentId);
return;
}
lock (sp.AttachmentsSyncLock)
{
foreach (RezMultipleAttachmentsFromInvPacket.ObjectDataBlock obj in objects)
{
RezSingleAttachmentFromInventory(remoteClient, obj.ItemID, obj.AttachmentPt);
RezSingleAttachmentFromInventory(sp, obj.ItemID, obj.AttachmentPt);
}
}
}
public ISceneEntity RezSingleAttachmentFromInventory(
IClientAPI remoteClient, UUID itemID, uint AttachmentPt)
public ISceneEntity RezSingleAttachmentFromInventory(IClientAPI remoteClient, UUID itemID, uint AttachmentPt)
{
// m_log.DebugFormat(
// "[ATTACHMENTS MODULE]: Rezzing attachment to point {0} from item {1} for {2}",
@ -345,6 +360,11 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
return null;
}
return RezSingleAttachmentFromInventory(sp, itemID, AttachmentPt);
}
public ISceneEntity RezSingleAttachmentFromInventory(ScenePresence sp, UUID itemID, uint AttachmentPt)
{
// TODO: this short circuits multiple attachments functionality in LL viewer 2.1+ and should
// be removed when that functionality is implemented in opensim
AttachmentPt &= 0x7f;
@ -362,6 +382,8 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
{
IInventoryAccessModule invAccess = m_scene.RequestModuleInterface<IInventoryAccessModule>();
if (invAccess != null)
{
lock (sp.AttachmentsSyncLock)
{
SceneObjectGroup objatt;
@ -374,9 +396,9 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
null, assetID, Vector3.Zero, Vector3.Zero, UUID.Zero, (byte)1, true,
false, false, sp.UUID, true);
// m_log.DebugFormat(
// "[ATTACHMENTS MODULE]: Retrieved single object {0} for attachment to {1} on point {2}",
// objatt.Name, remoteClient.Name, AttachmentPt);
// m_log.DebugFormat(
// "[ATTACHMENTS MODULE]: Retrieved single object {0} for attachment to {1} on point {2}",
// objatt.Name, remoteClient.Name, AttachmentPt);
if (objatt != null)
{
@ -413,6 +435,8 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
// Do this last so that event listeners have access to all the effects of the attachment
m_scene.EventManager.TriggerOnAttach(objatt.LocalId, itemID, sp.UUID);
return objatt;
}
else
{
@ -420,8 +444,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
"[ATTACHMENTS MODULE]: Could not retrieve item {0} for attaching to avatar {1} at point {2}",
itemID, sp.Name, attachmentPt);
}
return objatt;
}
}
return null;
@ -473,6 +496,8 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
{
ScenePresence presence;
if (m_scene.TryGetScenePresence(remoteClient.AgentId, out presence))
{
lock (presence.AttachmentsSyncLock)
{
// Save avatar attachment information
m_log.Debug("[ATTACHMENTS MODULE]: Detaching from UserID: " + remoteClient.AgentId + ", ItemID: " + itemID);
@ -484,6 +509,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
DetachSingleAttachmentToInv(itemID, presence);
}
}
}
public void DetachSingleAttachmentToGround(uint soLocalId, IClientAPI remoteClient)
{
@ -507,6 +533,8 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
ScenePresence presence;
if (m_scene.TryGetScenePresence(remoteClient.AgentId, out presence))
{
lock (presence.AttachmentsSyncLock)
{
if (!m_scene.Permissions.CanRezObject(
so.PrimCount, remoteClient.AgentId, presence.AbsolutePosition))
@ -527,6 +555,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
m_scene.EventManager.TriggerOnAttach(so.LocalId, so.UUID, UUID.Zero);
}
}
/// <summary>
/// Detach the given scene object to the ground.
@ -567,6 +596,8 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
EntityBase[] detachEntities = m_scene.GetEntities();
SceneObjectGroup group;
lock (sp.AttachmentsSyncLock)
{
foreach (EntityBase entity in detachEntities)
{
if (entity is SceneObjectGroup)
@ -602,6 +633,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
}
}
}
}
public void UpdateAttachmentPosition(SceneObjectGroup sog, Vector3 pos)
{

View File

@ -87,6 +87,15 @@ namespace OpenSim.Region.Framework.Interfaces
/// <returns>The scene object that was attached. Null if the scene object could not be found</returns>
ISceneEntity RezSingleAttachmentFromInventory(IClientAPI remoteClient, UUID itemID, uint AttachmentPt);
/// <summary>
/// Rez an attachment from user inventory and change inventory status to match.
/// </summary>
/// <param name="sp"></param>
/// <param name="itemID"></param>
/// <param name="AttachmentPt"></param>
/// <returns>The scene object that was attached. Null if the scene object could not be found</returns>
ISceneEntity RezSingleAttachmentFromInventory(ScenePresence sp, UUID itemID, uint AttachmentPt);
/// <summary>
/// Rez multiple attachments from a user's inventory
/// </summary>

View File

@ -60,6 +60,14 @@ namespace OpenSim.Region.Framework.Interfaces
/// </remarks>
AvatarAppearance Appearance { get; set; }
/// <summary>
/// The AttachmentsModule synchronizes on this to avoid race conditions between commands to add and remove attachments.
/// </summary>
/// <remarks>
/// All add and remove attachment operations must synchronize on this for the lifetime of their operations.
/// </remarks>
Object AttachmentsSyncLock { get; }
/// <summary>
/// The scene objects attached to this avatar.
/// </summary>

View File

@ -120,6 +120,8 @@ namespace OpenSim.Region.Framework.Scenes
/// </remarks>
protected List<SceneObjectGroup> m_attachments = new List<SceneObjectGroup>();
public Object AttachmentsSyncLock { get; private set; }
private Dictionary<UUID, ScriptControllers> scriptedcontrols = new Dictionary<UUID, ScriptControllers>();
private ScriptControlled IgnoredControls = ScriptControlled.CONTROL_ZERO;
private ScriptControlled LastCommands = ScriptControlled.CONTROL_ZERO;
@ -709,6 +711,8 @@ namespace OpenSim.Region.Framework.Scenes
public ScenePresence(
IClientAPI client, Scene world, RegionInfo reginfo, AvatarAppearance appearance, PresenceType type)
{
AttachmentsSyncLock = new Object();
m_sendCourseLocationsMethod = SendCoarseLocationsDefault;
m_sceneViewer = new SceneViewer(this);
m_animator = new ScenePresenceAnimator(this);