Fix race condition that would sometimes send or save appearance for the wrong avatar.

In AvatarFactoryModule.HandleAppearanceUpdateTimer(), we loop through appearance save and send requests and dispatch via a FireAndForget thread.
If there was more than one request in the save or send queue, then this led to a subtle race condition where the foreach loop would load in the next KeyValuePair before the thread was dispatched.
This gave the thread the wrong avatar ID, leaving some avatar appearance cloudy since appearance data was never sent.
This change loads the fields into local references so that this doesn't happen.
0.7.2-post-fixes
Justin Clark-Casey (justincc) 2011-11-02 18:12:12 +00:00
parent f9e6e32ce2
commit 6a994f8c9c
2 changed files with 24 additions and 9 deletions

View File

@ -385,7 +385,7 @@ namespace OpenSim.Region.CoreModules.Avatar.AvatarFactory
/// </summary>
public void QueueAppearanceSend(UUID agentid)
{
// m_log.WarnFormat("[AVFACTORY]: Queue appearance send for {0}", agentid);
// m_log.DebugFormat("[AVFACTORY]: Queue appearance send for {0}", agentid);
// 10000 ticks per millisecond, 1000 milliseconds per second
long timestamp = DateTime.Now.Ticks + Convert.ToInt64(m_sendtime * 1000 * 10000);
@ -444,10 +444,17 @@ namespace OpenSim.Region.CoreModules.Avatar.AvatarFactory
Dictionary<UUID, long> sends = new Dictionary<UUID, long>(m_sendqueue);
foreach (KeyValuePair<UUID, long> kvp in sends)
{
if (kvp.Value < now)
// We have to load the key and value into local parameters to avoid a race condition if we loop
// around and load kvp with a different value before FireAndForget has launched its thread.
UUID avatarID = kvp.Key;
long sendTime = kvp.Value;
// m_log.DebugFormat("[AVFACTORY]: Handling queued appearance updates for {0}, update delta to now is {1}", avatarID, sendTime - now);
if (sendTime < now)
{
Util.FireAndForget(delegate(object o) { SendAppearance(kvp.Key); });
m_sendqueue.Remove(kvp.Key);
Util.FireAndForget(o => SendAppearance(avatarID));
m_sendqueue.Remove(avatarID);
}
}
}
@ -457,10 +464,15 @@ namespace OpenSim.Region.CoreModules.Avatar.AvatarFactory
Dictionary<UUID, long> saves = new Dictionary<UUID, long>(m_savequeue);
foreach (KeyValuePair<UUID, long> kvp in saves)
{
if (kvp.Value < now)
// We have to load the key and value into local parameters to avoid a race condition if we loop
// around and load kvp with a different value before FireAndForget has launched its thread.
UUID avatarID = kvp.Key;
long sendTime = kvp.Value;
if (sendTime < now)
{
Util.FireAndForget(delegate(object o) { SaveAppearance(kvp.Key); });
m_savequeue.Remove(kvp.Key);
Util.FireAndForget(o => SaveAppearance(avatarID));
m_savequeue.Remove(avatarID);
}
}
}
@ -535,6 +547,8 @@ namespace OpenSim.Region.CoreModules.Avatar.AvatarFactory
public bool SendAppearance(UUID agentId)
{
// m_log.DebugFormat("[AVFACTORY]: Sending appearance for {0}", agentId);
ScenePresence sp = m_scene.GetScenePresence(agentId);
if (sp == null)
{

View File

@ -2610,7 +2610,8 @@ namespace OpenSim.Region.Framework.Scenes
/// </summary>
public void SendAppearanceToAllOtherAgents()
{
//m_log.DebugFormat("[SCENE PRESENCE] SendAppearanceToAllOtherAgents: {0} ({1})", Name, UUID);
// m_log.DebugFormat("[SCENE PRESENCE] SendAppearanceToAllOtherAgents: {0} {1}", Name, UUID);
// only send update from root agents to other clients; children are only "listening posts"
if (IsChildAgent)
{
@ -2638,7 +2639,7 @@ namespace OpenSim.Region.Framework.Scenes
/// </summary>
public void SendOtherAgentsAppearanceToMe()
{
//m_log.DebugFormat("[SCENE PRESENCE] SendOtherAgentsAppearanceToMe: {0} ({1})", Name, UUID);
// m_log.DebugFormat("[SCENE PRESENCE] SendOtherAgentsAppearanceToMe: {0} {1}", Name, UUID);
int count = 0;
m_scene.ForEachRootScenePresence(delegate(ScenePresence scenePresence)