The requested operation cannot be performed on a file with a user-mapped section open

The issue: Document.Save method randomly throws exception.

The app processes multiple PDFs simultaneously. The issue happens with random files. The issue cannot be reproduced at will with the same file. The same file may produce the issue 1-2 times out of 30.

Unfortunately, I cannot provide a code snippet to reproduce the issue the app has a lot of logic and I was unable to identify what lines of code may cause the issue.

System.IO.IOException: The requested operation cannot be performed on a file with a user-mapped section open.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)     
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)   
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)  
at System.IO.FileStream..ctor(String path, FileMode mode)    
at Aspose.Pdf.Document.#=zmCBp3wj3d_ct(String #=zkfhq0KfKswXu)     
at Aspose.Pdf.Document.Save(String outputFileName)    
...
  • Any ideas?
  • Does Aspose.PDF use memory-mapped files? Does it release all resources?

For internal reference: MDP-14453

Thanks.

1 Like

@licenses

Aspose.PDF is a multithread safe API as long as one file is being accessed by one thread at a time. Also, Document.Save() method acts like Dispose() method i.e. it deallocate all the resources being used by the particular document being processed by the thread.

It is hard to determine the cause of error that you are facing. You can check in the code snippet and routine of your logic if there is a possibility for a file being accessed by two process simultaneously. You can also try to create a simple console application and try to replicate the issue with minimal code snippet and share that application with us so that we can further investigate and address the issue accordingly.

There was one more question.

Does Aspose.PDF use memory-mapped files?

@licenses

We will shortly get back to you on it.

@licenses

Not for now, although we are considering this in future to increase performance.

Hi.

Created a test project to replicate the issue.

The test project is a console app. Originally, the app contained code that changes PDFs. But after some testing, I was able to replicate the issue without this code. At the moment, the app simply opens a pdf file and then saves it back.

AsposePdfIssues.zip (286.8 KB)

A sample PDF file is included in the project.

To start the app run the following command:

.\AsposePdfIssues.exe --pdf all_metadata.pdf --iterations 10001 --threads 8

I was able to replicate the issue using the test project on the following environments:

  1. Env A
    • Winver.exe: MS Hyper-V Server 2019, Version 1809 (OS build 17763.3532)
    • CPU: 1 socket, 2 virtual processors
    • RAM: 8 GB
  2. Env B
    • winver.exe: Windows Server 2019, Version 1809 (OS build 17763.3406)
    • CPU: 1 socket, 2 virtual processors
    • RAM: 8 GB
  3. Env C
    • winver.exe: Windows 10, Version 21H2 (OS build 19044.2130)
    • CPU: 8 cores, 16 logical processors
    • RAM: 32 GB

Notes

  • Use different machines to replicate the issue.
  • You can play with the command line parameters. It is easier to reproduce the issue if the number of threads (-t parameter) is bigger than the number of logical processors.
  • Set iteration count (-i parameter) to a big value (for example, 10000).
  • Run the app multiple times until you replicate the issue on your environment.

Thanks.

@licenses

We have logged an investigation ticket as PDFNET-52849 in our issue tracking system to further analyze this case. We will look into its details and keep you posted with the status of its resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hello.

Is it possible to somehow change the priority of the PDFNET-52849?

This issue is really critical - I have several angry customers that constantly report new instances of this issue.

@licenses

The issue is related to API performance and we need to test for it on different environments. The issues in free support forum are resolved on first come first serve basis unlike paid support where critical issues are reported and are resolved on priority/urgent basis. Nevertheless, your concerns have been recorded and we will consider them during investigation. We will inform you once we have some more updates about its investigation.

We apologize for your inconvenience.

1 Like

Hi.

  • Have developers started to investigate the issue? Or PDFNET-52849 is still in the TODO queue?
  • Is the root cause known by now?

Thanks.

@licenses

Regretfully, no significant progress could be made towards ticket resolution yet. Your concerns were already recorded and we will surely inform you once we have some updates about ticket resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hi.

Was there any progress on this issue? Any updates there?

Thanks.

@licenses

We are afraid that the ticket could not get resolved due to other issues in the queue. We will surely inform you once we have some updates about its resolution. Please spare us some time. We apologize for the inconvenience.

Do you know if there was a resolution to this issue mentioned last October 2022? A release that it was fixed in?

PDFNET-52849

The requested operation cannot be performed on a file with a user-mapped section open.

@jdighton

We tested the solution over 30 times. So far we haven’t catch any exceptions. We will still continue our investigation but it looks like the issue is not reproducible.

We tried with 8/16/32 threads.

  • Based on the stack trace, we can point to the line that is causing the error.
using (FileStream fs = new FileStream(outputFileName, FileMode.Create))
{
//
}//outputFileName - it is path from your code(path for new file)

It is safe code.

  • Does Aspsoe.PDF use memory-mapped files?

No (in this case).

We investigated the solution and found little mistake:

public static bool IsSignedDocument(Document pdf)
{
    try
    {
        var pdfSign = new PdfFileSignature();
        pdfSign.BindPdf(pdf);
        return pdfSign.ContainsSignature();
    }
    catch (NullReferenceException)
    {
        return false;
    }
}

PdfFileSignature is disposable object, that disposes input document. Perhaps the GC is triggered at an unfortunate moment and the document (pdf) object starts to be destroyed.

Try this code snippet:

public static bool IsSignedDocument(Document pdf)
{
    try
    {
        using (var pdfSign = new PdfFileSignature())
        {
            pdfSign.BindPdf(pdf.FileName);
            return pdfSign.ContainsSignature();
        }
    }
    catch (NullReferenceException)
    {
        return false;
    }
}

Class Document is disposable too. We recommend to add ‘using’ too.

Thanks Asad.
Our error does not appear to be related to IsSignedDocument or FS. But during the Save process.
The error was is similar to this topic, so I posted it here.

We have been able to reproduce this error, but when it occurs, is the variable. It appears when generating a large group of documents, 2500+, but will fail sometimes after 30, 500, 1200, etc. never the same place in the same batch. This is what we get as a response:

The requested operation cannot be performed on a file with a user-mapped section open.

Module: System.IO.__Error
**Method:**WinIOError

Call Stack

.bbillBuildInvoice (xmlParms As String, printArgsStr As String) As String at line number: 7157
.ExternalReferences.Server.PdfDocument.Save(fileName As String) at line number: 50
Aspose.Pdf.Document.Save(outputFileName As String)

I appreciate your feedback.

@jdighton

We are checking it and will get back to you soon.

@jdighton

We have update the ticket information and will include your provided details in our ongoing investigation. As soon as we have some feedback to share, we will inform you. Please spare us some time.

We apologize for the inconvenience.

1 Like