OfficeDev / Open-Xml-PowerTools

MIT License
692 stars 26 forks source link

DocumentBuilder.BuildDocument not thread-safe #249

Closed rmourato closed 5 years ago

rmourato commented 6 years ago

DocumentBuilder maintains private static collections that BuildDocument mutates. One such collection is NamespaceAttributes which can cause the API to fail when used concurrently, with exceptions like:

System.InvalidOperationException: Duplicate attribute. at System.Xml.Linq.XElement.AddAttributeSkipNotify(XAttribute a) at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content) at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content) at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content) at OpenXmlPowerTools.DocumentBuilder.BuildDocument(List`1 sources, WordprocessingDocument output)

For now I've worked around this by creating a wrapper that synchronizes access to it but the contention introduced is not ideal.

Easiest solution I can see would be to make a non-static DocumentBuilder whilst leaving the current public API static as is (as not to break existing code). The public API would operate on a new instance so data is never shared between thread contexts.

If you'd welcome a pull request let me know and I'll see what I can do.

tomjebo commented 5 years ago

Closing all issues as this repo is being archived and will no longer be maintained by Microsoft. The project is licensed for continued use and development by forking to your own repo.