Get attachment script state before taking sp.AttachmentsSyncLock() to avoid race conditions between closing agents and scripts that may be doing attachment manipulation.
This is in an effort to resolve http://opensimulator.org/mantis/view.php?id=6557user_profiles
parent
fa9f4ef1ba
commit
ccd6f443e1
|
@ -241,12 +241,27 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
|
|
||||||
// m_log.DebugFormat("[ATTACHMENTS MODULE]: Saving changed attachments for {0}", sp.Name);
|
// m_log.DebugFormat("[ATTACHMENTS MODULE]: Saving changed attachments for {0}", sp.Name);
|
||||||
|
|
||||||
|
List<SceneObjectGroup> attachments = sp.GetAttachments();
|
||||||
|
|
||||||
|
if (attachments.Count <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Dictionary<SceneObjectGroup, string> scriptStates = new Dictionary<SceneObjectGroup, string>();
|
||||||
|
|
||||||
|
foreach (SceneObjectGroup so in attachments)
|
||||||
|
{
|
||||||
|
// Scripts MUST be snapshotted before the object is
|
||||||
|
// removed from the scene because doing otherwise will
|
||||||
|
// clobber the run flag
|
||||||
|
// This must be done outside the sp.AttachmentSyncLock so that there is no risk of a deadlock from
|
||||||
|
// scripts performing attachment operations at the same time. Getting object states stops the scripts.
|
||||||
|
scriptStates[so] = PrepareScriptInstanceForSave(so, false);
|
||||||
|
}
|
||||||
|
|
||||||
lock (sp.AttachmentsSyncLock)
|
lock (sp.AttachmentsSyncLock)
|
||||||
{
|
{
|
||||||
foreach (SceneObjectGroup so in sp.GetAttachments())
|
foreach (SceneObjectGroup so in attachments)
|
||||||
{
|
UpdateDetachedObject(sp, so, scriptStates[so]);
|
||||||
UpdateDetachedObject(sp, so);
|
|
||||||
}
|
|
||||||
|
|
||||||
sp.ClearAttachments();
|
sp.ClearAttachments();
|
||||||
}
|
}
|
||||||
|
@ -285,8 +300,6 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
|
|
||||||
private bool AttachObjectInternal(IScenePresence sp, SceneObjectGroup group, uint attachmentPt, bool silent, bool temp)
|
private bool AttachObjectInternal(IScenePresence sp, SceneObjectGroup group, uint attachmentPt, bool silent, bool temp)
|
||||||
{
|
{
|
||||||
lock (sp.AttachmentsSyncLock)
|
|
||||||
{
|
|
||||||
// m_log.DebugFormat(
|
// m_log.DebugFormat(
|
||||||
// "[ATTACHMENTS MODULE]: Attaching object {0} {1} to {2} point {3} from ground (silent = {4})",
|
// "[ATTACHMENTS MODULE]: Attaching object {0} {1} to {2} point {3} from ground (silent = {4})",
|
||||||
// group.Name, group.LocalId, sp.Name, attachmentPt, silent);
|
// group.Name, group.LocalId, sp.Name, attachmentPt, silent);
|
||||||
|
@ -302,13 +315,23 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
|
|
||||||
if (sp.GetAttachments(attachmentPt).Contains(group))
|
if (sp.GetAttachments(attachmentPt).Contains(group))
|
||||||
{
|
{
|
||||||
// m_log.WarnFormat(
|
// m_log.WarnFormat(
|
||||||
// "[ATTACHMENTS MODULE]: Ignoring request to attach {0} {1} to {2} on {3} since it's already attached",
|
// "[ATTACHMENTS MODULE]: Ignoring request to attach {0} {1} to {2} on {3} since it's already attached",
|
||||||
// group.Name, group.LocalId, sp.Name, AttachmentPt);
|
// group.Name, group.LocalId, sp.Name, AttachmentPt);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove any previous attachments
|
||||||
|
List<SceneObjectGroup> existingAttachments = sp.GetAttachments(attachmentPt);
|
||||||
|
string existingAttachmentScriptState = null;
|
||||||
|
|
||||||
|
// At the moment we can only deal with a single attachment
|
||||||
|
if (existingAttachments.Count != 0 && existingAttachments[0].FromItemID != UUID.Zero)
|
||||||
|
DetachSingleAttachmentToInv(sp, group);
|
||||||
|
|
||||||
|
lock (sp.AttachmentsSyncLock)
|
||||||
|
{
|
||||||
Vector3 attachPos = group.AbsolutePosition;
|
Vector3 attachPos = group.AbsolutePosition;
|
||||||
|
|
||||||
// TODO: this short circuits multiple attachments functionality in LL viewer 2.1+ and should
|
// TODO: this short circuits multiple attachments functionality in LL viewer 2.1+ and should
|
||||||
|
@ -352,21 +375,6 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
|
|
||||||
private void UpdateUserInventoryWithAttachment(IScenePresence sp, SceneObjectGroup group, uint attachmentPt, bool temp)
|
private void UpdateUserInventoryWithAttachment(IScenePresence sp, SceneObjectGroup group, uint attachmentPt, bool temp)
|
||||||
{
|
{
|
||||||
// Remove any previous attachments
|
|
||||||
List<SceneObjectGroup> attachments = sp.GetAttachments(attachmentPt);
|
|
||||||
|
|
||||||
// At the moment we can only deal with a single attachment
|
|
||||||
if (attachments.Count != 0)
|
|
||||||
{
|
|
||||||
if (attachments[0].FromItemID != UUID.Zero)
|
|
||||||
DetachSingleAttachmentToInvInternal(sp, attachments[0]);
|
|
||||||
// Error logging commented because UUID.Zero now means temp attachment
|
|
||||||
// else
|
|
||||||
// m_log.WarnFormat(
|
|
||||||
// "[ATTACHMENTS MODULE]: When detaching existing attachment {0} {1} at point {2} to make way for {3} {4} for {5}, couldn't find the associated item ID to adjust inventory attachment record!",
|
|
||||||
// attachments[0].Name, attachments[0].LocalId, attachmentPt, group.Name, group.LocalId, sp.Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the new attachment to inventory if we don't already have it.
|
// Add the new attachment to inventory if we don't already have it.
|
||||||
if (!temp)
|
if (!temp)
|
||||||
{
|
{
|
||||||
|
@ -426,14 +434,12 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// m_log.DebugFormat("[ATTACHMENTS MODULE]: Rezzing multiple attachments from inventory for {0}", sp.Name);
|
// m_log.DebugFormat("[ATTACHMENTS MODULE]: Rezzing multiple attachments from inventory for {0}", sp.Name);
|
||||||
lock (sp.AttachmentsSyncLock)
|
|
||||||
{
|
|
||||||
foreach (KeyValuePair<UUID, uint> rez in rezlist)
|
foreach (KeyValuePair<UUID, uint> rez in rezlist)
|
||||||
{
|
{
|
||||||
RezSingleAttachmentFromInventory(sp, rez.Key, rez.Value);
|
RezSingleAttachmentFromInventory(sp, rez.Key, rez.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void DetachSingleAttachmentToGround(IScenePresence sp, uint soLocalId)
|
public void DetachSingleAttachmentToGround(IScenePresence sp, uint soLocalId)
|
||||||
{
|
{
|
||||||
|
@ -511,11 +517,6 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
|
|
||||||
public void DetachSingleAttachmentToInv(IScenePresence sp, SceneObjectGroup so)
|
public void DetachSingleAttachmentToInv(IScenePresence sp, SceneObjectGroup so)
|
||||||
{
|
{
|
||||||
lock (sp.AttachmentsSyncLock)
|
|
||||||
{
|
|
||||||
// Save avatar attachment information
|
|
||||||
// m_log.Debug("[ATTACHMENTS MODULE]: Detaching from UserID: " + sp.UUID + ", ItemID: " + itemID);
|
|
||||||
|
|
||||||
if (so.AttachedAvatar != sp.UUID)
|
if (so.AttachedAvatar != sp.UUID)
|
||||||
{
|
{
|
||||||
m_log.WarnFormat(
|
m_log.WarnFormat(
|
||||||
|
@ -525,11 +526,24 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Scripts MUST be snapshotted before the object is
|
||||||
|
// removed from the scene because doing otherwise will
|
||||||
|
// clobber the run flag
|
||||||
|
// This must be done outside the sp.AttachmentSyncLock so that there is no risk of a deadlock from
|
||||||
|
// scripts performing attachment operations at the same time. Getting object states stops the scripts.
|
||||||
|
string scriptedState = PrepareScriptInstanceForSave(so, true);
|
||||||
|
|
||||||
|
lock (sp.AttachmentsSyncLock)
|
||||||
|
{
|
||||||
|
// Save avatar attachment information
|
||||||
|
// m_log.Debug("[ATTACHMENTS MODULE]: Detaching from UserID: " + sp.UUID + ", ItemID: " + itemID);
|
||||||
|
|
||||||
bool changed = sp.Appearance.DetachAttachment(so.FromItemID);
|
bool changed = sp.Appearance.DetachAttachment(so.FromItemID);
|
||||||
if (changed && m_scene.AvatarFactory != null)
|
if (changed && m_scene.AvatarFactory != null)
|
||||||
m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
|
m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
|
||||||
|
|
||||||
DetachSingleAttachmentToInvInternal(sp, so);
|
sp.RemoveAttachment(so);
|
||||||
|
UpdateDetachedObject(sp, so, scriptedState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -739,8 +753,27 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
return newItem;
|
return newItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetObjectScriptStates(SceneObjectGroup grp)
|
/// <summary>
|
||||||
|
/// Prepares the script instance for save.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This involves triggering the detach event and getting the script state (which also stops the script)
|
||||||
|
/// This MUST be done outside sp.AttachmentsSyncLock, since otherwise there is a chance of deadlock if a
|
||||||
|
/// running script is performing attachment operations.
|
||||||
|
/// </remarks>
|
||||||
|
/// <returns>
|
||||||
|
/// The script state ready for persistence.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name='grp'>
|
||||||
|
/// </param>
|
||||||
|
/// <param name='fireDetachEvent'>
|
||||||
|
/// If true, then fire the script event before we save its state.
|
||||||
|
/// </param>
|
||||||
|
private string PrepareScriptInstanceForSave(SceneObjectGroup grp, bool fireDetachEvent)
|
||||||
{
|
{
|
||||||
|
if (fireDetachEvent)
|
||||||
|
m_scene.EventManager.TriggerOnAttach(grp.LocalId, grp.FromItemID, UUID.Zero);
|
||||||
|
|
||||||
using (StringWriter sw = new StringWriter())
|
using (StringWriter sw = new StringWriter())
|
||||||
{
|
{
|
||||||
using (XmlTextWriter writer = new XmlTextWriter(sw))
|
using (XmlTextWriter writer = new XmlTextWriter(sw))
|
||||||
|
@ -752,7 +785,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateDetachedObject(IScenePresence sp, SceneObjectGroup so)
|
private void UpdateDetachedObject(IScenePresence sp, SceneObjectGroup so, string scriptedState)
|
||||||
{
|
{
|
||||||
// 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
|
||||||
|
@ -765,11 +798,6 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
&& (m_scene.UserManagementModule == null
|
&& (m_scene.UserManagementModule == null
|
||||||
|| m_scene.UserManagementModule.IsLocalGridUser(sp.UUID));
|
|| m_scene.UserManagementModule.IsLocalGridUser(sp.UUID));
|
||||||
|
|
||||||
// Scripts MUST be snapshotted before the object is
|
|
||||||
// removed from the scene because doing otherwise will
|
|
||||||
// clobber the run flag
|
|
||||||
string scriptedState = GetObjectScriptStates(so);
|
|
||||||
|
|
||||||
// Remove the object from the scene so no more updates
|
// Remove the object from the scene so no more updates
|
||||||
// are sent. Doing this before the below changes will ensure
|
// are sent. Doing this before the below changes will ensure
|
||||||
// updates can't cause "HUD artefacts"
|
// updates can't cause "HUD artefacts"
|
||||||
|
@ -793,24 +821,12 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
so.RemoveScriptInstances(true);
|
so.RemoveScriptInstances(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DetachSingleAttachmentToInvInternal(IScenePresence sp, SceneObjectGroup so)
|
|
||||||
{
|
|
||||||
// m_log.DebugFormat("[ATTACHMENTS MODULE]: Detaching item {0} to inventory for {1}", itemID, sp.Name);
|
|
||||||
|
|
||||||
m_scene.EventManager.TriggerOnAttach(so.LocalId, so.FromItemID, UUID.Zero);
|
|
||||||
sp.RemoveAttachment(so);
|
|
||||||
|
|
||||||
UpdateDetachedObject(sp, so);
|
|
||||||
}
|
|
||||||
|
|
||||||
private SceneObjectGroup RezSingleAttachmentFromInventoryInternal(
|
private SceneObjectGroup RezSingleAttachmentFromInventoryInternal(
|
||||||
IScenePresence sp, UUID itemID, UUID assetID, uint attachmentPt)
|
IScenePresence sp, UUID itemID, UUID assetID, uint attachmentPt)
|
||||||
{
|
{
|
||||||
if (m_invAccessModule == null)
|
if (m_invAccessModule == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
lock (sp.AttachmentsSyncLock)
|
|
||||||
{
|
|
||||||
SceneObjectGroup objatt;
|
SceneObjectGroup objatt;
|
||||||
|
|
||||||
if (itemID != UUID.Zero)
|
if (itemID != UUID.Zero)
|
||||||
|
@ -822,7 +838,24 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
null, assetID, Vector3.Zero, Vector3.Zero, UUID.Zero, (byte)1, true,
|
null, assetID, Vector3.Zero, Vector3.Zero, UUID.Zero, (byte)1, true,
|
||||||
false, false, sp.UUID, true);
|
false, false, sp.UUID, true);
|
||||||
|
|
||||||
if (objatt != null)
|
if (objatt == null)
|
||||||
|
{
|
||||||
|
m_log.WarnFormat(
|
||||||
|
"[ATTACHMENTS MODULE]: Could not retrieve item {0} for attaching to avatar {1} at point {2}",
|
||||||
|
itemID, sp.Name, attachmentPt);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove any previous attachments
|
||||||
|
List<SceneObjectGroup> attachments = sp.GetAttachments(attachmentPt);
|
||||||
|
string previousAttachmentScriptedState = null;
|
||||||
|
|
||||||
|
// At the moment we can only deal with a single attachment
|
||||||
|
if (attachments.Count != 0)
|
||||||
|
DetachSingleAttachmentToInv(sp, attachments[0]);
|
||||||
|
|
||||||
|
lock (sp.AttachmentsSyncLock)
|
||||||
{
|
{
|
||||||
// m_log.DebugFormat(
|
// m_log.DebugFormat(
|
||||||
// "[ATTACHMENTS MODULE]: Rezzed single object {0} for attachment to {1} on point {2} in {3}",
|
// "[ATTACHMENTS MODULE]: Rezzed single object {0} for attachment to {1} on point {2} in {3}",
|
||||||
|
@ -869,15 +902,6 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
|
|
||||||
return objatt;
|
return objatt;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
m_log.WarnFormat(
|
|
||||||
"[ATTACHMENTS MODULE]: Could not retrieve item {0} for attaching to avatar {1} at point {2}",
|
|
||||||
itemID, sp.Name, attachmentPt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -1026,8 +1050,6 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
|
|
||||||
ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
|
ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
|
||||||
if (sp != null)
|
if (sp != null)
|
||||||
{
|
|
||||||
lock (sp.AttachmentsSyncLock)
|
|
||||||
{
|
{
|
||||||
List<SceneObjectGroup> attachments = sp.GetAttachments();
|
List<SceneObjectGroup> attachments = sp.GetAttachments();
|
||||||
|
|
||||||
|
@ -1041,7 +1063,6 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void Client_OnObjectDrop(uint soLocalId, IClientAPI remoteClient)
|
private void Client_OnObjectDrop(uint soLocalId, IClientAPI remoteClient)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue