Resolve various race conditions between accessing and removing external script URLs by more consistently locking on m_UrlMap

0.7.3-extended
Justin Clark-Casey (justincc) 2012-06-22 23:49:52 +01:00
parent 1cfaacb88b
commit c5e5308120
1 changed files with 147 additions and 131 deletions

View File

@ -41,13 +41,39 @@ using OpenSim.Region.Framework.Scenes;
namespace OpenSim.Region.CoreModules.Scripting.LSLHttp namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
{ {
/// <summary>
/// Data describing an external URL set up by a script.
/// </summary>
public class UrlData public class UrlData
{ {
/// <summary>
/// Scene object part hosting the script
/// </summary>
public UUID hostID; public UUID hostID;
/// <summary>
/// The item ID of the script that requested the URL.
/// </summary>
public UUID itemID; public UUID itemID;
/// <summary>
/// The script engine that runs the script.
/// </summary>
public IScriptModule engine; public IScriptModule engine;
/// <summary>
/// The generated URL.
/// </summary>
public string url; public string url;
/// <summary>
/// The random UUID component of the generated URL.
/// </summary>
public UUID urlcode; public UUID urlcode;
/// <summary>
/// The external requests currently being processed or awaiting retrieval for this URL.
/// </summary>
public Dictionary<UUID, RequestData> requests; public Dictionary<UUID, RequestData> requests;
} }
@ -77,6 +103,10 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
/// Indexs the URL request metadata (which script requested it, outstanding requests, etc.) by the request ID /// Indexs the URL request metadata (which script requested it, outstanding requests, etc.) by the request ID
/// randomly generated when a request is received for this URL. /// randomly generated when a request is received for this URL.
/// </summary> /// </summary>
/// <remarks>
/// Manipulation or retrieval from this dictionary must be locked on m_UrlMap to preserve consistency with
/// m_UrlMap
/// </remarks>
private Dictionary<UUID, UrlData> m_RequestMap = new Dictionary<UUID, UrlData>(); private Dictionary<UUID, UrlData> m_RequestMap = new Dictionary<UUID, UrlData>();
/// <summary> /// <summary>
@ -109,10 +139,9 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
{ {
m_ExternalHostNameForLSL = config.Configs["Network"].GetString("ExternalHostNameForLSL", System.Environment.MachineName); m_ExternalHostNameForLSL = config.Configs["Network"].GetString("ExternalHostNameForLSL", System.Environment.MachineName);
bool ssl_enabled = config.Configs["Network"].GetBoolean("https_listener",false); bool ssl_enabled = config.Configs["Network"].GetBoolean("https_listener",false);
if (ssl_enabled) if (ssl_enabled)
{
https_port = (uint) config.Configs["Network"].GetInt("https_port",0); https_port = (uint) config.Configs["Network"].GetInt("https_port",0);
}
IConfig llFunctionsConfig = config.Configs["LL-Functions"]; IConfig llFunctionsConfig = config.Configs["LL-Functions"];
@ -257,6 +286,8 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
} }
public void HttpResponse(UUID request, int status, string body) public void HttpResponse(UUID request, int status, string body)
{
lock (m_UrlMap)
{ {
if (m_RequestMap.ContainsKey(request)) if (m_RequestMap.ContainsKey(request))
{ {
@ -271,8 +302,11 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
m_log.Info("[HttpRequestHandler] There is no http-in request with id " + request.ToString()); m_log.Info("[HttpRequestHandler] There is no http-in request with id " + request.ToString());
} }
} }
}
public string GetHttpHeader(UUID requestId, string header) public string GetHttpHeader(UUID requestId, string header)
{
lock (m_UrlMap)
{ {
if (m_RequestMap.ContainsKey(requestId)) if (m_RequestMap.ContainsKey(requestId))
{ {
@ -285,12 +319,14 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
{ {
m_log.Warn("[HttpRequestHandler] There was no http-in request with id " + requestId); m_log.Warn("[HttpRequestHandler] There was no http-in request with id " + requestId);
} }
}
return String.Empty; return String.Empty;
} }
public int GetFreeUrls() public int GetFreeUrls()
{ {
lock (m_UrlMap)
return m_TotalUrls - m_UrlMap.Count; return m_TotalUrls - m_UrlMap.Count;
} }
@ -349,9 +385,9 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
private Hashtable NoEvents(UUID requestID, UUID sessionID) private Hashtable NoEvents(UUID requestID, UUID sessionID)
{ {
Hashtable response = new Hashtable(); Hashtable response = new Hashtable();
UrlData url; UrlData urlData;
lock (m_RequestMap) lock (m_UrlMap)
{ {
// We need to return a 404 here in case the request URL was removed at exactly the same time that a // We need to return a 404 here in case the request URL was removed at exactly the same time that a
// request was made. In this case, the request thread can outrace llRemoveURL() and still be polling // request was made. In this case, the request thread can outrace llRemoveURL() and still be polling
@ -366,10 +402,9 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
return response; return response;
} }
url = m_RequestMap[requestID]; urlData = m_RequestMap[requestID];
}
if (System.Environment.TickCount - url.requests[requestID].startTime > 25000) if (System.Environment.TickCount - urlData.requests[requestID].startTime > 25000)
{ {
response["int_response_code"] = 500; response["int_response_code"] = 500;
response["str_response_string"] = "Script timeout"; response["str_response_string"] = "Script timeout";
@ -378,23 +413,19 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
response["reusecontext"] = false; response["reusecontext"] = false;
//remove from map //remove from map
lock (url) urlData.requests.Remove(requestID);
{
url.requests.Remove(requestID);
m_RequestMap.Remove(requestID); m_RequestMap.Remove(requestID);
}
return response; return response;
} }
}
return response; return response;
} }
private bool HasEvents(UUID requestID, UUID sessionID) private bool HasEvents(UUID requestID, UUID sessionID)
{ {
UrlData url = null; lock (m_UrlMap)
lock (m_RequestMap)
{ {
// We return true here because an external URL request that happened at the same time as an llRemoveURL() // We return true here because an external URL request that happened at the same time as an llRemoveURL()
// can still make it through to HttpRequestHandler(). That will return without setting up a request // can still make it through to HttpRequestHandler(). That will return without setting up a request
@ -406,40 +437,42 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
return true; return true;
} }
url = m_RequestMap[requestID]; UrlData urlData = m_RequestMap[requestID];
if (!url.requests.ContainsKey(requestID))
if (!urlData.requests.ContainsKey(requestID))
{ {
return true; return true;
} }
}
// Trigger return of timeout response. // Trigger return of timeout response.
if (System.Environment.TickCount - url.requests[requestID].startTime > 25000) if (System.Environment.TickCount - urlData.requests[requestID].startTime > 25000)
{ {
return true; return true;
} }
return url.requests[requestID].requestDone; return urlData.requests[requestID].requestDone;
}
} }
private Hashtable GetEvents(UUID requestID, UUID sessionID, string request) private Hashtable GetEvents(UUID requestID, UUID sessionID, string request)
{
Hashtable response;
lock (m_UrlMap)
{ {
UrlData url = null; UrlData url = null;
RequestData requestData = null; RequestData requestData = null;
lock (m_RequestMap)
{
if (!m_RequestMap.ContainsKey(requestID)) if (!m_RequestMap.ContainsKey(requestID))
return NoEvents(requestID, sessionID); return NoEvents(requestID, sessionID);
url = m_RequestMap[requestID]; url = m_RequestMap[requestID];
requestData = url.requests[requestID]; requestData = url.requests[requestID];
}
if (!requestData.requestDone) if (!requestData.requestDone)
return NoEvents(requestID, sessionID); return NoEvents(requestID, sessionID);
Hashtable response = new Hashtable(); response = new Hashtable();
if (System.Environment.TickCount - requestData.startTime > 25000) if (System.Environment.TickCount - requestData.startTime > 25000)
{ {
@ -459,8 +492,6 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
response["reusecontext"] = false; response["reusecontext"] = false;
//remove from map //remove from map
lock (url)
{
url.requests.Remove(requestID); url.requests.Remove(requestID);
m_RequestMap.Remove(requestID); m_RequestMap.Remove(requestID);
} }
@ -469,8 +500,6 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
} }
public void HttpRequestHandler(UUID requestID, Hashtable request) public void HttpRequestHandler(UUID requestID, Hashtable request)
{
lock (request)
{ {
string uri = request["uri"].ToString(); string uri = request["uri"].ToString();
bool is_ssl = uri.Contains("lslhttps"); bool is_ssl = uri.Contains("lslhttps");
@ -507,7 +536,6 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
// request was being processed. // request was being processed.
if (!m_UrlMap.TryGetValue(url, out urlData)) if (!m_UrlMap.TryGetValue(url, out urlData))
return; return;
}
//for llGetHttpHeader support we need to store original URI here //for llGetHttpHeader support we need to store original URI here
//to make x-path-info / x-query-string / x-script-url / x-remote-ip headers //to make x-path-info / x-query-string / x-script-url / x-remote-ip headers
@ -527,6 +555,7 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
string value = (string)header.Value; string value = (string)header.Value;
requestData.headers.Add(key, value); requestData.headers.Add(key, value);
} }
foreach (DictionaryEntry de in request) foreach (DictionaryEntry de in request)
{ {
if (de.Key.ToString() == "querystringkeys") if (de.Key.ToString() == "querystringkeys")
@ -553,14 +582,7 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
requestData.headers["x-query-string"] = queryString; requestData.headers["x-query-string"] = queryString;
requestData.headers["x-script-url"] = urlData.url; requestData.headers["x-script-url"] = urlData.url;
//requestData.ev = new ManualResetEvent(false);
lock (urlData.requests)
{
urlData.requests.Add(requestID, requestData); urlData.requests.Add(requestID, requestData);
}
lock (m_RequestMap)
{
m_RequestMap.Add(requestID, urlData); m_RequestMap.Add(requestID, urlData);
} }
@ -568,11 +590,6 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
urlData.itemID, urlData.itemID,
"http_request", "http_request",
new Object[] { requestID.ToString(), request["http-method"].ToString(), request["body"].ToString() }); new Object[] { requestID.ToString(), request["http-method"].ToString(), request["body"].ToString() });
//send initial response?
// Hashtable response = new Hashtable();
return;
} }
catch (Exception we) catch (Exception we)
{ {
@ -582,7 +599,6 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp
m_log.Warn(we.StackTrace); m_log.Warn(we.StackTrace);
} }
} }
}
private void OnScriptReset(uint localID, UUID itemID) private void OnScriptReset(uint localID, UUID itemID)
{ {