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

Is DocX thread safe?

Jun 10, 2016 at 10:32 AM
I have read that the Microsoft Open XML SDK is not thread safe ( and therefore not suitable for server side development without a fix.

I was wondering if this library known to be thread safe?
Jun 10, 2016 at 10:39 AM
Edited Jun 10, 2016 at 10: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 10: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 10: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:

Microsoft proposed a workaround for using that library here: 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);

    public void Flush()
        _m.WaitOne(Timeout.Infinite, false);            
Jun 10, 2016 at 10: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 11:02 AM
Sure, I will take a look later today.
Jun 10, 2016 at 4: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 (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
Jun 10, 2016 at 4:16 PM
Thanks for your input!
Jun 13, 2016 at 10: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?
Jun 13, 2016 at 10: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 12:40 PM
Done, tested nothing broke and submitted the pull request (
Marked as answer by MadBoy on 6/13/2016 at 5:59 AM
Jun 13, 2016 at 1:00 PM
Merged and done. Thanks. Will need to update binaries here later on.