VahidN / iTextSharp.LGPLv2.Core

iTextSharp.LGPLv2.Core is an unofficial port of the last LGPL version of the iTextSharp (V4.1.6) to .NET Core
Other
615 stars 154 forks source link

OrderedTree is not thread safe #101

Closed xiaoliyu closed 3 years ago

xiaoliyu commented 3 years ago

Summary of the issue

The OrderedTree is using global static variable and it will result in unpredictable issue in multiple threading environment.

Environment

.NET Core 3.1

iTextSharp.LGPLv2.Core version: 
.NET Core SDK version: 
IDE: 

Example code/Steps to reproduce:


paste your core code
string pdfFile = @"c:\temp\test.pdf";
            int startpage = 1000, endpage=1004
            Parallel.For(0, 20,new ParallelOptions() { MaxDegreeOfParallelism = 8 }, (i) =>
            {
                PdfReader reader = null;
                Document sourceDocument = null;
                PdfCopy pdfCopyProvider = null;
                PdfImportedPage importedPage = null;

                reader = new PdfReader(new RandomAccessFileOrArray(File.ReadAllBytes(pdfFile)), null);
                sourceDocument = new Document(reader.GetPageSizeWithRotation(1000));
                var outputStream = new MemoryStream();
                pdfCopyProvider = new PdfCopy(sourceDocument, outputStream);
                pdfCopyProvider.SetFullCompression();
                sourceDocument.Open();

                for (int j = startpage; i <= endpage; j++)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                    pdfCopyProvider.AddPage(importedPage);
                }

                pdfCopyProvider.FreeReader(reader);
                reader.Close();

                sourceDocument.Close();

            });

## Output:

The application will hang when calling document.Close
Exception message:
Full Stack trace:
    iTextSharp.LGPLv2.Core.dll!iTextSharp.text.pdf.PdfWriter.PdfBody.WriteCrossReferenceTable(System.IO.Stream os, iTextSharp.text.pdf.PdfIndirectReference root, iTextSharp.text.pdf.PdfIndirectReference info, iTextSharp.text.pdf.PdfIndirectReference encryption, iTextSharp.text.pdf.PdfObject fileId, int prevxref)   Unknown
    iTextSharp.LGPLv2.Core.dll!iTextSharp.text.pdf.PdfWriter.Close()    Unknown
    iTextSharp.LGPLv2.Core.dll!iTextSharp.text.pdf.PdfCopy.Close()  Unknown
    iTextSharp.LGPLv2.Core.dll!iTextSharp.text.pdf.PdfDocument.Close()  Unknown
    iTextSharp.LGPLv2.Core.dll!iTextSharp.text.Document.Close() Unknown
>   LargePdfTester.dll!LargePdfTester.Program.ExtractPages(string pdfFile, int startpage, int endpage) Line 80  C#
    LargePdfTester.dll!LargePdfTester.Program.Main.AnonymousMethod__0(int i) Line 34    C#
    System.Threading.Tasks.Parallel.dll!System.Threading.Tasks.Parallel.ForWorker.AnonymousMethod__1(ref System.Threading.Tasks.RangeWorker currentWorker, int timeout, out bool replicationDelegateYieldedBeforeCompletion)    Unknown
    System.Threading.Tasks.Parallel.dll!System.Threading.Tasks.TaskReplicator.Replica<System.Threading.Tasks.RangeWorker>.ExecuteAction(out bool yieldedBeforeCompletion)   Unknown
    System.Threading.Tasks.Parallel.dll!System.Threading.Tasks.TaskReplicator.Replica.Execute() Unknown
    System.Threading.Tasks.Parallel.dll!System.Threading.Tasks.TaskReplicator.Replica..ctor.AnonymousMethod__4_0(object s)  Unknown
    System.Private.CoreLib.dll!System.Threading.Tasks.Task.InnerInvoke()    Unknown
    System.Private.CoreLib.dll!System.Threading.Tasks.Task..cctor.AnonymousMethod__274_0(object obj)    Unknown
    System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread threadPoolThread, System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)   Unknown
    System.Private.CoreLib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot, System.Threading.Thread threadPoolThread)    Unknown
    System.Private.CoreLib.dll!System.Threading.Tasks.Task.ExecuteEntryUnsafe(System.Threading.Thread threadPoolThread) Unknown
    System.Private.CoreLib.dll!System.Threading.Tasks.Task.ExecuteFromThreadPool(System.Threading.Thread threadPoolThread)  Unknown
    System.Private.CoreLib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch()  Unknown
    System.Private.CoreLib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()   Unknown
[Fixed-OrderedTree.cs.txt](https://github.com/VahidN/iTextSharp.LGPLv2.Core/files/7188337/Fixed-OrderedTree.cs.txt)
xiaoliyu commented 3 years ago

I have provided an fixed version for your reference.

https://github.com/VahidN/iTextSharp.LGPLv2.Core/files/7188337/Fixed-OrderedTree.cs.txt

VahidN commented 3 years ago

Improved it via#https://github.com/VahidN/iTextSharp.LGPLv2.Core/commit/603ceeea661f6cd437c53e1a95fca6504ce65077