This project has moved. For the latest updates, please go here.

Is DocX thread safe?

Jun 10, 2016 at 11:32 AM
I have read that the Microsoft Open XML SDK is not thread safe (https://support.microsoft.com/en-gb/kb/951731) and therefore not suitable for server side development without a fix.

I was wondering if this library known to be thread safe?
Developer
Jun 10, 2016 at 11:39 AM
Edited Jun 10, 2016 at 11:40 AM
Your best bet is to make tests yourself. I don't think we did any testing. Eventually take a look at implementation in sources and modify to include workaround provided in your link.
Jun 10, 2016 at 11:49 AM
Ok thanks for the quick response. I thought I would ask first on the off chance it had already been covered :-)
Jun 10, 2016 at 11:54 AM
Ok had a look and I can see it will have the same issue as the Open XML SDK as its using the System.IO.Packaging.PackagePart which is where the problem rests:

https://docx.codeplex.com/SourceControl/latest#DocX/_BaseClasses.cs

Microsoft proposed a workaround for using that library here: https://support.microsoft.com/en-gb/kb/951731 which should work here as well
public class PackagePartStream
{
    private Stream _stream;
    private static Mutex _m = new Mutex(false); 

    public PackagePartStream(Stream Stream)
    {
        _stream = Stream;
    } 

    public int Read(byte[] buffer, int offset, int count)
    {
        return _stream.Read(buffer, offset, count);
    } 

    public void Write(byte[] buffer, int offset, int count)
    {
        _m.WaitOne(Timeout.Infinite, false);
        _stream.Write(buffer, offset, count);
        _m.ReleaseMutex();
    } 

    public void Flush()
    {
        _m.WaitOne(Timeout.Infinite, false);            
        _stream.Flush();
        _m.ReleaseMutex();
    }
}
Developer
Jun 10, 2016 at 11:58 AM
Well go ahead and apply it to the sources on github, test it and when you're good I'll apply it to main sources and release binary later on.
Jun 10, 2016 at 12:02 PM
Sure, I will take a look later today.
Jun 10, 2016 at 5:03 PM
Ok spent a little time looking into this in more detail and looks like the underlying issue was fixed by Microsoft as part of a hotfix to .NET 4.5

https://support.microsoft.com/en-us/kb/2996566 (Issue 1)

And the author of OpenXml also rewrote it to avoid the issue, so looks like this is no longer an issue and in principal DocX should be thread safe as well.
Marked as answer by MadBoy on 6/10/2016 at 9:16 AM
Developer
Jun 10, 2016 at 5:16 PM
Thanks for your input!
Jun 13, 2016 at 11:29 AM
Ok sorry I have re-looked at this and it appears even though the hotfix is only available (see previous link) for those that requested it, it is not installed by default (as far as I can tell).

I have created a test program locally and I was getting what appeared to be deadlocks (the program never terminated) when I ran 6 threads (number varies between runs) loading an 15Mb JPEG into new documents.

I have tested a fix for this that worked which I can submit if that's ok?
Developer
Jun 13, 2016 at 11:32 AM
Yep ;-) Github. Make sure thou that all tests are working after fix is there and that output documents from Examples are good too.
Jun 13, 2016 at 1:40 PM
Done, tested nothing broke and submitted the pull request (https://github.com/WordDocX/DocX/pull/32)
Marked as answer by MadBoy on 6/13/2016 at 5:59 AM
Developer
Jun 13, 2016 at 2:00 PM
Merged and done. Thanks. Will need to update binaries here later on.