Skip to content

Commit

Permalink
Adding locking for lazy-load of static dictionary in .NET
Browse files Browse the repository at this point in the history
When running in parallel, there is a chance that multiple threads could
attempt to populate a static dictionary used for translating error codes.
This commit adds locking around the initialization of the static
dictionary. Note that we are not using "double-check" locking, but the
locking we use should be good enough. Fixes and closes issue SeleniumHQ#3166.
  • Loading branch information
jimevans committed Mar 29, 2018
1 parent a4f3c97 commit 81e91a8
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dotnet/src/webdriver/Remote/WebDriverError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ internal static class WebDriverError
public const string UnsupportedOperation = "unsupported operation";

private static Dictionary<string, WebDriverResult> resultMap;
private static object lockObject = new object();

/// <summary>
/// Converts a string error to a <see cref="WebDriverResult"/> value.
Expand All @@ -187,9 +188,12 @@ internal static class WebDriverError
/// <returns>The converted <see cref="WebDriverResult"/> value.</returns>
public static WebDriverResult ResultFromError(string error)
{
if (resultMap == null)
lock(lockObject)
{
InitializeResultMap();
if (resultMap == null)
{
InitializeResultMap();
}
}

if (!resultMap.ContainsKey(error))
Expand Down

0 comments on commit 81e91a8

Please sign in to comment.