Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ControlTag's _factory Dicttionary is not threadsafe #86

Open
jespersh opened this issue Oct 11, 2023 · 0 comments
Open

ControlTag's _factory Dicttionary is not threadsafe #86

jespersh opened this issue Oct 11, 2023 · 0 comments

Comments

@jespersh
Copy link

In an attempt to scale out, I found that

private static Dictionary<Type, Func<bool, ControlWord<bool>>> _factory = new Dictionary<Type, Func<bool, ControlWord<bool>>>();

is being accessed in a manner that isn't thread-safe inside of
public static ControlWord<bool> Negate(ControlWord<bool> word)
{
var type = word.GetType();
if (!_factory.TryGetValue(type, out var factory))
{
var ctor = type.GetConstructor(new[] { typeof(bool) });
var boolParam = Expression.Parameter(typeof(bool), "value");
factory = (Func<bool, ControlWord<bool>>)Expression.Lambda(Expression.Convert(Expression.New(ctor, boolParam), typeof(ControlWord<bool>)), boolParam).Compile();
_factory[type] = factory;
}
return factory(!word.Value);
}

It can show up as various exceptions, but in this case it's an System.IndexOutOfRangeException

   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at RtfPipe.ControlTag.Negate(ControlWord`1 word)
   at RtfPipe.Model.HtmlVisitor.GetNewStyles(IEnumerable`1 styles, HtmlTag tag)
   at RtfPipe.Model.HtmlVisitor.Visit(Element element)
   at RtfPipe.Model.HtmlVisitor.Visit(Element element)
   at RtfPipe.Model.HtmlVisitor.Visit(RtfHtml document)
   at RtfPipe.Rtf.ToHtml(RtfSource source, XmlWriter writer, RtfHtmlSettings settings)
   at RtfPipe.Rtf.ToHtml(RtfSource source, RtfHtmlSettings settings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant