Skip to content

DocX.SavE() is not thread safe #9

@miebster

Description

@miebster

I'm using DocX to create several documents from different threads at the same time. The thread that is trying to call DocX.Save() has hung indefinitely.

WindowsBase.dll!MS.Internal.IO.Packaging.PackagingUtilities.CreateUserScopedIsolatedStorageFileStreamWithRandomName(int retryCount, out string fileName) + 0x7b bytes   
WindowsBase.dll!MS.Internal.IO.Packaging.SparseMemoryStream.SwitchModeIfNecessary() + 0x172 bytes   
WindowsBase.dll!MS.Internal.IO.Packaging.SparseMemoryStream.Write(byte[] buffer, int offset, int count) + 0xac bytes    
WindowsBase.dll!MS.Internal.IO.Packaging.CompressEmulationStream.Write(byte[] buffer, int offset, int count) + 0x3d bytes   
WindowsBase.dll!MS.Internal.IO.Packaging.CompressStream.Write(byte[] buffer, int offset, int count) + 0x8a bytes    
WindowsBase.dll!MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Write(byte[] buffer, int offset, int count) + 0xad bytes 
WindowsBase.dll!MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Write(byte[] buffer, int offset, int count) + 0x56 bytes    
mscorlib.dll!System.IO.StreamWriter.Flush(bool flushStream, bool flushEncoder) + 0x62 bytes 
mscorlib.dll!System.IO.StreamWriter.Write(char[] buffer, int index, int count) + 0xab bytes 
System.Xml.dll!System.Xml.XmlEncodedRawTextWriter.FlushBuffer() + 0x3c bytes    
System.Xml.dll!System.Xml.XmlEncodedRawTextWriter.RawText(char* pSrcBegin, char* pSrcEnd) + 0xf1 bytes  
System.Xml.dll!System.Xml.XmlEncodedRawTextWriter.RawText(string s) + 0x23 bytes    
System.Xml.dll!System.Xml.XmlEncodedRawTextWriterIndent.WriteIndent() + 0x22 bytes  
System.Xml.dll!System.Xml.XmlEncodedRawTextWriterIndent.WriteStartElement(string prefix, string localName, string ns) + 0x21 bytes  
System.Xml.dll!System.Xml.XmlWellFormedWriter.WriteStartElement(string prefix, string localName, string ns) + 0x9d bytes    
System.Xml.Linq.dll!System.Xml.Linq.ElementWriter.WriteStartElement(System.Xml.Linq.XElement e) + 0x47 bytes    
System.Xml.Linq.dll!System.Xml.Linq.ElementWriter.WriteElement(System.Xml.Linq.XElement e) + 0x3c bytes 
System.Xml.Linq.dll!System.Xml.Linq.XElement.WriteTo(System.Xml.XmlWriter writer) + 0x49 bytes  
System.Xml.Linq.dll!System.Xml.Linq.XContainer.WriteContentTo(System.Xml.XmlWriter writer) + 0xd4 bytes 
System.Xml.Linq.dll!System.Xml.Linq.XDocument.WriteTo(System.Xml.XmlWriter writer) + 0x79 bytes 
System.Xml.Linq.dll!System.Xml.Linq.XDocument.Save(System.IO.TextWriter textWriter, System.Xml.Linq.SaveOptions options) + 0x3c bytes   
DocX.dll!Novacode.DocX.Save() + 0xc1 bytes  

Searching for this method "CreateUserScopedIsolatedStorageFileStreamWithRandomName" yielded this SO question https://stackoverflow.com/questions/21482820/openxml-hanging-while-writing-elements#

Microsoft published an article about the problem here https://stackoverflow.com/questions/21482820/openxml-hanging-while-writing-elements#

Long story short, "CreateUserScopedIsolatedStorageFileStreamWithRandomName" is not thread safe, so by extension "XDocument.Save" and "DocX.Save" are not thread safe.

For now I will try to work around the issue in my application. I'd recommend a fix within DocX to prevent other users from trying to debug a deadlock.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions