Issue with Upload plugin or theme on Azure Web App

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hello,

Some of our clients that are using Azure reported us that their whole stores break when they try to use the Upload plugin or theme functionality from the nopCommerce administration.

We bought an Azure subscription to test this issue since it can only be reproduced there. We made a Web App with nopCommerce 4.00 installed on it.
When you try to upload the plugins from the administration the first time (after a clean install) there are no problems. The plugins are uploaded and you can install them. However, if you try to upload them again (say you want to update a plugin and need to replace the existing on your store with a newer version) you will get an error back.
The error is that some folder in the directory of the plugin is missing and nopCommerce cannot delete it (IOException).

After a lot of time spent in trying to figure out the problem, we narrowed it down to the public static void DeleteDirectory(string path) method in the Libraries/Nop.Core/CommonHelper.cs file.
It seems that the code on line 383:


try
{
    Directory.Delete(path, true);
}


is trying to delete all children of the directory and then delete the directory itself. This seems to be a problem when you have nested folders in your plugin package.

e.g. with PluginRootFolder/Areas/Admin/Views/someView.cshtml

Directrory.Delete will be called for the Areas/Admin/Views directory, essentially deleting someView.cshtml (or all the children inside the folder) and then deleting the folder Areas/Admin/Views.
Then on the next iteration of DeleteDirectory (this method is called recursively) the Directory.Delete method will try to delete all children of Areas/Admin, that is Areas/Admin/Views. However, Areas/Admin doesn't have children anymore since Areas/Admin/Views has been deleted in the previous iteration.
That will throw an IOException and since the catch block tries to call Directory.Delete for the same path (causing the same exception again), the deleting of the files of the plugin will stop mid-way leaving the plugin essentially broken since it is missing essential files.

At first, we thought this issue is only present in our products and was due to something we did in our code. However, we quickly realized this exception will happen every time you try to upload packages of plugins that have 2 or more nested folders in their root directory.

We found a solution that seems to work without a problem as far as we can tell. We would like to suggest it so we can be of any help.
All we did is change the try block in the DeleteDirectory method in the following way:

            
try
{
    Directory.Delete(path, true);

    GC.Collect();
    GC.WaitForPendingFinalizers();
}


The two added rows will ensure the deleting of the files in the previous iteration of the DeleteDirectory method will be finished before it tries to delete again. That means that in the example above the code will wait until Admin/Areas/Views is deleted and everything is ok before trying to delete the children of the Admin/Areas directory.

Note: We've only tested this solution for half an hour and didn't manage to brake it. However, do not take our word for it since more testing should be done for such a change in our opinion.

Looking forward to hearing from you!

Regards,
Anton
6 years ago
Hello,

Sorry that I am making two posts in a row but I forgot to mention the source of the code I've added. Here is the stack overflow question from where I got the idea to use the Garbage Collector to ensure that all files from the previous iteration of the DeleteDirectory methods are deleted before continuing.

Also, please note that that is a very serious issue for our clients using Azure. As far as we know it is not possible to use the upload plugins functionality from the nopCommerce administration with plugins that have nested folders in them. There are cases that, depending on which files got deleted, our clients experience errors that prevent them from accessing their stores until they manually delete and/or re-upload the plugins.

Regards,
Anton
6 years ago
Hi Anton,

Thanks a lot for detailed post and the solution. We'll investigate it soon - https://github.com/nopSolutions/nopCommerce/issues/2855
6 years ago
Done. Please see this commit for more details
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.