eSigning API multiple calls issue in .NET

Hi,

We’re using the Signature API to sign a pdf in memory using memory streams. This works fine when called in a single thread. However, this is used in an intranet API and multiple calls are possible. And once the code is used by multiple requests this exception is thrown at the Signature.Sign method :

Cannot access a disposed object. Object name: ‘Document’.

The code is used in MVC Web API and following is the full code:

        using (var destination = new MemoryStream())
        {
            using (var ms = new MemoryStream(pdfBytes))
            {
                ms.CopyTo(destination);

                using (var signature = new Signature(ms))
                {
                    var digitalSignOptions = new DigitalSignOptions(CertificateStream)
                    {
                        
                        Left = left,
                        Top = top,
                        Height = height,
                        Password = DigitalSignaturePassword,
                        Contact = "test@test.com",
                        Reason = "Authorize abc",
                        Location = $"Test Location",
                        Visible = false,
                        
                    };



                    signature.Sign(destination, digitalSignOptions);
                }
            }

            return destination.ToArray();
        }

Please advise.

1 Like

@taxreturned,

Can you please specify the API version (e.g. 19.10, 20.1) that you integrated in the application?

20.1, the latest Nuget package.

1 Like

@taxreturned,

We are investigating this behavior. Your investigation ticket ID is SIGNATURENET-2609. As there is any further update, you’ll be notified.

@taxreturned,

Please have a look at the code, on line 3 are you are trying to copy pdfBytes to destination array?

  1. using (var destination = new MemoryStream()){
  2. using (var ms = new MemoryStream(pdfBytes)){
  3. ms.CopyTo(destination);

Please share following details:

  1. Please provide full example with pdfBytes source , example files etc
  2. Do not copy pdfBytes to destination stream
  3. We need to see the full example where pdfBytes were taken from. In this block we do not see any issues with multi threading since all variable are local?
  4. Is there some synchronization routines are being used?
  5. Are you facing this issue for all kind of file formats (Word, PDF etc) or just for PDF?

Hi,

Thanks for the reply.

I’ve since removed the copying of the source memory stream to the destination but still getting the error. Also, it’s worth knowing that this issue arises even when direct files are used instead of memory streams.

  1. I can’t send you the full code as it’s a complex Web API project. I’ve created a sample project which replicates the issue we’re having on the API.
  2. I’ve already removed this in the web API and the sample project and still getting the error.
  3. In the Web API, the pdf is generated dynamically into a byte array, in the sample, it’s loaded from the file system, both are giving the same result and having the same issue.
  4. There are no synchronisation routines, the web API call to sign the PDF is called from another app internally.
  5. We haven’t tried with Word documents as we use the functionality on PDFs.

Memory Issue On Signature.zip (180.1 KB)

1 Like

@taxreturned,

Thanks for the details. We’ll further investigate this issue and get back to you.

@taxreturned,

Please download and run this project.zip (179.3 KB). It uses pre-release 20.2 staging version of the API. And let us know if issue persists.

Thanks for that.

The project contains a reference to GroupDocs.Signature.20.2.0-alpha-20200219112656, and it’s not available in Nuget. Where can I download it?

@taxreturned,

You can get NuGet here (NuGet QA Gallery | GroupDocs.Signature 20.2.0-alpha-20200219112656).

Thank you very much for this. The pre-release seems to have fixed the issue. Will do more tests and confirm. Is it safe to run this pre-release api in a production environment?

1 Like

@taxreturned,

This staging version has no changes except Pdf Digital Signing fix, next week we will publish official version with few other changes.
So please feel free to use exactly this version until February release.

Hi, We’re having a similar issue again, where the signature library is throwing an out of memory exception when usage is high. We have upgraded to the latest nuget version, but still have the issue. Please advise. Thanks.

@taxreturned

In order to further investigate this scenario, we may need a sample console application using that issue could be reproduced. Could you please share such an application along-with problematic file(s)?

We released latest 21.3 - I will double check on sample project provided long time ago. @taxreturned is it possible to share your current part of project that uses Signature to try reproducing the issue? mb you can share sample code to simulate this? Thank you!

Hi,

Thanks for your quick reply.

We’re essentially using the same code as we did last year for signing. As I’ve mentioned above its part of a Web API project so can’t really send the project. This is the code:

        using (var ms = new MemoryStream(pdfBytes))
        {
            using (var destination = new MemoryStream())
            {

                using (var signature = new Signature(ms))
                {
                    using (var pfxStream = new MemoryStream(CertificateBytes))
                    {
                        var digitalSignOptions = new DigitalSignOptions(pfxStream)
                        {
                            Left = left,
                            Top = top,
                            Height = height,

                            Password = DigitalSignaturePassword,
                            Contact = "t@test.com",
                            Reason = "Reason",
                            Location = "xx,yy",
                            Visible = false
                        };

                        var imageSignOptions = new ImageSignOptions()
                        {
                            Left = left,
                            Top = top,
                            Height = height,
                            ImageStream = new MemoryStream(signatureImage)
                        };


                        signature.Sign(destination, new List<SignOptions> {imageSignOptions, digitalSignOptions});
                    }
                }


                return destination.ToArray();
            }
        }

Like last time this only happens when simultaneous connections occur. Then it throws an GroupDocsSignatureException with message “Out of memory”.

TIA.

@taxreturned, so just to confirm - the pdfBytes is the pdf document (how many pages or this is various?) - can you send some if this is not private? The Certificate Pfx is the RSA or DES ? the key size?
we have many similar Unit Tests passed well so I assume this is something to data related.
Can you describe if this code runs in API so it will be called parallel threads right?
I am trying to reproduce it much closely to your environment. How many times does it run per second?
Just to imagine entire picture
Thank you!

Hi,

If it’s a data issue won’t it happen most of the time and not only when it’s busy? This happens only when the server/web API is busy (but the server is not OOM, it has 64GB RAM and usage is about 60% when in full use).

The certificate is RSA. The actual documents contain confidential information, they’re all single page pdfs under 200kb, identical to the following except those will have actual data. And only going to be one of these 3.

64-8_2019-20 MT.pdf (182.3 KB)
P87 DigiSign Print Page 1.pdf (114.5 KB)
Signature Certificate.pdf (70.1 KB)

Yes, as it’s a Web API the call will get called parallelly. I would say max we’ll get 15-20 calls per second.

Thanks for your help.

Hi, @taxreturned
I am trying to reproduce the issue using your files and code and figured one issue related to other point - I see that CertificateStream is being passed to options - is this variable thread safe ?
just to confirm this is not shared resource between the multiple calls
UPD
I see you changed the code to use certificateBytes - so just to double check this is internal variable
I will update long today with the results.
Thanks

Hi @taxreturned
Please find attached archive with sample project that allows to run multiple tasks to signing the files in the folder randomly. So far I am not experiencing extremely memory leaks but I am going to stay the tests over the night run over 1-100M files and check if GC will handle it or we have some internal memory leaks.
I was trying to follow main block the same structure as you send. take a look please if it makes sense.

MultiThreadTest021.zip (339.8 KB)