Collection was modified; enumeration operation may not execute to convert to pdfa

In a multithreading solution we convert millions of pdf documents to pdfa/2.

Used in a webapplication (c# dotnet10), and code is in library (netstandard2.0)

Aspose.pdf 26.6

Exception message:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at #=zT2vlDZ2U15SXfDyfOupsH_zDC9uviBr3PAu5D$wpVAyb_joZGTkLzDs=.#=zAl69wFqK1BZQ(String #=zww3N5S6RQy5e, Boolean #=zMQ9GLEw=, Boolean #=zbYsYK$H$_LVQnDq4kjMk2gY=)
at #=zB0SXVa5PcQ9xfECYmTeQcN0$WN3dnYufhZqlXWQOGrVY.#=zjP2EbfUCRzPzCuBRUA==()
at #=zB0SXVa5PcQ9xfECYmTeQcN0$WN3dnYufhZqlXWQOGrVY.#=zjP2EbfUCRzPzCuBRUA==()
at #=zcP5F6q2oZGu6Hpfdmwl4I68BX0hsIQyqhzqR3nIN6Nic1FFbHg==.#=zHe0FnRi8q07r(#=zr59bolJlkh6W6QoU5a_gfhPfF9C86zpKtOVYa68_xhKXtLghsx4$$QSBeC4P #=zuYTPvX_SF1KO, Dictionary`2 #=zexyj40rn1rw9)
at #=zdoRdcBLQ5Kf5FknnEt9mjQiyJF_v1YQLQLpGu3gdGuKRtk4_RjMoQQ8=.#=zrc3OIl4Vapa5()
at #=z0XyUYpXV$2uwpPMgrOneSxE659Ogl06HTc9lYodnFiweSdURimhTStw=.#=zdstYGA8=()
at #=z0XyUYpXV$2uwpPMgrOneSxE659Ogl06HTc9lYodnFiweSdURimhTStw=.#=z6CafyS8=()
at Verzekeraar.ITIntern.BC.FileConverter.Wrapper.Pdf.Convert2Pdfa(IArchiveFileUnconverted fileToConvert, ConverterOptions converterOpts)

Our code

    public override IArchiveFileConverted Convert2Pdfa(IArchiveFileUnconverted fileToConvert, ConverterOptions converterOpts)
    {
        fileToConvert = fileToConvert ?? throw new ArgumentNullException( nameof( fileToConvert ) );
        converterOpts = converterOpts ?? throw new ArgumentNullException( nameof( converterOpts ) );

        ValidateInputForConversion2Pdfa( fileToConvert, converterOpts );


        // Fix for incident INC0291180/story 1743307
        // When source document uses MinionPro-Regular but is not embedded, conversion to PDF/A-2A will fail
        // Substituting the font with Times New Roman will prevent this.
        FontRepository.Substitutions.Add(new SimpleFontSubstitution("MinionPro-Regular", "Times New Roman"));

        var documentAndStream = MakeDocument( fileToConvert );
        var document = documentAndStream.Document;
        var stream   = documentAndStream.Stream;

        using ( document )
        using ( stream )
        {
            // see the following website for optimization tips
            //https://docs.aspose.com/display/pdfnet/Optimize+PDF+Document

            var optimizeOptions = new Aspose.Pdf.Optimization.OptimizationOptions()
            {
                RemoveUnusedObjects = true,
                RemoveUnusedStreams = true,
                AllowReusePageContent = true,
                LinkDuplicateStreams = true
            };

            if (converterOpts.CompressImages)
            {
                // The following options need to be set together
                optimizeOptions.ImageCompressionOptions.CompressImages = true;
                optimizeOptions.ImageCompressionOptions.ImageQuality = converterOpts.PdfImageQuality;   // 0 (low quality) - 100% (high quality)
                optimizeOptions.ImageCompressionOptions.ResizeImages = true;
                optimizeOptions.ImageCompressionOptions.Version = Aspose.Pdf.Optimization.ImageCompressionVersion.Standard;   // Standard for better quality
            }

            document.OptimizeResources(optimizeOptions);

            document.EnsureCreationDate();
            document.EnsureModDate();

#if DEBUG // Handy when you want to track font substitution (e.g. for incident INC0291180/story 1743307)
document.FontSubstitution += Document_FontSubstitution;
#endif

            using (var ms = new MemoryStream())
            {
                document.Convert(ms, PdfFormat.PDF_A_2A, ConvertErrorAction.Delete);
            }


            // NOTE : No SaveOptions are needed...
            var result = SavePdf( converterOpts , saveOpts : null as object , document ,
                saveActionForStream : ( pDoc , pStream   , pSaveOpts ) => pDoc.Save( pStream   ) ,
                saveActionForFile   : ( pDoc , pFilename , pSaveOpts ) => pDoc.Save( pFilename ) );

            return result;
        }
    }

Is see another form topic with same issue:

Convert PDF to PDF/A_2A using Aspose.PDF for.NET - Collection was modified; enumeration operation may not execute - Free Support Forum - aspose.com

But still we have this issue.

@jordybaart
Hi, thank you for reporting
Does this error appears on specific documents or it’s just general case that during multi-thread operations the process breaks with exception you mentioned?
Is it present on smaller amounts of documents ?

UPD. I’ve read the code you provided. Can you share an example of document described here so we could test that?

When source document uses MinionPro-Regular but is not embedded, conversion to PDF/A-2A will fail

I’m a collegue of Jordy B and am also aware of the issue.

It’s a general case of multi-threaded operations issue. It’s not related to a specific document.
Reducing the number of concurrent threads (16 down-to 4) only reduces the frequency of the issue.

This seems to be a related issue:
Convert PDF to PDF/A_2A using Aspose.PDF for.NET - Collection was modified; enumeration operation may not execute - Free Support Forum - aspose.com