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.
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.
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.
I was able to replicate the issue using the test project on the following environments:
Env A
Winver.exe: MS Hyper-V Server 2019, Version 1809 (OS build 17763.3532)
CPU: 1 socket, 2 virtual processors
RAM: 8 GB
Env B
winver.exe: Windows Server 2019, Version 1809 (OS build 17763.3406)
CPU: 1 socket, 2 virtual processors
RAM: 8 GB
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.
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.
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.
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 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.
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)
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
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.